Trigger condition per actionable entity

Trying to use as little automations as possible to keep the overview about my system I ask myself if it is possible to check per actionable entity, if a condition specific to this entity evaluates to true, prior to performing said action.
One example I have is to lower the shutters on the westside partly, in the afternoon to prevent heating up of room temperature. This is mainly triggered by the suns azimuth crossing a certain value. I have other triggers such as the temperature, but lets keep it as simple as possible for this purpose.

To only close the shutters partly, I call cover.set_cover_position with a given value.
Now it could be that any of the shutters are already fully closed (Position 0), due to somebody sleeping or has been manually set to a position lower than the automation value. In which case these specific shutters should not be triggered by that automation.

Automation
- alias: Shutters - Close Shutters Partly to keep Heat out
  id: 'shutters_close_shutters_partly_to_keep_heat_out'
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'azimuth') }}"
    above: 200  # sun shines at the west side of the house
  
  condition:
  - condition: template
    value_template: >
      {{ (state_attr('cover.bedroom', 'current_position') | float) > 40 or
         (state_attr('cover.kids_room', 'current_position') | float) > 40 }}

  action:
  - service: cover.set_cover_position
    data:
      entity_id:
        - cover.bedroom
        - cover.kids_room
      position: 40

Is there a solution to this that will prevent me from creating one automation per shutter?

There are multiple ways in which you can combine such automations. The simplest way is to use the choose type action. It will look like this.

alias: Shutters - Close Shutters Partly to keep Heat out
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'azimuth') }}"
    above: 200  # sun shines at the west side of the house
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ (state_attr('cover.bedroom', 'current_position') | float) > 40 }}
        sequence:
          - service: cover.set_cover_position
            data:
              entity_id: cover.bedroom
              position: 40
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ (state_attr('cover.kids_room', 'current_position') | float) > 40 }}
        sequence:
          - service: cover.set_cover_position
            data:
              entity_id: cover.kids_room
              position: 40
    default: []

There are also other methods which involve using templates but its a bit complicated. This should work for you.

1 Like

Thanks for your help.
My understanding of ‘choose’ is that the automation stops with the first option that evaluates to true, or will it trigger both actions, if both conditions evaluate to true?

I messed up the yaml first time now I have corrected the yaml.

Yes you are right but instead of giving the second condition as an option of the first choose action, we should create a second choose action for the second condition. So it wont quite. Please check the updated yaml.

Ah perfect. Sometimes things can be so easy. Once you see it, it becomes almost obvious. Thanks again.

1 Like