Problems with automation false triggering

I created an automation to send push notification on weather alert. Alert seems to update quite frequently (every hour or so) it triggers automation despite the fact that neither value of sensor state nor attributes are changong (just update to the same state). I tried to limit triggering of action to avoid sending over o and over the same notification, but without success. here is my automation:

id: weather_alert_notification
alias: Weather Alert Notification
initial_state: true
trigger:
  - platform: state
    entity_id: sensor.meteoalarm_weather_alert
    for: '00:00:05'
condition:
  - condition: template
    value_template: '{{ states(''sensor.meteoalarm_weather_alert'') != ''N/A'' }}'
  - condition: template
    value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'
action:
  - service: notify.mobile_app_mireks_iphone
    data:
      title: ALARM POGODOWY
      message: '{{state_attr(''sensor.meteoalarm_weather_alert'', ''alert'')}}'
      data:
        subtitle: '{{states(''sensor.meteoalarm_weather_alert'')}}'
        tag: weather

As you can see I tried to apply limits:

  • to send notification only when alert state after change is not N/A (so no notification, when alert is revoked)
  • to send notification only when original and new trigger states are not the same (so send only when there is real change in alert type or change from N/A)
  • finally to send notification, when change is persistant for at least 5 seconds (to avoid possible race conditions in sensor state value calculation)

All of this does not work and I receive push notification on every state change. Strangely, when I look at history of automation I can see that every time sensor state change occures, there are 2 automation runs’ exactly’ at the same time. The first one seems not to honor conditions (and sends notification:

And the second, triggered split of the second later, that run as intended:

It could be explained by some race condition in updating sensor value, that could cause interim state of sensor value, but since this change would be applied to the very first automation run, message send should contain data that is different from stable state of sensor, but this is not the case, message contains exactly expected information with current sensor state. So the second automation run properly disregards. What is triggering the automation if target and initial states are the same? Why is delay (for: "00:00:05’) not working, not waiting for state to stabilize at the first state chnage (if there is any). Please help me to understand what is going on here!

Try adding a condition to remove trigger to and from states being unavailable or unknown.

  condition:
    - "{{ states('sensor.meteoalarm_weather_alert') != 'N/A' }}"
    - "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
    - "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"
    - "{{ trigger.from_state.state != trigger.to_state.state}}"

https://www.home-assistant.io/docs/scripts/conditions/#template-condition-shorthand-notation

These days you can skip the current/previous state check with:

  - platform: state
    entity_id: sensor.meteoalarm_weather_alert
    to: 
    for: '00:00:05'

https://www.home-assistant.io/docs/automation/trigger/#state-trigger

1 Like