Automation Best Practice - State Change or Tirgger ID

I’m moving my automations back to HA from Node-Red. There are many ways to do the same thing. I want to make sure I’m doing automations that are optimal / best practice.

One of my first ones is a simple door sensor, turning a light on and off. I did it two different ways and I’m not sure which way is the “best” way or if I’m not even in the ball park and should be doing somthing different. They both work. Which one is the best approach?


This one just watches for any state change by the door sensor and the chooses the action

alias: Pantry Door Light Option 1
description: Door open/close, on/off lights
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_pantry_door_sensor_status
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.kitchen_pantry_door_sensor_status
            state: "on"
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_pantry_light
      - conditions:
          - condition: state
            entity_id: binary_sensor.kitchen_pantry_door_sensor_status
            state: "off"
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_pantry_light
mode: single

This one uses muitple “Trigger IDs” and then chooses what to do based on which trigger fired.

alias: Pantry Door Light Option 2
description:  Door open/close, on/off lights
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_pantry_door_sensor_status
    to: "on"
    id: door-open
  - platform: state
    entity_id:
      - binary_sensor.kitchen_pantry_door_sensor_status
    to: "off"
    id: door-closed
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: door-open
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_pantry_light
      - conditions:
          - condition: trigger
            id: door-closed
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_pantry_light
mode: single

I personally use Trigger IDs when I’ve got multiple triggers that fall into triggering the same actions as that allows me to collapse the choose in the actions easier.

Either one would technically work but the first one will trigger on not only every state change but on every attribute change as well.

you can stop that by using a blank “to:” so it only listens for states changes:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_pantry_door_sensor_status
    to:

But then the other thing to be concerned about would be if your binary sensor ever goes to unavailable or unknown. if it does then it can be mitigated like:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_pantry_door_sensor_status
    not_from:
      - 'unavailable'
      - 'unknown'
    to:

then it really doesn’t matter and it’s just down to personal preference

1 Like

FWIW, my preference:

alias: Pantry Door Light Option 1
description: Door open/close, on/off lights
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_pantry_door_sensor_status
    from:
      - 'on'
      - 'off'
    to:
      - 'off'
      - 'on'
condition: []
action:
  - service: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: switch.kitchen_pantry_light
mode: single

It explicitly restricts the state-changes to/from nominal values (i.e. on/off only) and then uses the value in a service template.

1 Like