Binary_sensor state based on two other binary_sensors

Hello,

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') }}"

Thank you!

This is not something I have done, but maybe a group can help.

2 Likes

Thank you!

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.

This is the new code:

group:
  movimiento_piso:
    entities:
      - binary_sensor.sonoff_a480019e03
      - binary_sensor.sonoff_a480028dba
    all: on

binary_sensor:
  - platform: template
    sensors:
      movimiento_piso:
        friendly_name: Sensor Movimiento piso
        device_class: motion
        value_template: "{{ is_state('group.movimiento_piso', 'on') }}"

Iā€™d use a state based template sensor with an and operator over the 2 states in its state template. Template - Home Assistant

See this example (itā€™s or instead of and): Template - Home Assistant

1 Like

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.

This is the code:

binary_sensor:
  - platform: template
    sensors:
      movimiento_piso:
        friendly_name: Sensor Movimiento piso
        device_class: motion
        value_template: >
          {% if is_state('binary_sensor.sonoff_a480019e03', 'on') and is_state('binary_sensor.sonoff_a480028dba', 'on') %}
            {{'on'}}
          {% elif is_state('binary_sensor.sonoff_a480019e03', 'off') and is_state('binary_sensor.sonoff_a480028dba', 'off') %}
            {{'off'}}
          {% endif %}

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.

1 Like

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.

1 Like

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).

alarma_movimiento_piso_off:
  friendly_name: "Alarma:Movimiento piso off"
  value_template: >-
    {% if states('binary_sensor.sonoff_a480019e03') == 'off' and  states('binary_sensor.sonoff_a480028dba') == 'off' %}
      Result1
    {% elif states('binary_sensor.sonoff_a480019e03') == 'off' and  states('binary_sensor.sonoff_a480028dba') == 'on' %}
      Result2
    {% elif states('binary_sensor.sonoff_a480019e03') == 'on' and  states('binary_sensor.sonoff_a480028dba') == 'off' %}
      Result3
    {% elif states('binary_sensor.sonoff_a480019e03') == 'on' and  states('binary_sensor.sonoff_a480028dba') == 'on' %}
      Result4
    {% endif %}

How about thisā€¦ Depending on the 2 switch conditions, the sensor can generate 4 different results.

1 Like

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).

This seems a solution to your requirements.
Just to understand, what is missing in this solution?

1 Like

Yes itā€™s solved.

Last messages were just comments to the answers and to the initial set up.

Thank you everybody for your help.