Common condition in three out of five choose options

Hi *,

I do have an automation with 5 choose blocks (automating my covers). When I close the covers (3 choose blocks) I check for windspeeds, when I open the cover (2 choose blocks) I don’t care about windspeeds.

Now I’m looking for an elegant way to not repeat the wind speed check three times in my automation. Setting this in the list of conditions before the actions block does not work, as the conditions should only be checked in those three blocks where the covers are closed.

Appreciate any help.

Post the complete YAML, correctly formatted.

I expect we can work around that with an or condition. Show the code and we’ll see.

OK, here we go. My code :see_no_evil:

alias: Haus.Markise.Automation
description: ""
trigger:
  - platform: sun
    event: sunset
    enabled: true
    id: Sonnenuntergang
    alias: Sonnenuntergang - Markisen Oben Schliessen
  - alias: "SuedOst - Runterfahren "
    platform: time
    at: "07:15:00"
    id: SuedOst - Runterfahren
  - alias: SuedOst - Hochfahren
    platform: time
    at: "14:00:00"
    id: SuedOst -  Hochfahren
  - alias: SuedWest - Runterfahren
    platform: time
    at: "11:30:00"
    id: SuedWest - Runterfahren
  - platform: time
    at: "06:30:00"
    alias: Werktags 6:30 - Alles Hochfahren
    id: Werktags 6:30 - Alles Hochfahren
  - platform: time
    at: "09:00:00"
    id: Wochenende/Feiertag - 9:00 - Alles Hochfahren
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - Sonnenuntergang
                enabled: true
              - alias: "Nur zwischen April und September "
                condition: template
                value_template: "{{ now().month in [4,5,6,7,8,9] }}"
                enabled: true
              - condition: numeric_state
                entity_id: sensor.openweathermap_wind_speed
                below: 20
                enabled: true
              - condition: numeric_state
                entity_id: sensor.openweathermap_forecast_wind_speed
                below: 20
                enabled: true
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id: cover.oben
            enabled: true
        alias: "Sonnenuntergang: Markisen Oben Schliessen"
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - SuedOst - Runterfahren
              - condition: numeric_state
                entity_id: sensor.temperature_max
                above: 20
              - alias: "Nur zwischen April und September "
                condition: template
                value_template: "{{ now().month in [4,5,6,7,8,9] }}"
              - condition: numeric_state
                entity_id: sensor.openweathermap_wind_speed
                below: 20
              - condition: numeric_state
                entity_id: sensor.openweathermap_forecast_wind_speed
                below: 20
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.hwr
                - cover.gastezimmer_nachbarin
        alias: SuedOst - Runterfahren
      - conditions:
          - condition: trigger
            id:
              - SuedOst -  Hochfahren
        sequence:
          - service: cover.open_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.hwr
                - cover.gastezimmer_nachbarin
        alias: SuedOst - Hochfahren
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - SuedWest - Runterfahren
              - condition: numeric_state
                entity_id: sensor.temperature_max
                above: 20
              - alias: "Nur zwischen April und September "
                condition: template
                value_template: "{{ now().month in [4,5,6,7,8,9] }}"
              - condition: numeric_state
                entity_id: sensor.openweathermap_wind_speed
                below: 20
              - condition: numeric_state
                entity_id: sensor.openweathermap_forecast_wind_speed
                below: 20
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.schlafzimmer
                - cover.arbeitszimmer_garten
        alias: SuedWest - Runterfahren
      - conditions:
          - condition: trigger
            id:
              - Werktags 6:30 - Alles Hochfahren
          - condition: state
            entity_id: binary_sensor.workday_sensor
            state: "on"
        sequence:
          - service: cover.open_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.oben
                - cover.unten
        alias: Werktags - 6:30 - Hochfahren
      - conditions:
          - condition: trigger
            id:
              - Wochenende/Feiertag - 9:00 - Alles Hochfahren
        sequence:
          - service: cover.open_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.wohnzimmer
                - cover.esszimmer
                - cover.kuche
                - cover.gastebad
                - cover.arbeitszimmer
                - cover.schlafzimmer
                - cover.badezimmer
                - cover.jalousien_flur
                - cover.gastezimmer_strasse
        alias: Sonntag / Feiertag - 9:00 - Alles Hochfahren
mode: single

I’d like to “aggregate” the following condition check. It appears three times in my automation.

condition: numeric_state
                entity_id: sensor.openweathermap_wind_speed
                below: 20
              - condition: numeric_state
                entity_id: sensor.openweathermap_forecast_wind_speed
                below: 20
alias: Haus.Markise.Automation
description: ""
trigger:
  - platform: sun
    event: sunset
    enabled: true
    id: Sonnenuntergang
    alias: Sonnenuntergang - Markisen Oben Schliessen
  - alias: "SuedOst - Runterfahren "
    platform: time
    at: "07:15:00"
    id: SuedOst - Runterfahren
  - alias: SuedOst - Hochfahren
    platform: time
    at: "14:00:00"
    id: SuedOst -  Hochfahren
  - alias: SuedWest - Runterfahren
    platform: time
    at: "11:30:00"
    id: SuedWest - Runterfahren
  - platform: time
    at: "06:30:00"
    alias: Werktags 6:30 - Alles Hochfahren
    id: Werktags 6:30 - Alles Hochfahren
  - platform: time
    at: "09:00:00"
    id: Wochenende/Feiertag - 9:00 - Alles Hochfahren
condition:
  - or:
    - and:
      - "{{ trigger.id in ('Sonnenuntergang','SuedOst - Runterfahren','SuedWest - Runterfahren') }}"
      - condition: numeric_state
        entity_id: sensor.openweathermap_wind_speed
        below: 20
      - condition: numeric_state
        entity_id: sensor.openweathermap_forecast_wind_speed
        below: 20
    - "{{ trigger.id not in ('Sonnenuntergang','SuedOst - Runterfahren','SuedWest - Runterfahren') }}"
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - Sonnenuntergang
                enabled: true
              - alias: "Nur zwischen April und September "
                condition: template
                value_template: "{{ now().month in [4,5,6,7,8,9] }}"
                enabled: true
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id: cover.oben
            enabled: true
        alias: "Sonnenuntergang: Markisen Oben Schliessen"
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - SuedOst - Runterfahren
              - condition: numeric_state
                entity_id: sensor.temperature_max
                above: 20
              - alias: "Nur zwischen April und September "
                condition: template
                value_template: "{{ now().month in [4,5,6,7,8,9] }}"
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.hwr
                - cover.gastezimmer_nachbarin
        alias: SuedOst - Runterfahren
      - conditions:
          - condition: trigger
            id:
              - SuedOst -  Hochfahren
        sequence:
          - service: cover.open_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.hwr
                - cover.gastezimmer_nachbarin
        alias: SuedOst - Hochfahren
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - SuedWest - Runterfahren
              - condition: numeric_state
                entity_id: sensor.temperature_max
                above: 20
              - alias: "Nur zwischen April und September "
                condition: template
                value_template: "{{ now().month in [4,5,6,7,8,9] }}"
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.schlafzimmer
                - cover.arbeitszimmer_garten
        alias: SuedWest - Runterfahren
      - conditions:
          - condition: trigger
            id:
              - Werktags 6:30 - Alles Hochfahren
          - condition: state
            entity_id: binary_sensor.workday_sensor
            state: "on"
        sequence:
          - service: cover.open_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.oben
                - cover.unten
        alias: Werktags - 6:30 - Hochfahren
      - conditions:
          - condition: trigger
            id:
              - Wochenende/Feiertag - 9:00 - Alles Hochfahren
        sequence:
          - service: cover.open_cover
            metadata: {}
            data: {}
            target:
              entity_id:
                - cover.wohnzimmer
                - cover.esszimmer
                - cover.kuche
                - cover.gastebad
                - cover.arbeitszimmer
                - cover.schlafzimmer
                - cover.badezimmer
                - cover.jalousien_flur
                - cover.gastezimmer_strasse
        alias: Sonntag / Feiertag - 9:00 - Alles Hochfahren
mode: single
1 Like

aargh - so easy

thanks for the eye opener :v:

Here’s a strategy that doesn’t use choose and puts all conditions directly in the trigger (using trigger variables) so that all information associated with the trigger is centralized and easier to manage.

Example
alias: Haus.Markise.Automation
description: ""
trigger:
  - platform: sun
    event: sunset
    id: Sonnenuntergang
    alias: Sonnenuntergang - Markisen Oben Schliessen
    variables:
      criteria: >
        {{ states('sensor.openweathermap_wind_speed') | float(0) < 20 and
          states('sensor.openweathermap_forecast_wind_speed') | float(0) < 20 and
          states('sensor.temperature_max') | float(0) > 20 and
          now().month in [4,5,6,7,8,9] }}
      command: "close"
      covers: cover.oben

  - alias: "SuedOst - Runterfahren "
    platform: time
    at: "07:15:00"
    id: SuedOst - Runterfahren
    variables:
      criteria: >
        {{ states('sensor.openweathermap_wind_speed') | float(0) < 20 and
          states('sensor.openweathermap_forecast_wind_speed') | float(0) < 20 and
          states('sensor.temperature_max') | float(0) > 20 and
          now().month in [4,5,6,7,8,9] }}
      command: "close"
      covers:
        - cover.hwr
        - cover.gastezimmer_nachbarin

  - alias: SuedOst - Hochfahren
    platform: time
    at: "14:00:00"
    id: SuedOst -  Hochfahren
    variables:
      criteria: "{{ true }}"
      command: "open"
      covers:
        - cover.hwr
        - cover.gastezimmer_nachbarin

  - alias: SuedWest - Runterfahren
    platform: time
    at: "11:30:00"
    id: SuedWest - Runterfahren
    variables:
      criteria: >
        {{ states('sensor.openweathermap_wind_speed') | float(0) < 20 and
          states('sensor.openweathermap_forecast_wind_speed') | float(0) < 20 and
          states('sensor.temperature_max') | float(0) > 20 and
          now().month in [4,5,6,7,8,9] }}
      command: "close"
      covers:
        - cover.schlafzimmer
        - cover.arbeitszimmer_garten

  - platform: time
    at: "06:30:00"
    alias: Werktags 6:30 - Alles Hochfahren
    id: Werktags 6:30 - Alles Hochfahren
    variables:
      criteria: "{{ is_state('binary_sensor.workday_sensor', 'on') }}"
      command: "open"
      covers:
        - cover.oben
        - cover.unten

  - platform: time
    at: "09:00:00"
    id: Wochenende/Feiertag - 9:00 - Alles Hochfahren
    variables:
      criteria: "{{ is_state('binary_sensor.workday_sensor', 'on') }}"
      command: "open"
      covers:
        - cover.wohnzimmer
        - cover.esszimmer
        - cover.kuche
        - cover.gastebad
        - cover.arbeitszimmer
        - cover.schlafzimmer
        - cover.badezimmer
        - cover.jalousien_flur
        - cover.gastezimmer_strasse
condition:
  - condition: template
    value_template: "{{ criteria }}"
action:
  - service: "cover.{{ command }}_cover"
    target:
      entity_id: "{{ covers }}"
mode: single

Each trigger has three variables:

  1. criteria represents the conditions that must be met in order to allow it to execute the action.
  2. command represents the action to perform (open or close the cover).
  3. covers represents the covers to be opened or closed.

Here’s the first trigger:

  - platform: sun
    event: sunset
    id: Sonnenuntergang
    alias: Sonnenuntergang - Markisen Oben Schliessen
    variables:
      criteria: >
        {{ states('sensor.openweathermap_wind_speed') | float(0) < 20 and
          states('sensor.openweathermap_forecast_wind_speed') | float(0) < 20 and
          states('sensor.temperature_max') | float(0) > 20 and
          now().month in [4,5,6,7,8,9] }}
      command: "close"
      covers: cover.oben

The criteria template for three of the triggers is identical and a bit long. If you wish, you can create a Template Binary Sensor (employing the template) and then simply refer to it in criteria.

    variables:
      criteria: "{{ is_state('binary_sensor.cover_conditions', 'on') }}"
      command: "close"
      covers: cover.oben

1 Like

I do learn a lot here. Thanks

But my problem is the same here, the windspeed occurs three times in the code. Today I realised 16km/h is a very low wind speed and I had to change the wind speed in all three condition to 20km/h. That brought me to my question. What is if I have to change it again?

Create the Template Binary Sensor I suggested (see screenshot above), then you will have just one place to adjust windspeed in its template.

If you employ the strategy I suggested, it will also help to simplify maintenance because a trigger and its conditions are effectively in the same place (listed in each trigger’s configuration), as opposed to using a choose where a trigger and its conditions are in separate places.

Anyways, it’s your choice, use whatever strategy serves you best. Personally, I have migrated away from using choose when all its choices ultimately perform the same action (like in your automation where each choice is either opening or closing a set of covers).

You could even create that sensor taking the windspeed from an Input Number helper allowing you to dynamically adjust it from the dashboard. State template for a template binary sensor just handling the windspeed conditions could be, for example:

{{ states('sensor.openweathermap_wind_speed') | float(0) <
   states('input_number.cover_max_wind')|float and
   states('sensor.openweathermap_forecast_wind_speed') | float(0) <
   states('input_number.cover_max_wind')|float }}

Solved: Reuse code with YAML Anchors and more.
A clean way to do this with your original code.