How to template a binary sensor that reflects the states of 2 binary sensors?

I’m trying to template a binary sensor to reflect the states of either of two input sensors. So, if either of the two sensors is on, the new sensor will be on. And, if either turns off, the new sensor will be off. The following template seems to work for only 1 of the two. Can anyone offer suggestions to make it work for both?

{{ 'on' if states('input_boolean.c425_motion' or 'input_boolean.c425_person', 'state') == 'on' else 'off' }}

This also does not work:

{{ 'on' if states('input_boolean.c425_motion', 'state') or states('input_boolean.c425_person', 'state') == 'on' else 'off' }}

and neither does this:

{{ 'on' if (states('input_boolean.c425_motion', 'state') or states('input_boolean.c425_person', 'state')) == 'on' else 'off' }}

You don’t need to include the “on” and “off” values for the new sensor… a template binary sensor will be “on” when it’s template resolves to True, yes, on, enable or a positive number. If it resolves to anything else it’s state will be “off”

The basic OR is:

  • If either is “on”, the new sensor would be “on”.
  • If both are “off”, the new sensor would be “off”
{{ is_state('input_boolean.c425_motion', 'on') 
or is_state('input_boolean.c425_person', 'on') }}

That’s not a basic OR… are you sure that’s the correct logic?

What you have described will need triggers because you need to track the “on to off” and “off to on” events.

Yeah definitely triggers for that event driven logic.

template:
  - trigger:
      - id: 'on'
        trigger: state
        entity_id: 
          - input_boolean.c425_motion
          - input_boolean.c425_person
        from: 'off'
        to: 'on'
      - id: 'off'
        trigger: state
        entity_id: 
          - input_boolean.c425_motion
          - input_boolean.c425_person
        from: 'on'
        to: 'off'
    binary_sensor:
      - name: Combined sensor
        state: "{{ trigger.id }}

I’m not understanding.

Suppose the two are both “on”. One switches to “off”. The statement

means that the new sensor will be off. But the resultant state of both sensors is

which means

So what is the desired state of the new sensor when both sensors are on and one switches off?

It would be off.

X = don’t care
↓ 0 = change to off
↑1 = change to on

Sensor1 Sensor2 Out
X ↓ 0 0
↓ 0 X 0
X ↑1 1
↑1 X 1

The output simply reflects the last input change, no matter which input sensor changes.

So the statement

Should be

if either of the two sensors is turned on, the new sensor will be on.

Yeah. At least that’s how I interpret their requirements, given the other statements.

Or he could just use a group helper…?

Not for that logic.

1 Like

Sorry everyone. Bad logic. Yeah, it was supposed to be if either sensor is on, then the new sensor is on.

1 Like

If that’s the case you don’t need an automation. Use a group helper.