Weather notification when the state changes from one populated value to another

Hi Everyone.

I have an automation

alias: NOTIFY_Weather Statement
description: Sends a notice when the weather sensor changes from null to populated
trigger:
  - platform: template
    value_template: >-
      {% if states('sensor.weather_location_1_warnings') != "" %}True{%
      endif %}
condition: []
action:
  - service: notify.mobile_app_phone1
    data:
      title: Weather Alert!
      message: >-
        There is a weather alert presently. Be advised of
        {{states('sensor.weather_location_1_warnings')}}.
      data:
        tag: weather_alert
  - service: notify.mobile_app_phone2
    data:
      title: Weather Alert!
      message: >-
        There is a weather alert presently. Be advised of
        {{states('sensor.weather_location_1_warnings')}}.
      data:
        tag: weather_alert
mode: single

Now essentially when the sensor has no warning or weather statement it’s null. And this (I’m not sure if it even works yet as I only made it today :stuck_out_tongue: but the state in the developer tab seems to work). But this would only fire when it changes from null to something.
How would I have it fire when it changes from one warning to another?

For example from snow storm warning to blizzard warning.
Note that these are warnings from the “Environment Canada” weather integration.

Thank you.

I recommend you use this instead (State Trigger vs Template Trigger).

alias: NOTIFY_Weather Statement
description: Sends a notice when the weather sensor changes from empty string to populated
trigger:
  - platform: state
    entity_id: sensor.weather_location_1_warnings
condition: "{{ trigger.to_state.state != '' }}"
action:
  - variables:
      msg: 'There is a weather alert presently. Be advised of {{ trigger.to_state.state }}.'
  - service: notify.mobile_app_phone1
    data:
      title: Weather Alert!
      message: '{{ msg }}'
      data:
        tag: weather_alert
  - service: notify.mobile_app_phone2
    data:
      title: Weather Alert!
      message: '{{ msg }}'
      data:
        tag: weather_alert
mode: single

The Template Trigger you created is triggered when the sensor’s value changes from an empty string to a non-empty string. When that happens, the Template Trigger is “set”. It will be “reset” only when the sensor’s value changes to an empty string. That means if the sensor’s value changes from one non-empty string to another non-empty string (“Winter Storm Warning” to “Blizzard Warning”) it will not trigger. It must first change to an empty string before it can be triggered again.

The State Trigger I have suggested doesn’t share this behavior and is triggered by any change to the sensor’s value. The condition ensures only non-empty strings are accepted.

1 Like

Thanks! I’ll give it a bash! :slight_smile:

I’ll mark it as the solution for now.

Okay so this does work, but… :stuck_out_tongue:

So it seems that anytime the state updates from the sensor it sends a notification. Even if it changes from the same warning to the same warning.

Do I need to store the current state in an entity somewhere and do compare operations?

That’s because, by default, a State Trigger will also trigger if the value of any of the entity’s attributes changes. That particular sensor has an observation_time attribute whose value is updated when data is received.

To limit the State Trigger to listen to changes of the state value only (i.e. ignore changes to attribute values) simply append an empty to: option to the State Trigger.

  - platform: state
    entity_id: sensor.weather_location_1_warnings
    to:

That’s what I love about this community, learning the ins and outs :).

Thanks for this!

1 Like