`or` condition not working in template

Consider the following templates:

{{ states('binary_sensor.test_sensor') }} #resolves to false
{{ states('input_boolean.test_boolean') }} #is set to true/on

Why does the following resolve to false?

{{ states('binary_sensor.test_sensor') or states('input_boolean.test_boolean') }}

I also notice that the template listens to only the first half of the condition (ie I can swap them around and it’s listens to the new input). If I replace the or with an and, then it listens to both. And of course Demorgan’s makes this work as required but I’d prefer to use a straight or!

Edit: Actually Demogan’s doesn’t work and the template only listens to the first half again!

You need to use is_state i.e.

{{is_state('sensor.garage_door_status','open')}}

Or at least tell the template what it’s actually trying to do i.e. :

{{is_state('media_player.sony_kd_55x85l_2','on') or states('sensor.roku_f1')|int(0) >= 1}}

In your example you are not telling the template what it’s actually looking for, other than “show me the state”

1 Like

It resolves to either on or off for a binary_sensor. Both values are strings, not booleans, and are not interpreted as booleans.

Therefore your template is effectively this:

{{ 'cat' or 'dog' }}

The result will be the first term, cat.

If you want to use logical operators, ensure they’re comparing boolean values (or a value that can be interpreted as a boolean).

1 Like

I got to the same place in anticipation of a reply:

{{ is_state('binary_sensor.test_sensor', 'on') or is_state('input_boolean.test_boolean', 'on') }}

Did the trick.

I’m always amused at how even half a decade of using HA this kind of stuff remains arbitrary to me :).

1 Like

Is it possible to create a binary sensor that outputs boolean values?

The state value of every entity is always a string.

Even if the value of a temperature sensor is numeric, it’s actually stored as a string.

You can convert strings on/off to boolean true/false using the bool filter.

{{ states('binary_sensor.test_sensor') | bool or states('input_boolean.test_boolean') | bool }}

Similarly, 1/0 and yes/no can be converted to true/false.

1 Like