Trigger a notification only on state change, but not on state update with no state change

I’m trying to create a notification that is triggered by the weather sensor

- id: '1620286122573'
  alias: Weather
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.casa_weather_condition
  condition: []
  action:
  - service: notify.notify
    data:
      message: '{{states(''sensor.casa_weather_condition'') }}'
  mode: single

I didn’t specify anything in “from” or “to” as I want it triggered when changing from “sunny” to “cloudy”, or “cloudy” to “rainy”, anything basically
The problem is that the sensors seems to be updated every 15 minutes, even if the state is always “cloudy”, so the automation is triggered even if it shouldn’t

How can I avoid this?

The behaviour you’re seeing is as documented. I think you’ll have to specify all the possible states in either from: or to:.

I don’t think that will make it behave differently; the only solution I see is creating n*(n-1) different triggers, where n is the number of states

- id: '1620286122573'
  alias: Weather
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.casa_weather_condition
  condition:
  - condition: template
    value_template: "{{ trigger.from_state != trigger.to_state }}"
  action:
  - service: notify.notify
    data:
      message: '{{states(''sensor.casa_weather_condition'') }}'
  mode: single
2 Likes

Won’t that condition still allow for changes of attribute values?

For example, if the state value remains unchanged, but some attribute’s value changes, it will trigger the State Trigger, the condition will evaluate to true, and the action will be executed.

Perhaps you meant this?

    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
4 Likes

Yes, you’re right Taras.

I’m trying to do something like this but in a template sensor - not an automation. I scrape my alarm status from a website, and want to know when the status (state) last changed. The state can be anyone of about 6 different values, and I want to trigger the template if it changes from anyone of those possible states to anyone different.

Can anyone show me how do this in a template? I see above the coding for an automation - but I’m not sure about doing it in a template. My template so far is shown below (which triggers on restarts as well as changes so doesn’t yet help me):

#          
#  ALARM STATE LAST CHANGED (TEST TEMPLATE)
  - trigger:
      - platform: state
        entity_id: sensor.home_alarm_status
    sensor:
      - name: alarm status changed
        state:  "{{ now() }}"

'''

Try this:

  - trigger:
      - platform: state
        entity_id: sensor.home_alarm_status
        not_from:
          - unknown
          - unavailable
    sensor:
      - name: alarm status changed
        state:  "{{ now() }}"
1 Like

Seems to do the trick! Thanks so much!