Single automation for multiple devices with cool-off

Hi,

I have an automation for each blind which opens/closes it depending on the state of a binary_sensor (separate for each blind) and then forces a 30-minute delay before waiting for the trigger again.

- alias: "Blind: Sunscreener -- Balcony Right"
  id: "ureptoeivrupoervg"
  trigger:
    - platform: state
      entity_id: sensor.blind_balcony_right_optimal_state
      to: "off"
      variables:
        stejt: "close"
    - platform: state
      entity_id: sensor.blind_balcony_right_optimal_state
      to: "on"
      variables:
        stejt: "open"
  condition:
    - condition: state
      entity_id: input_boolean.blind_sunscreener_enable
      state: "on"
  action:
    - service: cover.{{stejt}}_cover
      target:
        entity_id: cover.blind_balcony_right
    - delay: "0:30:00"
  mode: single

I would like to combine the individual automation for each blind into a single automation. If it weren’t for the delay action it would be easy. Is there some way of doing it, instead of the 10 individual automations I currently have?

In case it matters for the templates, all the blinds have entity_ids of the form cover.blind_[room]_[position] and the corresponding binary_sensor is binary_sensor.blind_[room]_[position]_optimal_state

Thanks for any suggestions!

You could use a separate timer for each blind instead of the delay, and then add another condition that the corresponding timer has to be idle. Also change the automation mode to parallel. And if you have more than 10 blinds, don’t forget to set the automation’s max option accordingly.

Maybe something like:

timer:
  blind_balcony_right:
  blind_xxx_yyy:
  ...
automation:
- mode: parallel
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.blind_balcony_right_optimal_state
    - binary_sensor.blind_xxx_yyy_optimal_state
    ...
    to: ['on', 'off']
    variables:
      blind: "{{ trigger.to_state.object_id.removesuffix('_optimal_state') }}"
      action: "{{ 'open' if trigger.to_state.state == 'on' else 'close' }}"
  condition:
  - >
    {{ is_state('input_boolean.' ~ blind ~ '_enable', 'on') and
       is_state('timer.' ~ blind, 'idle') }}
  action:
  - service: timer.start
    entity_id: "timer.{{ blind }}"
    data:
      duration: "00:30"
  - service: "cover.{{ action }}_cover"
    entity_id: "cover.{{ blind }}"

^^ This! :slight_smile:

But keep in mind, “delay” doesn’t survive a restart of HA. :slight_smile: If you want that, you could eg. work with the last_changed attribute . :slight_smile:

Thanks for this – so many new things to unpack. Exactly what I was hoping to take my scripting to the next level.

Feel free to ask if you have any questions. Also, I didn’t actually try this, so if you get any errors, let me know.