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?
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 returningunavailable 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