How to use or in Template

I am trying to learn more about templates and using “or”. Trying to determine if rain probability is over 60% in next 2 days. I can do it with elif, but would like to understand how to write it with “or”

{% if states('sensor.dark_sky_precip_probability_0d') |int > 60 %}
         true
{% elif states('sensor.dark_sky_precip_probability_1d') |int > 60 %}
         true
        {% else %}
         false
        {% endif %}
      {% if states('sensor.dark_sky_precip_probability_0d') | int > 60 or states('sensor.dark_sky_precip_probability_1d') | int > 60 %}
        true
      {% else %}
        false
      {% endif %}

Thanks, that was too simple. I kept putting too many brackets.

Please mark as solved, it helps others find similar especially ones with solutions

1 Like

There’s a simpler form. Try this in the template editor. It should give true/false as well:

{{ states('sensor.dark_sky_precip_probability_0d')|int > 60 or states('sensor.dark_sky_precip_probability_1d')|int > 60 }}
1 Like

As is usual Tom is correct, though that was goint to be a lesson for another day.
If you can get your states to do the work for you everything comes out shorter eg
{{ (true or not true) and not (true or false) }} = false
It’s logical really :rofl: :rofl: :rofl:

I know of three ways to create a template to produce a true/false result. Tom’s is one of them and, frankly, the one I prefer because it is the most streamlined of the three.

There’s this one which states all the steps (very legible):

{% if states('sensor.whatever') | int > 60 %}
  true
{% else %}
  false
{% endif %}

Here’s the inline version of it (more compact):

{{ 'true' if states('sensor.whatever') | int > 60 else 'false' }}

Here’s the most compact of all because there’s no need to explicitly indicate what constitutes true or false. If the state of sensor.whatever is greater than 60 then the template reports true otherwise it will report false.

{{ states('sensor.whatever') | int > 60 }}

Where is the ‘or’ in that ?

Edit: ha ha ha ! :rofl:

… discarded for clarity in order to focus on the three different techniques . Even the sensor’s name was made generic.

Thanks everyone. New to HA and trying to get a better grasp on the coding structure.

Trying to understand why % is required in your first example but not the second?

From the documentation Building Templates:

Templating in Home Assistant is powered by the Jinja2 templating engine.

So this {{ }} and this {% %} have special meaning in Jinja2.

In a nutshell, this:

{%     %}

is used for expressions or logic whereas this:

{{     }}

is used for rendering a result.

Here’s a very simple example:

{% set x = 'cat' %}
The {{ x }} is in the house.

The result of these two lines is:
The cat is in the house.

Here’s another example:

{% set x = 10 %}
{% set y = 20 %}
{% if x == y %}
  {{ x }} is the same as {{ y }}
{% else %}
  {{ x }} is not the same as {{ y }}
{% endif %}

The result of all of this is:
10 is not the same as 20

One more (a very contrived example):

{% if states('switch.kitchen') == 'on' %}
  {% set x = 'bright' %}
{% else %}
  {% set x = 'dark' %}
{% endif %}
The kitchen is {{ x }}.

If the current state of switch.kitchen is off then the result would be:
The kitchen is dark.

Here’s the same example in inline format:

{% set x = 'bright' if states('switch.kitchen') == 'on'  else 'dark' %}
The kitchen is {{ x }}.

If we apply everything we’ve learned so far, we get this:

The kitchen is {{'bright' if states('switch.kitchen') == 'on'  else 'dark'}}.
2 Likes

Thank you for the great examples