Template: and _and_ or value_template

Hi everybody,

I have a sensor checking multiple conditions:

          {{ 'Regen' not in states('sensor.wetter_sensor_condition')
              and states('sensor.season') in ['summer', 'spring']
              and is_state("input_select.pumpe_in_tonne", "Links")
              and (states("sensor.tonne_links") | int > 200)
              or is_state("input_select.pumpe_in_tonne", "Rechts")
              and (states("sensor.tonne_rechts") | int > 200)
            }}

This will currently return True, even though the state of sensor.wetter_sennsor_condition is Leichter Regen. I know that the value_template must be incorrect, but I don’t know how to fix it. What I want to check is

  • Regen must not be in sensor.wetter_sensor_condition
  • sensor.season must be either summer or spring

Then check those following lines like this

  • if the is_state is Links, check whether sensor.tonne_links | int > 200
  • otherwise if is_state is Rechts, check whether sensor.tinne_rechts | int > 200

Currently, the sensor will always return True as long as those values are > 200, I believe, but will not actually take the Regen part into consideration.

Thank you for your help.

I think you have a precedence issue with the “or” in your statement. Try this

{{ 'Regen' not in states('sensor.wetter_sensor_condition')
    and states('sensor.season') in ['summer', 'spring']
    and ((is_state("input_select.pumpe_in_tonne", "Links")
    and (states("sensor.tonne_links") | int > 200))
    or (is_state("input_select.pumpe_in_tonne", "Rechts")
    and (states("sensor.tonne_rechts") | int > 200))
 }}

When making complex things, its better to make things readable then to make them compact or 1 line.

{% if 'Regen' not in states('sensor.wetter_sensor_condition') and states('sensor.season') in ['summer', 'spring'] %}
  {{ is_state("input_select.pumpe_in_tonne", "Links") and states("sensor.tonne_links") | int > 200 }}
{% else %}
  {{ is_state("input_select.pumpe_in_tonne", "Rechts") and states("sensor.tonne_rechts") | int > 200 }}
{% endif %}

@micque
Thank you. Unfortunately, this will return Error rendering template: TemplateSyntaxError: unexpected '}', expected ')'. I was not able to figure out where the ) is missing, as it looks like it is in the second to last line (200)).

@petro Thank you. Your template will currently return False, even though right now it should be True (it is not raining and both values are above 200). I believe the if / else should be somewhere else, but then again, I always have trouble with templates… Both the ‘Regen’ and ‘summer’ / ‘spring’ part always has to be the case. It must not rain, and it must be either summer or spring. The other part of the template is if/else: if the pump is in the left water reservoir, the left value must be above 200 - but if it is in the right one, the corresponding right value must be above 200.

Yes the last line should be

and (states("sensor.tonne_rechts") | int > 200)))
 }}
1 Like

Still recommend breaking it up. I read the requirements a bit wrong so the logic was flawed.

{% set no_regen = 'Regen' not in states('sensor.wetter_sensor_condition') %}
{% set summer_or_spring = states('sensor.season') in ['summer', 'spring'] %}
{% set tonne_links = is_state("input_select.pumpe_in_tonne", "Links") and (states("sensor.tonne_links") | int > 200) %}
{% set tonne_rechts = is_state("input_select.pumpe_in_tonne", "Rechts") and (states("sensor.tonne_rechts") | int > 200) %}
{{ no_regen and summer_or_spring and (tonne_rechts or tonne_links) }}
1 Like

Thank you. This works perfectly.

I tried modifying this a bit, but it will still return True, though it should currently be False. What did I do wrong?

{% set no_regen = 'Regen' not in states('sensor.wetter_sensor_condition') %}
{% set bedeckt = 'Bedeckt' not in states('sensor.wetter_sensor_condition') %}
{% set summer_or_spring = states('sensor.season') in ['summer', 'spring'] %}
{% set tonne_links = is_state("input_select.pumpe_in_tonne", "Links") and (states("sensor.tonne_links") | int > 200) %}
{% set tonne_rechts = is_state("input_select.pumpe_in_tonne", "Rechts") and (states("sensor.tonne_rechts") | int > 200) %}
{{ (no_regen or bedeckt) and summer_or_spring and (tonne_rechts or tonne_links) }}

are you sure the state for sensor.wetter_sensor_condition is translated in your language? Everything in the template is correct syntactically. So the issue is in one of your entities.

I believe so. It does work when I use the Regen part of it (which is “rain”); Bedeckt means something like “partly cloudy”, but it is displayed in German.

Or do you mean that, while Home Assistant displays the value in German, I still have to use the English value?

you have to look at the states page and verify that the actual state is what you think it is.