Hello, I want to clean out some of my yaml files and I wonder if I can simplify some of my input_boolean automations into binary_sensors. For example, my current bathroom_occupancy is an input_boolean and I use multiple automations to decide whether it’s on or off. I want to achieve the same with only one Template - Home Assistant
So in short, I want to change these automations:
- If motion detected → do
input_boolean_turn_on
for input_boolean.bathroom_occupied
- If door is closed AND motion detected → do
input_boolean_turn_on
for input_boolean.bathroom_occupied
- If humidity is high AND motion detected → do
input_boolean_turn_on
for input_boolean.bathroom_occupied
- If motion not detected → do
input_boolean_turn_off
for input_boolean.bathroom_occupied
- If door is opened AND input_boolean.bathroom_occupied is On → do
input_boolean_turn_off
for input_boolean.bathroom_occupied
Into something like this:
template:
- trigger: all the above
binary_sensor:
- name: Bathroom Occupied
state: {{ some templates }}
Thanks before
I think that maybe you could use a template trigger in your automaion something like this:
automation:
trigger:
- platform: template
value_template: >
{% if is_state('motion', 'on') on %}
{% elif (is_state('door', 'off') and (is_state('motion', 'on')) on %}
{% elif ....etc.... on %}
{% else off %}
{% endif %}
The ‘else’ at the end should ensure that the trigger resets when all the previous conditions are no longer true.
Hope that helps a bit!
1 Like
Thanks! Is it really just that simple? By the way, the second line of the template:
{% elif (is_state('door', 'off') and (is_state('motion', 'on')) on %}
In my automation, the door=off is a trigger, and motion=on is the condition. Will this work the same? Or is there a way to make conditions in the template?
Yes I think it will work the same. The trigger has to be true to trigger the automation and the condition has to also be true to execute the following actions - this is therefore an ‘and’ function.
The other option would be to create a bathroom_occupied template binary sensor and stick all your logic in there so the automation just needs binary_senor.bathroom_occupied as a state trigger. The advantage of this method would be that you can include the sensor in your front end panels if that would be useful.
In case you are not aware, it is good practise to test your template in Developer Tools - Template first to make sure the syntax is correct and it works as expected.
Thanks!
Yes, this is what I want to do with the binary_sensor template.
Yes, and I think your code above, the on %}
should be %} on
to work EDIT: Also, the (is_state())
should be is_state()
:
automation:
trigger:
- platform: template
value_template: >
{% if is_state('motion', 'on') %} on
{% elif is_state('door', 'off') and is_state('motion', 'on') %} on
{% elif ....etc.... %} on
{% else %} off
{% endif %}
I will try it out and see if it works. Thanks again
Here’s what I got in the end in case somebody need this:
template:
- binary_sensor:
- name: Bathroom Occupancy
device_class: occupancy
state: >
{% if is_state('binary_sensor.bathroom_motion_occupancy', 'on') %}
on
{% elif is_state('binary_sensor.bathroom_door_contact', 'off')
and is_state('binary_senor.bathroom_motion_occupancy', 'on') %}
on
# If humidity is above 60 and there's motion or door is closed, in case you are taking a shower without doing any movement for a long time
{% elif states('sensor.bathroom_humidity')|int > 60 %}
{% if is_state('binary_sensor.bathroom_motion_occupancy', 'on') %}
or is_state('binary_sensor.bathroom_door_contact', 'off') %}
on
{% endif %}
{% else %}
off
{% endif %}