Core: Automatons ignore state trigger

Created on 16 Jul 2020  路  23Comments  路  Source: home-assistant/core

The problem


When binary sensors go from "on" to "off" while waiting for a "for" statement, the "off" state doesn't stop the "for" timer.

Environment

  • Home Assistant Core release with the issue: 0.113b0
  • Last working Home Assistant Core release (if known): 0.112.3
  • Operating environment (OS/Container/Supervised/Core): Container
  • Integration causing this issue: automation
  • Link to integration documentation on our website: n/a

Problem-relevant configuration.yaml

- alias: Door Left Open

  trigger:
    platform: state
    to: "on"
    entity_id:
      - binary_sensor.garagedoor
      - binary_sensor.patiodoor
      - binary_sensor.frontdoor

    for:
      minutes: 15

  action:
    - service: script.house_annoucement
      data_template:
        this_message: "{{trigger.to_state.name}} left open."
        volume: 0.8

Traceback/Error logs

n/a

Additional information

In the automation example above, when the door is indeed closed within the 15 minutes, this automation still fires.

automation

All 23 comments

Hey there @home-assistant/core, mind taking a look at this issue as its been labeled with an integration (automation) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

I have a similar problem.
The following two automations worked with home assistant 0.112.
But the trigger and the values in the data_templates for the sensor "sensor.aktuelle_harmony_wohnzimmer_aktion" are ignored with homeassistant 0.113 beta.

  - id: harmony_hub_aktion_starten
    alias: Harmony Hub Aktion starten
    initial_state: on
    trigger:
    - entity_id: input_select.harmony_wohnzimmer
      platform: state
    action:
    - service: remote.turn_on
      entity_id: remote.harmony_wohnzimmer
      data_template:
        activity: |
          {% if is_state("input_select.harmony_wohnzimmer", "Fern-sehen") and not is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "Fern-sehen") %}
            12688954
          {% elif is_state("input_select.harmony_wohnzimmer", "PlayStation 3 Slim") and not is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "PlayStation 3 Slim") %}
            12688955
          {% elif is_state("input_select.harmony_wohnzimmer", "Filme") and not is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "Filme") %}
            35751285
          {% elif is_state("input_select.harmony_wohnzimmer", "PowerOff") and not is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "PowerOff") %}
            -1
          {% else %} {% endif %}

  - id: harmony_hub_abfragen
    alias: Harmony Hub abfragen
    initial_state: on
    trigger:
    - platform: state
      entity_id: sensor.aktuelle_harmony_wohnzimmer_aktion
    - event: start
      platform: homeassistant
    action:
    - service: input_select.select_option
      entity_id: input_select.harmony_wohnzimmer
      data_template:
        option: |
          {% if is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "PowerOff") and not is_state("input_select.harmony_wohnzimmer", "PowerOff") %}
            PowerOff
          {% elif is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "Fern-sehen") and not is_state("input_select.harmony_wohnzimmer", "Fern-sehen") %}
            Fern-sehen
          {% elif is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "PlayStation 3 Slim") and not is_state("input_select.harmony_wohnzimmer", "PlayStation 3 Slim") %}
            PlayStation 3 Slim
          {% elif is_state("sensor.aktuelle_harmony_wohnzimmer_aktion", "Filme") and not is_state("input_select.harmony_wohnzimmer", "Filme") %}
            Filme
          {% else %} {% endif %}

This is very likely caused by same issue as #37902

diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py
index c4a6de8397..80ffaf5653 100644
--- a/homeassistant/helpers/event.py
+++ b/homeassistant/helpers/event.py
@@ -163,7 +163,7 @@ def async_track_state_change_event(

             _LOGGER.debug("_async_state_change_dispatcher: %s", entity_id)

-            for action in entity_callbacks[entity_id]:
+            for action in entity_callbacks[entity_id][:]:
                 try:
                     hass.async_run_job(action, event)
                 except Exception:  # pylint: disable=broad-except

async_track_same_state pushes a new callback into the list by calling async_track_state_change when the original event is still being processed in _async_state_change_dispatcher

However I can't see how that breaks anything

What is providing sensor.aktuelle_harmony_wohnzimmer_aktion ?

- id: '1594920465682'
  alias: Master Bed Room is Left Open
  description: ''
  trigger:
  - entity_id:
    - input_boolean.fake_sensor_1
    - input_boolean.fake_sensor_2
    for: 0:01:00
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: light.family_room_lights
    service: light.turn_on
  mode: single

Screen Shot 2020-07-16 at 12 37 06 PM

I've tried the above automation in order to attempt to replicate this without success

@firstof9 @Krocko

Does the below patch resolve the issue for you?

diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py
index c4a6de8397..80ffaf5653 100644
--- a/homeassistant/helpers/event.py
+++ b/homeassistant/helpers/event.py
@@ -163,7 +163,7 @@ def async_track_state_change_event(

             _LOGGER.debug("_async_state_change_dispatcher: %s", entity_id)

-            for action in entity_callbacks[entity_id]:
+            for action in entity_callbacks[entity_id][:]:
                 try:
                     hass.async_run_job(action, event)
                 except Exception:  # pylint: disable=broad-except

Testing it now

Initial test seems good. I'll enable the other automations for a full test

May be fixed by #37924 but we need beta testers to confirm

What is providing sensor.aktuelle_harmony_wohnzimmer_aktion ?

It is a sensor that is showing the current harmony activity.

What is providing sensor.aktuelle_harmony_wohnzimmer_aktion ?

It is a sensor that is showing the current harmony activity.

Would you please paste the yaml that created it?

What is providing sensor.aktuelle_harmony_wohnzimmer_aktion ?

It is a sensor that is showing the current harmony activity.

Would you please paste the yaml that created it?

Sure

sensor:

  - platform: template
    sensors:

      aktuelle_harmony_wohnzimmer_aktion:
        friendly_name: Aktuelle Harmony Aktion
        value_template: '{{ states.remote.harmony_wohnzimmer.attributes.current_activity }}'

Thanks. Before I go try and replicate this, can you test 0.113b1 as other have reported that #37924 fixed a similar issue.

Yes, I will test it after work.

I also had the problem in 0.113.0b0 with this trigger:

      - platform: state
        entity_id: binary_sensor.house_front_sensor
        to: 'on'
        for:
          minutes: 15

Same happened with a similar numeric_state trigger.

After upgrading to 0.113.0b1 it has not happened.

Also, looking at the code change, it makes sense -- the list being iterated was changing during the iteration, which can cause weird effects like this. Copying the list using [:] before iterating is a classic way to solve that problem. 馃槂

@bdraco For me it is not fixed with 0.113.0b1.

@bdraco For me it is not fixed with 0.113.0b1.

It's likely a different issue as your example has no delay or call to track same state. I'll see if I can replicate it now that I have all the yaml

Thank you. If you turn off the activity with the physical remote, or the native app, homeassistant turn on the activity again. So it is a Ping-Pong.

Thank you. If you turn off the activity with the physical remote, or the native app, homeassistant turn on the activity again. So it is a Ping-Pong.

Harmony is a lot faster at updating the activity now.

That might be this change https://github.com/home-assistant/core/pull/37559

I'll know for sure once I get the testing setup.

@Krocko Its caused by #37559. Harmony is too fast now.

Try this instead

- id: harmony_hub_aktion_starten
  alias: Harmony Hub Aktion starten
  trigger:
  - entity_id: input_select.harmony_wohnzimmer
    platform: state
  condition:
  - condition: template
    value_template: '{{ states("input_select.harmony_wohnzimmer") != states("sensor.aktuelle_harmony_wohnzimmer_aktion")
      }}'
  action:
  - data_template:
      activity: '{{ states("input_select.harmony_wohnzimmer") }}'
    entity_id: remote.harmony_wohnzimmer
    service: remote.turn_on
  initial_state: true
  mode: single
- id: harmony_hub_abfragen
  alias: Harmony Hub abfragen
  trigger:
  - entity_id: sensor.aktuelle_harmony_wohnzimmer_aktion
    platform: state
  - event: start
    platform: homeassistant
  condition:
  - condition: template
    value_template: '{{ states("input_select.harmony_wohnzimmer") != states("sensor.aktuelle_harmony_wohnzimmer_aktion")
      }}'
  action:
  - data_template:
      option: '{{ states("sensor.aktuelle_harmony_wohnzimmer_aktion") }}'
    entity_id: input_select.harmony_wohnzimmer
    service: input_select.select_option
  initial_state: true
  mode: single

Thanks @bdraco
I am now on a similar automation with success.

  - id: harmony_hub_aktion_starten
    alias: Harmony Hub Aktion starten
    initial_state: on
    trigger:
    - entity_id: input_select.harmony_wohnzimmer
      platform: state
    condition:
    - condition: template
      value_template: '{{ states.input_select.harmony_wohnzimmer.state != states.remote.harmony_wohnzimmer.attributes.current_activity }}'
    action:
    - service: remote.turn_on
      entity_id: remote.harmony_wohnzimmer
      data_template:
        activity: '{{ states.input_select.harmony_wohnzimmer.state }}'

  - id: harmony_hub_abfragen
    alias: Harmony Hub abfragen
    initial_state: on
    trigger:
    - platform: state
      entity_id: sensor.aktuelle_harmony_wohnzimmer_aktion
    - event: start
      platform: homeassistant
    condition:
    - condition: template
      value_template: '{{ states.input_select.harmony_wohnzimmer.state != states.remote.harmony_wohnzimmer.attributes.current_activity }}'
    action:
    - service: input_select.select_option
      entity_id: input_select.harmony_wohnzimmer
      data_template:
        option: '{{ states.remote.harmony_wohnzimmer.attributes.current_activity }}'
Was this page helpful?
0 / 5 - 0 ratings