Setting template binary_sensor to unavailable

I want to combine the states of 2 binary_sensors into a single binary_sensor using a template binary_sensor.
The template sensor should be ON in case any of the 2 sensors are ON, OFF in case both are OFF.

In case any of the 2 sensors is unavailable/unknown, the template should become unavailable.
The template below seems to work fine for combining the states, except that the template does not render as unavailable in case the binary_sensors are unavailable.

template:
  - binary_sensor:
      - name: "Combined"
        state: >
          {% if is_state('binary_sensor.sensor_a', 'on') or is_state('binary_sensor.sensor_b', 'on') %}
            on
          {% elif is_state('binary_sensor.sensor_a', 'off') and is_state('binary_sensor.sensor_b', 'off') %}
            off
          {% else %}
            unavailable
          {% endif %}

I also tried defining a availability_template for this sensor, but this throws an error:

Invalid config for [template]: [availability_template] is an invalid option for [template].

Could anyone point out what I’m doing wrong here?

Your else will never execute unless both sensors are unavailable.

If they’re not both unavailable one of them will be either on or off, so the first or second clauses will always execute.

Just change the order of evaluation: make the first check to check for either being unavailable and the else becomes the on state.

@parautenbach Fair enough, I didn’t consider this when writing the template.
Here’s a modified version:

template:
  - binary_sensor:
      - name: "Combined"
        state: >
          {% if is_state('binary_sensor.sensor_a', 'unavailable') or is_state('binary_sensor.sensor_b', 'unavailable') %}
            unavailable
          {% elif is_state('binary_sensor.sensor_a', 'unknown') or is_state('binary_sensor.sensor_b', 'unknown') %}
            unavailable
          {% elif is_state('binary_sensor.sensor_a', 'on') or is_state('binary_sensor.sensor_b', 'on') %}
            on
          {% else %}
            off
          {% endif %}

Still, the unavailable state is never shown, it renders as OFF instead.

What does this in your template editor show when you’re testing the unavailable condition?

{{ states('binary_sensor.sensor_a') }}
{{ states('binary_sensor.sensor_b') }}

@parautenbach See screenshot:

Could you try this?

template:
  - binary_sensor:
      - name: "Combined"
        state: >
          {{ is_state('binary_sensor.sensor_a', 'on') or is_state('binary_sensor.sensor_b', 'on') }}
        availability: >
          {{ not is_state('binary_sensor.sensor_a', 'unavailable') or not is_state('binary_sensor.sensor_b', 'unavailable') }}

I think the issue is that returning unavailable isn’t doing what you’re expecting it to do:

The sensor is on if the template evaluates as True , yes , on , enable or a positive number. Any other value will render it as off . The actual appearance in the frontend (Open /Closed , Detected /Clear etc) depends on the sensor’s device_class value

1 Like

That did the job!
Thanks a lot!

Here’s my working code:

template:
  - binary_sensor:
      - name: "Combined"
        state: >
          {% if is_state('binary_sensor.sensor_a', 'on') or is_state('binary_sensor.sensor_b', 'on') %}
            on
          {% else %}
            off
          {% endif %}
        availability: >
          {% if is_state('binary_sensor.sensor_a', 'unavailable') or is_state('binary_sensor.sensor_a', 'unknown') %}
            False
          {% elif is_state('binary_sensor.sensor_b', 'unavailable') or is_state('binary_sensor.sensor_b', 'unknown') %}
            False
          {% else %}
            True
          {% endif %}
2 Likes

Use an availability template. That’s what it is for.

template:
  - binary_sensor:
      - name: "Combined"
        state: >
          {% if is_state('binary_sensor.sensor_a', 'on') or is_state('binary_sensor.sensor_b', 'on') %}
            on
          {% elif is_state('binary_sensor.sensor_a', 'off') and is_state('binary_sensor.sensor_b', 'off') %}
            off
          {% else %}
            unavailable
          {% endif %}
        availability: "{{ not ( is_state('binary_sensor.sensor_a', 'unavailable') or is_state('binary_sensor.sensor_b', 'unavailable') ) }}"

You realise you just described the default group behaviour?

Why not use a group?

I wish, but a group doesn’t go to unavailable when a sensor becomes unavailable.
Instead it just considers the state of the other :confused:

Then use the availability option with your template sensor.

@tom_l can you give me a hint why is_state returns false?

Because you spelled “binary_sensor” wrong and it looks like the template editor may be interpreting the results:

Please don’t tag me demanding an answer.

2 Likes