If you're like me you probably started your Home Assistant automation journey by emulating or straight up copying some of the readily available example code in the Home Assistant documentation.
You probably started off automating a lightbulb or switch using a simple trigger
, maybe even a condition
and finally an action
containing some service call like light.turn_on
or switch.turn_on
. This is one of the most straightforward type of automations and therefore the perfect way to get started. I want to clarify that there is absolutely nothing wrong with this and it all makes perfect sense.
However most of the time when there is a turn_on
automation there is a high likelyhood that there is also a need for a turn_off
equivalent. Often times these automations are quite similar except their actions and conditions are oposite. For example; when there is motion detected turn on the lights, if the motion is no longer detected turn them off.
Let's look at a very basic example of this. Below I have a very basic automation that turns on the hallway light as soon as motion is detected:
- id: hallway_001
alias: hallway_001
trigger:
platform: state
entity_id: binary_sensor.hallway_motion_detected
to: 'on'
action:
service: light.turn_on
entity_id: light.hallway
If we were to leave it like this it will never ever turn off so what we also need is an automation that turns it off if we're not in the hallway. The most basic version would probably look something like this:
- id: hallway_002
alias: hallway_002
trigger:
platform: state
entity_id: binary_sensor.hallway_motion_detected
to: 'off'
for:
minutes: 5
action:
service: light.turn_off
entity_id: light.hallway
You see how these automations are practically the same except for some reverse state triggers and reverse service calls, seems a bit wasteful right?. In comes the magic of templating in Home Assistant to save the day! By using multiple triggers and determining the trigger.to_state
we can merge these two automations into a neat single serving automation. Let me give you the result of this below:
- id: hallway_001
alias: hallway_001
trigger:
- platform: state
entity_id: binary_sensor.hallway_motion_detected
to: 'on'
- platform: state
entity_id: binary_sensor.hallway_motion_detected
to: 'off'
for:
minutes: 5
action:
service_template: >
{% if trigger_to_state == 'on' %}
light.turn_on
{% elif trigger_to_state == 'off' %}
light.turn_off
{% endif %}
entity_id: light.hallway
I hope by this point you realize the power and potential this has to logically merge/bundle your automations and make the whole codebase less repetitive.
This however is just a very basic example without very specific conditions. In this example we've made the service
dynamicly assigned but we can even go further by dynamically assigning the entity_id
as well. This opens up a whole new world of possibilities to make your automations more consise and logically bundled.
As soon as you master this concept you are able to cut down on repetitive code chunks and create a more easily testable and maintainable Home Assistant codebase.
I will be writing way more on advanced templating and utilizing the power of scripts to abstract logic in a future post so be sure to keep an eye out for more tutorials and practical tips here.
Comments