Template sensor: multiple and AND or conditions

Hi everybody,

I have an automation (code below) to control my rain water pump and connected valves in order to irrigate my garden. I thought it’d work the way I have it (it used to), but when adding code to it, it must have broken. However, I decided to change things up and create a template sensor as a condition.

Conditions

  • input_boolean.bewasserung_ein must be on (I set this manually; in the future, it will be automated depending on whether or not there is a sufficient amount of water in the rain water bins)
  • sensor.wetter_sensor_rain must be not raining
  • sensor.wetter_sensor_condition must not contain Regen (I am not sure what states can be set for this sensor; it is currently not actually raining, but the sensor states light rain Leichter Regen; I assume there is also Starker Regen etc., so hopefully the automation should work if the state for this sensor just does not contain the word Regen alltogether)
  • sensor.season must be either summer or spring

If possible, I’d check all these conditions with a single template sensor, but I do not know how. This way, I could use this single template sensor as condition instead of having all these conditions in multiple automations (at least two, an hour after sunrise and half an hour before sunset).

This is my current automation, which I must have broken when adding things (the switch.valve_<n> entities; it worked fine before).

automation:
  - alias: "[Timer] Bewässerung Morgens"
    trigger:
      platform: sun
      event: sunrise
      offset: "+00:30:00"
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: sensor.wetter_sensor_rain
          state: "not raining"
        - condition: or
          conditions:
            - condition: state
              entity_id: sensor.season
              state: "spring"
            - condition: state
              entity_id: sensor.season
              state: "summer"
    action:
      - service: switch.turn_off
        entity_id: switch.valve_01, switch.valve_02, switch.valve_03
      - service: switch.turn_on
        entity_id: switch.dr_wasser
      - delay: "00:00:02"
      - service: switch.turn_on
        entity_id: switch.valve_01
      - delay: "{{ states('input_datetime.dauer_bewaesserung_morgens') }}"
      - service: switch.turn_off
        entity_id: switch.valve_01
      - service: switch.turn_on
        entity_id: switch.valve_02
      - delay: "{{ states('input_datetime.dauer_bewaesserung_morgens') }}"
      - service: switch.turn_off
        entity_id: switch.dr_wasser, switch.valve_01, switch.valve_02, switch.valve_03

Instead, I’d like something like this

automation:
  - alias: "[Timer] Bewässerung Morgens"
    trigger:
      platform: sun
      event: sunrise
      offset: "+00:30:00"
    condition:
      - condition: state
        entity_id: <the_template_sensor_with_all_the_stuff_from_above>
        state: true
    action: 
      # ...

How can I combine all those conditions above into one template sensor? When I do something like this {{ is_state("sensor.season", "summer") or is_state("sensor.season", "spring") }}, I’ll get true, but I don’t know how to do and and or conditions etc.

Thank you for your ideas :slight_smile:

Hey there,

you can use {% if %} in the template :slight_smile:
So, this might do it:

# Determine when the washing machine has a load running.
binary_sensor:
  - platform: template
    sensors:
      let_it_rain:
        friendly_name: "Regen_Bedingungen"
        value_template: >-
          {% if is_state('input_boolean.bewasserung_ein', 'on') and is_state('sensor.wetter_sensor_rain', 'not raining') %}
            {% if is_state('sensor.season', 'spring') or is_state('sensor.season', 'summer') %}
              {% if states('sensor.wetter_sensor_condition')|regex_search("regen", ignorecase=True) %}
                True
              {% endif %}
            {% endif %}
          {% endif %}
2 Likes

here is the template that should work for the conditions you listed:

{{ states('input_boolean.bewasserung_ein') == 'on' and states('sensor.wetter_sensor_rain') != 'raining' and ('Regen' not in states('sensor.wetter_sensor_condition')) and ('summer' in states('sensor.season') or 'spring' in states('sensor.season')) }}

the entities and states are taken directly from your post above. If those aren’t right then you will need to adjust accordingly.

to make it more readable you can put them on separate lines but make sure you use the multi-line indicator in the code:

value_template: >
  {{ states('input_boolean.bewasserung_ein') == 'on' 
    and states('sensor.wetter_sensor_rain') != 'raining' 
    and ('Regen' not in states('sensor.wetter_sensor_condition')) 
    and ('summer' in states('sensor.season') or 'spring' in states('sensor.season')) 
    }}

you could also change the last line to just use the state of the sensor:

{{ states('input_boolean.bewasserung_ein') == 'on' 
  and states('sensor.wetter_sensor_rain') != 'raining' 
  and ('Regen' not in states('sensor.wetter_sensor_condition')) 
  and (states('sensor.season') == 'summer' or states('sensor.season') == 'spring') 
  }}
2 Likes

The solution of @finity is way more elegant, didn’t know about the x in y part, nice one :slight_smile:

2 Likes

input_boolean.bewasserung_ein must be on

{{ is_state('input_boolean.bewasserung_ein', 'on') }}

sensor.wetter_sensor_rain must be not raining

{{ not is_state('sensor.wetter_sensor_rain', 'raining') }}

sensor.wetter_sensor_condition must not contain Regen

{{ 'Regen' not in states('sensor.wetter_sensor_condition') }}

sensor.season must be either summer or spring

{{ states('sensor.season') in ['summer', 'spring'] }}

Combined:

{{ is_state('input_boolean.bewasserung_ein', 'on')
  and not is_state('sensor.wetter_sensor_rain', 'raining')
  and 'Regen' not in states('sensor.wetter_sensor_condition')
  and states('sensor.season') in ['summer', 'spring'] }}
6 Likes

Thanks for the help, everybody :slight_smile: