Question for all you clever templating people, what is wrong with the following:
{% if (is_state('binary_sensor.1', 'off')) or
(is_state('binary_sensor.2', 'off')) or
(is_state('binary_sensor.3', 'off')) and
(is_state('binary_sensor.4', 'on')) %}
true {% else %} false {% endif %}
All the above binary sensors are showing the state of off, however the above renders as true?
I am convinced its the mixing of OR & AND as if I remove the and it always renders correctly.
Since and binds first/strongest compared to or, your template (with all sensors ‘off’) boils down to:
TRUE or TRUE or TRUE and FALSE ->
TRUE or TRUE or FALSE ->
TRUE or FALSE ->
TRUE
If you need to change the order they are processed in, use parentheses to have all the or's processed first:
{{ ( is_state('binary_sensor.1', 'off') or
is_state('binary_sensor.2', 'off') or
is_state('binary_sensor.3', 'off') ) and
is_state('binary_sensor.4', 'on') }}
In your example case this would evaluate as follows:
(TRUE or TRUE or TRUE) and FALSE ->
(TRUE or TRUE) and FALSE ->
TRUE and FALSE ->
FALSE
EDIT: It isn’t necessary, in this case, to structure it as an if statement.
Perfect, thank you not only for the answer but the explanation as well. I would mark your post and the answer but dont seem to have the option as is often the case!