core: Automatons ignore state trigger

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.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (14 by maintainers)

Most upvoted comments

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 }}'

@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

@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

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. 😃

Yes, I will test it after work.

This is very likely caused by same issue as #37902