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