Automations #1: trigger only fires when it changes from not true to true

In an automation written to run when the heating drops below 20C, the trigger will fire when the temperature changes from 20 to 19.

trigger:
  - platform: numeric_state
    entity_id:
      - climate.study
    for:
      hours: 0
      minutes: 10
      seconds: 0
    below: 20

It will not fire again until the temperature rises above 20C and drops for a second time.

A trigger is an event… Not a state, but a change of state.

You can have more than one trigger:

trigger:
  - platform: numeric_state
    entity_id:
      - climate.study
    for:
      hours: 0
      minutes: 10
      seconds: 0
    below: 20
  - platform: numeric_state
    entity_id:
      - climate.study
    for:
      hours: 0
      minutes: 10
      seconds: 0
    below: 18

This automation will run when either one of the triggers fires - when the temperature drops from 20 to 19 or when it drops from 18 to 17. If the thermostat goes up from 17 to 19, then falls again, the automation rill run a second time because the second trigger has fired again.

A common issue is that the actions will not run during HA start-up should the entity ID’s value already be above or below the set threshold. In that case, one must trigger on the HA start event and add a guard condition.

trigger:
  - platform: numeric_state
    entity_id:
      - climate.study
    below: 20
  - platform: homeassistant
    event: start
condition:
  - condition: numeric_state
    entity_id: 
      - climate.study
    below: 20 

With state triggers (not with numeric state) you can leave the trigger value unspecified and use conditions in the action block:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.daytime
action:
  - if:
      - condition: state
        entity_id: binary_sensor.daytime
        state: "off"
    then:
      - service: light.turn_on
        target:
          entity_id: light.yard
        data: {}
    else:
      - service: light.turn_off
        target:
          entity_id: light.yard
        data: {}

This trigger will fire every time the state of binary_sensor.daytime changes. Be careful when you do this - some entities change state very quickly and the automation will run every time. It will also fire if the value of the sensor changes to “unavailable” and whenever any of the entity’s attributes change.

To prevent it firing when the sensor is unavailable:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.daytime
    not_to:
      - unknown
      - unavailable

Testing

You can test an automation in the UI by clicking Run in the three dots menu (top right).

However, this only runs the Action block of the automation. To test triggers and conditions as well, go to Developer Tools | States and change the value of the trigger entity.

Note: Developer Tools only changes sensor values temporarily. They will revert to their correct value next time the sensor updates.

The service automation.trigger will also run an automation. You can use this in Developer Tools | Services:

It does not test its triggers and by default it also bypasses any conditions, but setting the skip_conditions attribute to false will include these in a test.


The Home Assistant Cookbook - Index.

4 Likes

Should one add a few things how to mitigate common follow-up questions?

For example, one issue is that numeric state triggers won’t update upon start-up, so you need to trigger on the start event and add a numeric state condition.

2 Likes

Yes please… :grin:

1 Like