Control shutters based on dynamic group

Hi all,
For controlling my shutters I use shelly 2PM’s. I have created helpers to group them according to my needs.

But I was not so happy with the automation especially in terms of centralised opening/closing of all shutters. I didn’t like the fact that with a central group/helper all the shellies switch even though some of the shutters may already be in the end position.

I have found a way to open only the shutters that are closed or close only those that are open using two scripts and two dynamic groups and would like to share it with you:

alias: Close all open shutters
sequence:
  - condition: template
    value_template: >-
      {{ expand('cover.shutters_zentral') | selectattr('state','equalto',
      'open') | map(attribute='entity_id') | list | length >= 1 }}
  - data_template:
      object_id: all_open_shutters
      entities: >
        {{ expand('cover.shutters_zentral') | selectattr('state','equalto',
        'open') | map(attribute='entity_id') | list  }}
    action: group.set
  - action: cover.close_cover
    metadata: {}
    data: {}
    target:
      entity_id: group.all_open_shutters
description: Closes all open shutters
icon: mdi:window-shutter


If there’s also an easier way, I’m all ears. Sometimes I complicate things.

What’s the reason for creating a group? Is it referenced outside of the script as well?

Because if it’s not, then the script can be reduced to a single cover.close_cover action.

alias: Close all open shutters
sequence:
  - action: cover.close_cover
    target:
      entity_id: >
        {{ expand('cover.shutters_zentral')
          | selectattr('state', 'equalto', 'open')
          | map(attribute='entity_id') | list  }}
description: Closes all open shutters
icon: mdi:window-shutter

If all covers are already closed, the action simply closes nothing.

2 Likes

Great, thank you very much. That works perfectly. I thought when the cover.close_cover action is executed at least an entity_id must be passed. But it does not lead to any error in the logs.

The following works to open only those that are closed or in an intermediate position:

alias: Open all shutters
sequence:
  - action: cover.open_cover
    target:
      entity_id: >
        {{ expand('cover.shutters_zentral') |
        selectattr('attributes.current_position', '<', 100) |
        map(attribute='entity_id') |  list}}
description: Opens all covers that are not already completely open
icon: mdi:window-shutter