I want to create a binary_sensor that is set to on when two other binary_sensors change from off to on (and condition) and is set to off when the same two binary_sensors change from on to off (also and condition).
Currently I do it with a couple automations and an input_boolean helper, which is set to on or off based on state of the two binary_sensors, and then the binary_sensor changes it state based on the state of the input_boolean.
Is there a way of doing if without the helper? I know that triggers may be used in sensors and binary_sensors, but I donāt get to replicate the desired behaviour.
input_boolean:
movimiento_piso
automation:
- id: alarma_movimiento_piso_on
alias: Alarma:Movimiento piso on
description: ''
trigger:
- platform: state
entity_id: binary_sensor.sonoff_a480019e03
from: 'off'
to: 'on'
- platform: state
entity_id: binary_sensor.sonoff_a480028dba
from: 'off'
to: 'on'
condition:
- condition: and
conditions:
- condition: state
entity_id: binary_sensor.sonoff_a480019e03
state: 'on'
- condition: state
entity_id: binary_sensor.sonoff_a480028dba
state: 'on'
action:
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.movimiento_piso
mode: single
- id: alarma_movimiento_piso_off
alias: Alarma:Movimiento piso off
description: ''
trigger:
- platform: state
entity_id: binary_sensor.sonoff_a480019e03
from: 'on'
to: 'off'
- platform: state
entity_id: binary_sensor.sonoff_a480028dba
from: 'on'
to: 'off'
condition:
- condition: and
conditions:
- condition: state
entity_id: binary_sensor.sonoff_a480019e03
state: 'off'
- condition: state
entity_id: binary_sensor.sonoff_a480028dba
state: 'off'
action:
- service: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.movimiento_piso
mode: single
binary_sensor:
- platform: template
sensors:
movimiento_piso:
friendly_name: Sensor Movimiento piso
device_class: motion
value_template: "{{ is_state('input_boolean.movimiento_piso', 'on') }}"
The behaviour is not exactly what i tried, because with a group if all parameter is on, the group is set to on if all the entities are on as desired, but it is set to off if any of the entities are set to off. In any case, I think this behaviour is satisfactory for my use case.
I still need a binary_sensor, so I set its state based on the group state, so the automations are no longer needed.
With template I get the same behaviour as with group.
When both binary_sensors are set to on, the main binary_sensor is set to on, and afterwards, when one of them is set to off, the main is also set to off. I cannot get the behaviour that itās set to off only when both binary_sensors are set to off after a ātriggeringā.
In any case, as I said itās not a big problem, and I think it cannot be done with templates, because the desired behaviour needs a state dinamic model, which I suppose it can only be done with automations.
Itās possible you could do this by setting up a ānumberā that you increment/decrement as the other sensors change.
The initial value would be zero; whenever any of the other sensors move from off->on you increment the number by 1. Whenever any of the other sensors move from on->off you decrement the number by 1.
Then you can trigger on the number becoming 2, or becoming 0.
I suppose Iāll need a couple of automations to increment/decrement the ānumberā helper, so itās similar to my initial code, where I used a helper and a couple of automations.
This doesnāt work for the use case I presented, because in my case, if only one of the sensors is on, the main binary_sensor state shouldnāt change from previous state.
In any case, in practice I really donāt mind the ādesiredā use case Iāve just recalled, as the real use case is the next:
I had a motion sensor for my alarm system in several and different locations. The problem is that some motion sensors sometimes give false positives. So, Iām replicating the motion sensors, and only if the two sensors in the same location are activated at the same time (not necessarily triggered at the exact same time, but active at the same time), the main binary_sensor used for the alarm system is activated. As both motion sensors are located at the same location, I mind when one of the sensor goes from off to on while the other doesnāt (false positive), but I donāt mind when one of the sensor goes from on to off while the other remains in the on state, as this is a very short state until the other also goes from on to off (true positive).
In your code cases 2 and 3 distinguish which motion sensor is on and which is off (which does not really mind as both are equivalent), but donāt take into account the previous state of the system.
Said in a different way, the system I tried to make had memory, and cannot be done with a code which has no memory. That memory can be implemented with a variable (the number Kevin talked about or the from-to trigger in automations).