I have three motion sensors. I am seeking ideas of how to trigger/condition an automation if Two Out of the three (or More) are triggered. Currently, I can complete the task with multiple automations. Is it possible to have this in a single automation?
Someone like @123 would need to pop along and provide the actual code, but - if you put the sensors in a group, then using a template in the condition, you expand the group, select the sensors that are on and then count them, and check if there are more than 1 in the group that are currently on.
Something like:
{{ expand('group.group_name')|selectattr('state','eq','on')|list|count > 1 }}
Just going to post this as I was typing it up already…
{% set sensors = ['binary_sensor.sensor_one', 'binary_sensor.sensor_two', 'binary_sensor.sensor_three'] %}
{{ expand(sensors) | selectattr('state', 'eq', 'on') | list | count > 1 }}
Another way:
{{ states('binary_sensor.sensor_one')|bool(0) + states('binary_sensor.sensor_two')|bool(0) + states('binary_sensor.sensor_three')|bool(0) > 1 }}
|bool
is a recently introduced filter that converts common HA states (like on
or off
) to true
or false
, which are just representations of 1
or 0
respectively.
interesting that it converts them to integers so they can be added together.
It does not convert them. They are integers.
Put this in the template editor:
{{ 'on' | bool(0) }}
{{ 'on' | bool(0) |is_number }}
The conversion of boolean true
to integer 1
(false
/0
) is found in python (and in other languages as well).
For example, the result of this {{ true + true }}
is 2
.
FWIW, in the other home automation software I use, a binary_sensor’s state is a boolean value (not on
/off
strings) so you can easily do arithmetic with their states (like what tom_l did) without needing to explicitly convert them first.
Sorry everyone. It is now apparent I am behind on my leaning curve. I can see the recommended templates to determine the 2-outof-3 automation trigger, but I am lost in the syntax and placement of the code.
Add all the sensors you want to trigger the automation in the triggers part of the automation, then add the supplied code in the condition part of the automation - choosing “template” as the type of condition.
What the code is doing is selecting ONLY the sensors that match the state “on”, and then doing a simple test to see if the number of sensors matching that state - is more than one.
{{ states('binary_sensor.sensor_one')|bool(0) + states('binary_sensor.sensor_two')|bool(0) + states('binary_sensor.sensor_three')|bool(0) > 1 }}
So taking @tom_l 's example -
the automation is triggered by binary_sensor.sensor_two
the automation triggers, but the condition is false because only sensor is in the on state ( 0 + 1 + 0).
before binary_sensor.sensor_two
goes back to off, binary_sensor.sensor_one
changes to the on state, and the automation is triggered again, now the condition is true because (1 + 1 + 0) is more than 1. So now the actions in the automation can run.
@tom_l Thanks for your assistance. I used the template editor and understand the binary output. I have been toying with this for quite some time and just can not get the automation to pass the condition. Using the trace, I can verify the automation was triggered three times and the condition failed each instance. I am certain my issue is with the syntax of the condition, as mentioned earlier that I am lost in the syntax and learn by examples I find in this very helpful forum.
I am including two attachments to help visualize my results and the verbatim code used in this revision.
alias: driveway_motion
id: driveway_motion
description: ''
trigger:
- platform: state
entity_id: binary_sensor.driveway_1_motion
to: "on"
- platform: state
entity_id: binary_sensor.driveway_2_motion
to: "on"
- platform: state
entity_id: binary_sensor.driveway_3_motion
to: "on"
condition:
- condition: template
value_template: >-
template {{ states('binary_sensor.driveway_1_motion')|bool(0) +
states('binary_sensor.driveway_2_motion')|bool(0) +
states('binary_sensor.driveway_3_motion')|bool(0) > 1 }}
action:
- service: notify.alexa_media
data:
message: Driveway inferred sensor activated.
target:
- media_player.kitchen
- media_player.computer_room
- media_player.bed_room
- media_player.basement
- media_player.toy_box
data:
type: announce
mode: single
At least two of the binary sensors have to be on simultaneously.
Understood, First was #3 at 4:51:58, with it still activated #2 at 4:52:01, then again with both of the previous two activated, #1 4:52:02
… the graphic attached shows they were all “on”.
Something must be funky with my condition. By chance, can this be related to my search of locating a template error in my logs) …
Your template is wrong
value_template: >-
template ##### <- what is this?
This is what you should have:
condition:
- condition: template
value_template: >
{{ states('binary_sensor.driveway_1_motion')|bool(0) +
states('binary_sensor.driveway_2_motion')|bool(0) +
states('binary_sensor.driveway_3_motion')|bool(0) > 1 }}
Thank you @tom_l ! I am new to the template and value template thing. By chance, would you have any recommended reading that could improve my understanding?
Also… A huge thank you for your support given to all in the community. Reading various threads has opened up a world of possibilities in HA! Thank you !!!
@007DJ, I have been using HA for literally years now. I am not a coder and I am not a young guy. I really suck at anything template based and reading docs written for templates expects a level of understanding that I cannot wrap my brain around. So, I tend to write really large automations with the choose command and make things work that way, I do understand the logic flow in automations. I recently ran across a job that I could not make work that way and had to template it, the template made it so short and so clean.
I guess my point of all of this is that the folks here are fantastic at being patient and helping those of us who just do not get something. I to learn by example and and beginning to get a handle on basic templates, yes basic templates. I tend to read as many threads as I have time for here because it really helps me to see a good code block and the associated reasoning behind it! To boot, this is a trick that I feel that I will use in the future as well.