Shorten cover/window automation?

hi,

i got an automation that opens the cover of a window when i open the window.

since i have 9 windows it´s a quite long automation since i have set it for every windowsensor/cover.

just wanted to know if there is a way to make it a shorter automation. any ideas?

current automation:

alias: Cover
description: open Cover when Window is open
trigger:
  - platform: state
    entity_id:
      - binary_sensor.window_hallway_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_bedroom_1_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_bedroom_2_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_office_1_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_office_2_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_livingroom_1_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_livingroom_2_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_livingroom_3_contact
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.door_balcony_contact
    from: "off"
    to: "on"
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.window_office_1_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.pc_l
  - if:
      - condition: state
        entity_id: binary_sensor.window_office_2_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.pc_r
  - if:
      - condition: state
        entity_id: binary_sensor.window_bedroom_1_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.schlaf_l
  - if:
      - condition: state
        entity_id: binary_sensor.window_bedroom_2_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.schlaf_r
  - if:
      - condition: state
        entity_id: binary_sensor.window_hallway_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.gang
  - if:
      - condition: state
        entity_id: binary_sensor.window_livingroom_1_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.wz_1
  - if:
      - condition: state
        entity_id: binary_sensor.window_livingroom_2_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.wz_2
  - if:
      - condition: state
        entity_id: binary_sensor.window_livingroom_3_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.wz_3
  - if:
      - condition: state
        entity_id: binary_sensor.door_balcony_contact
        state: "on"
    then:
      - service: cover.set_cover_position
        data:
          position: 100
        target:
          entity_id: cover.balkon
mode: single

alias: Cover
description: open Cover when Window is open
trigger:
  - platform: state
    entity_id:
      - binary_sensor.window_hallway_contact
      - binary_sensor.window_bedroom_1_contact
      - binary_sensor.window_bedroom_2_contact
      - binary_sensor.window_office_1_contact
      - binary_sensor.window_office_2_contact
      - binary_sensor.window_livingroom_1_contact
      - binary_sensor.window_livingroom_2_contact
      - binary_sensor.window_livingroom_3_contact
      - binary_sensor.door_balcony_contact
    from: "off"
    to: "on"
condition: []
action:
  - variables:
      covers:
        window_hallway_contact: gang
        window_bedroom_1_contact: schlaf_l
        window_bedroom_2_contact: schlaf_r
        window_office_1_contact: pc_l
        window_office_2_contact: pc_r
        window_livingroom_1_contact: wz_1
        window_livingroom_2_contact: wz_2
        window_livingroom_3_contact: wz_3
        door_balcony_contact: balkon
  - service: cover.set_cover_position
    data:
      position: 100
    target:
      entity_id: "cover.{{ covers[trigger.to_state.object_id] }}"
mode: single

NOTE

Because this automation employs trigger.to_state.object_id in its action, you cannot test this automation using its Run command. The trigger variable is defined only when this automation is actually triggered by its State Trigger (and the Run command doesn’t do that, it just executes the actions).

1 Like

that´s what i was looking for. thanks alot!

1 Like

If you ever need to specify each cover’s position individually (as opposed to all set to 100) then you can do it like this:

alias: Cover
description: open Cover when Window is open
trigger:
  - platform: state
    entity_id:
      - binary_sensor.window_hallway_contact
      - binary_sensor.window_bedroom_1_contact
      - binary_sensor.window_bedroom_2_contact
      - binary_sensor.window_office_1_contact
      - binary_sensor.window_office_2_contact
      - binary_sensor.window_livingroom_1_contact
      - binary_sensor.window_livingroom_2_contact
      - binary_sensor.window_livingroom_3_contact
      - binary_sensor.door_balcony_contact
    from: "off"
    to: "on"
condition: []
action:
  - variables:
      covers:
        window_hallway_contact:
          name: gang
          posn: 50
        window_bedroom_1_contact:
          name: schlaf_l
          posn: 50
        window_bedroom_2_contact:
          name: schlaf_r
          posn: 100
        window_office_1_contact:
          name: pc_l
          posn: 75
        window_office_2_contact:
          name: pc_r
          posn: 100
        window_livingroom_1_contact:
          name: wz_1
          posn: 25
        window_livingroom_2_contact:
          name: wz_2
          posn: 25
        window_livingroom_3_contact:
          name: wz_3
          posn: 25
        door_balcony_contact:
          name: balkon
          posn: 100
  - service: cover.set_cover_position
    data:
      position: "{{ covers[trigger.to_state.object_id]['posn'] }}"
    target:
      entity_id: "cover.{{ covers[trigger.to_state.object_id]['name'] }}"
mode: single

Or like this:

alias: Cover
description: open Cover when Window is open
trigger:
  - platform: state
    entity_id:
      - binary_sensor.window_hallway_contact
      - binary_sensor.window_bedroom_1_contact
      - binary_sensor.window_bedroom_2_contact
      - binary_sensor.window_office_1_contact
      - binary_sensor.window_office_2_contact
      - binary_sensor.window_livingroom_1_contact
      - binary_sensor.window_livingroom_2_contact
      - binary_sensor.window_livingroom_3_contact
      - binary_sensor.door_balcony_contact
    from: "off"
    to: "on"
condition: []
action:
  - variables:
      covers:
        window_hallway_contact:
          - gang
          - 50
        window_bedroom_1_contact:
          - schlaf_l
          - 50
        window_bedroom_2_contact:
          - schlaf_r
          - 100
        window_office_1_contact:
          - pc_l
          - 75
        window_office_2_contact:
          - pc_r
          - 100
        window_livingroom_1_contact:
          - wz_1
          - 25
        window_livingroom_2_contact:
          - wz_2
          - 25
        window_livingroom_3_contact:
          - wz_3
          - 25
        door_balcony_contact:
          - balkon
          - 100
  - service: cover.set_cover_position
    data:
      position: "{{ covers[trigger.to_state.object_id][1] }}"
    target:
      entity_id: "cover.{{ covers[trigger.to_state.object_id][0] }}"
mode: single
1 Like

that‘s nice to know. i can change a few of my automations now and make them shorter :slightly_smiling_face: