Choose condition OR/AND

I hope someone can help me some logical thinking.
I am building an automation to open a bathroom window based on the humidity. Now that part is easy. However I want the action to be depending on two variables, outside temperature and windforce.
So if temp < 5C or wind > 7bft the window will be opened only 25%. If temp is 5-10C and wind 5-7bft the window will be opened for 50%… till temp >15C and wind <3bft.
I did think of creating a Choose - Condition in the Action section.
But now the question: What will be the result when the temp <5C and the wind <3bt

Any help is appreciated a lot.

  - choose:
        - condition: or
          conditions:
            - condition: numeric_state
              entity_id: sensor.buienradar_temperature
              below: 5
            - condition: numeric_state
              entity_id: sensor.buienradar_wind_force
              above: 7        
	      sequence:
                - service: cover.set_cover_position
                  data:
                    entity_id: cover.axa_test
                    position: 25
        - condition: or
          conditions:
            - condition: numeric_state
              entity_id: sensor.buienradar_temperature
              above: 5
              below: 10
            - condition: numeric_state
              entity_id: sensor.buienradar_wind_force
              above: 5
              below: 7        
	      sequence:
                - service: cover.set_cover_position
                  data:
                    entity_id: cover.axa_test
                    position: 50
        - condition: or
          conditions:
            - condition: numeric_state
              entity_id: sensor.buienradar_temperature
              above: 10
              below: 15
            - condition: numeric_state
              entity_id: sensor.buienradar_wind_force
              above: 3
              below: 5        
	      sequence:
                - service: cover.set_cover_position
                  data:
                    entity_id: cover.axa_test
                    position: 75
        - condition: or
          conditions:
            - condition: numeric_state
              entity_id: sensor.buienradar_temperature
              above: 15
            - condition: numeric_state
              entity_id: sensor.buienradar_wind_force
              below: 3        
	      sequence:
                - service: cover.set_cover_position
                  data:
                    entity_id: cover.axa_test
                    position: 100

Do you want to logically OR or AND the two conditions? In other words, the temperature OR wind or temperature AND wind are within a certain range?

It’s unnecessary to employ choose for this application. You simply need to template the value of position. In the following example, I have used a logical AND. However it’s easy to change it if you actually want a logical OR.
(just change and to or in the template).

- service: cover.set_cover_position
  target:
    entity_id: cover.axa_test
  data:
    position: >
      {% set t = states('sensor.buienradar_temperature') | float(0) %}
      {% set w = states('sensor.buienradar_wind_force') | float(0) %}
      {% if t < 5 and w > 7 %} 25
      {% elif 10 < t < 15 and 5 < w < 7 %} 50
      {% elif 10 < t < 15 and 3 < w < 5 %} 75
      {% elif t > 15 and w < 3 %} 100
      {% else %} state_attr('cover.axa_test', 'current_position')
      {% endif %}

Version with logical OR.

- service: cover.set_cover_position
  target:
    entity_id: cover.axa_test
  data:
    position: >
      {% set t = states('sensor.buienradar_temperature') | float(0) %}
      {% set w = states('sensor.buienradar_wind_force') | float(0) %}
      {% if t < 5 or w > 7 %} 25
      {% elif 10 < t < 15 or 5 < w < 7 %} 50
      {% elif 10 < t < 15 or 3 < w < 5 %} 75
      {% elif t > 15 or w < 3 %} 100
      {% else %} state_attr('cover.axa_test', 'current_position')
      {% endif %}

For the example I posted, that combination will be handled by the final {% else %} and the window will be left in its current position. If you prefer, you can easily change it to 0 in order to close the window.

Hi @123 ,

Thanks for helping in this direction. Besides the more compact code it looks great when I change the operators to OF. Since I want the window to open only 25% when temp is very low or the wind is powerful.
Actually my question behind the question was if a condition is met it breaks out instead of testing the other conditions.

    data:
      position: >
        {% set t = states('sensor.buienradar_temperature') | float(0) %}
        {% set w = states('sensor.buienradar_wind_force') | float(0) %}
        {% if t < 5 or w > 7 %} 25
        {% elif 10 < t < 15 or 5 < w < 7 %} 50
        {% elif 10 < t < 15 or 3 < w < 5 %} 75
        {% elif t > 15 or w < 3 %} 100
        {% else %} state_attr('cover.axa_test', 'current_position')
        {% endif %}

So it now looks like this and seems to work at the first tests. I will do more tests the coming days and then come back with the results

That’s precisely how a choose (or a if-elif chain) works; it exits at the first condition that evaluates to true.

That’s clear now. I found the description in the first post less clear because it gave examples using both or as well as and.

1 Like

Well after some extensive testing this is how the solution looks like to automate the bathroom window. Luckily the weather conditions are quite fluctuating at the moment so I was able to test in real life the behavior.
Just posting this example here so hopefully others can use it as ideas. The condition around uptime has to do with the calibration that the ESP8266 is doing on the window at boot time. Since the only position sensor in the window opener is the “hard locked” sensor, the ESP8266 will first open and close the window till fully closed and the hard locked sensor reports TRUE. This procedure takes about 60 sec in total.
And again thanks for your input @123 Taras

- id: cover_axa_bathroom_1_open
  alias: Window 1 Bathroom Open
  trigger:
  - platform: numeric_state
    entity_id: sensor.axa_test_humidity
    above: 90
  condition:
    condition: and
    conditions:
    - condition: time
      after: '06:00:00'
      before: '23:55:00'
    - condition: numeric_state
      entity_id: sensor.axa_test_uptime
      above: 100
  action:
  - variables:
      pos: >
        {% set t = states('sensor.buienradar_temperature') | float(0) %}
        {% set w = states('sensor.buienradar_wind_speed') | float(0) %}
        {% if 0 < t < 5 or 49 < w < 74.1 %} 25
        {% elif 4.9 < t < 10 or 38 < w < 49.1 %} 50
        {% elif 9.9 < t < 15 or 19 < w < 38.1 %} 75
        {% elif t > 14.9 or w < 19.1 %} 100
        {% else %} 15
        {% endif %}
  - service: cover.set_cover_position
    target:
      entity_id: cover.axa_test
    data:
      position: '{{pos}}'

You marked your own post with the Solution tag. It’s the custom of this community forum to assign the tag to the first post that resolves the problem. That would be my reply to you containing the template.

If everyone marked their own posts with the tag, it would give the impression that everyone ultimately solves their own problems.

FAQ Guideline 21