In the realm of home automation, Home Assistant stands out for its flexibility and customization capabilities. A common scenario, when working with blueprints, involves using triggers based on input variables. But what if you need to modify these inputs for different triggers? This challenge led me to a solution that might be helpful for others.
Understanding the Challenge
My initial setup in Home Assistant involved a blueprint with an input variable named no_motion_duration
. This variable determined the duration for a motion-triggered event – specifically, turning off lights when no motion is detected. However, I needed to evolve this into a more complex scenario: creating two triggers based on the same input variable but with a slight modification in the second trigger.
The Breakthrough: trigger_variables
and Templating
The solution hinges on two key features of Home Assistant: trigger_variables
and templating within triggers. The difference between trigger_variables
and (regular) variables
is that the latter isn’t set until the automation actually runs – so we get to use trigger_variables
to work around this.
Step 1: Defining trigger_variables
First, we need to declare a trigger_variable
which holds our input:
trigger_variables:
no_motion_duration_var: !input 'no_motion_duration'
Code language: YAML (yaml)
Here, no_motion_duration_var
becomes a variable that holds the value of our original input no_motion_duration
. The reason for doing this is that the !input
syntax doesn’t work inside a template, but by converting it to a variable, we can now do this.
Step 2: The original trigger
The first trigger is straightforward. It activates when the specified no_motion_duration
lapses after detecting no motion:
trigger:
- platform: state
entity_id: !input 'motion_sensor'
to: "off"
id: 'no_motion_triggered'
for:
minutes: !input 'no_motion_duration'
Code language: YAML (yaml)
Step 3: Crafting the Second Trigger with Templating
The second trigger is where the real magic happens. We use the same input variable but modify it with a template:
- platform: state
entity_id: !input 'motion_sensor'
to: "off"
id: 'no_motion_triggered_turn_off'
for:
minutes: >-
{{ no_motion_duration_var | int + 2 }}
Code language: YAML (yaml)
In this trigger, we take the no_motion_duration_var
, convert it to an integer (if it’s not already), and then add 2 minutes. This means the second trigger will activate 2 minutes after the first trigger.
Why not just wait?
You might ask, why didn’t I just “wait” for this time to pass. Yes, you can, depending on the mode you use, and what other things your template is doing, you certainly can. This is for the cases where you can’t do that.
Conclusion
This approach demonstrates the power of trigger_variables
and templating in Home Assistant. By creatively using these features, you can craft complex and customised automation scenarios that fit your specific needs. Whether it’s for managing lights, climate, or security systems, it unlocks some pretty cool options