Coordinate multiple automations & scripts by priority label

Description
This coordinates multiple automations and scripts that are identified by their Label name and priority

It works in that way that automations with higher priority turn off automations with lower priority. When the automations / scripts with higher priority finish they will turn back on automations / scripts with lower priority.

This is useful if you have multiple automations that affect the same entities and you do not want to put conditions everywhere distributed in your automations. The automations get more decoupled and do not need to know the states of each other.

For example: One automation controls the bathroom fan depending on the CO2 level. Another automation controls the same fan depending on the presence in the bathroom. This automation overrides the first automation so that the CO2 level is not relevant for the time the 2nd automation runs.

This is an improvement of the blueprint Coordinate multiple automations / simple choreography by priority

no groups are needed in the new version, You can migrate to this version of the blueprint by following the requirements below and removing the old automation afterwards.

Requirements

  1. All automations and scripts that need to be coordination must have the same label
  2. Each automation and scripts needs a additional label with a number that represents the priority (use digits only to that they can be compared within the automation coordination)
  3. Create the automation from the blueprint and provide the common label and all the automations and scripts that need to be controlled.
  4. Needs Home Assistant 2024.4 to provide labels

Blueprint Code

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or import this Blueprint by using the forum topic URL:

blueprint:
  name: _Automation and Script Coordination
  description: This coordinates multiple automations and scripts that are identified by their Label name and priority. It works in that way that automations with higher priority turn off automations with lower priority. When the automations / scripts with higher priority finish they will turn back on automations / scripts with lower priority. This is useful if you have multiple automations that affect the same entities and you do not want to put conditions everywhere distributed in your automations. The automations get more decoupled and do not need to know the states of each other. For example- One automation controls the bathroom fan depending on the CO2 level. Another automation controls the same fan depending on the presence in the bathroom. This automation overrides the first automation so that the CO2 level is not relevant for the time the 2nd automation runs.
  domain: automation
  homeassistant:
    min_version: 2024.4.0
  input:
    tag:
      name: Coordination_Tag
      description: The automations and scripts that are coordinated must have the same tag. and an additional tag with a simple number that define the priority level. higher number is a higher priority.
      selector:
        label:
          multiple: false
    automations:
      name: Automation_Entities
      description: Select the Automations and Scripts that you want to coordinate.
      selector:
        entity:
          multiple: true
          domain:
            - automation
            - script
variables:
  coordinationtag: !input tag
  automations: !input automations
trigger:
  - platform: state
    entity_id: !input automations
    attribute: current
condition:
action:
  - variables:
      priority: >
        {% set priority = int(max(labels(trigger.entity_id) | reject("match", label_name(coordinationtag)) | list)) %}
        {% if priority is not none %}
          {{ priority }}
        {% else %}
          false    
        {% endif %}
  - choose:
      - conditions:
          - condition: template
            value_template: >
              {% if trigger.from_state and trigger.to_state.attributes.current == 1 %}
              true
              {% else %}
              false    
              {% endif %}
        sequence:
          - repeat:
              count: >-
                {{ automations | count }}
              sequence:
                - condition: template
                  value_template: >
                    {% set currentautomationpriority = int((labels(automations[int(repeat.index)-1]) | reject("match", label_name(coordinationtag)) | list)[0]) %}
                    {% if currentautomationpriority < priority %}
                    {{true}}
                    {% else %}
                      {{false}}
                    {% endif %}
                - service: automation.turn_off
                  target:
                    entity_id: >
                      {{ automations[int(repeat.index)-1] }}
      - conditions:
          - condition: template
            value_template: >
              {% if trigger.from_state and trigger.to_state.attributes.current
              == 0 %}
                true
              {% else %}
                false    
              {% endif %}
        sequence:
          - variables:
              priority_still_running: 0
          - repeat:
              count: >-
                {{ automations | count }}
              sequence:
                - condition: template
                  value_template: >
                    {% set currentautomationpriority = int((labels(automations[int(repeat.index)-1]) | reject("match", label_name(coordinationtag)) | list)[0]) %}
                    {% if currentautomationpriority >= priority %}
                    {% elif state_attr(label_entities(automations)[int(repeat.index)-1],'current')==1 %}
                      {{true}}
                    {% else %}
                      {{false}}
                    {% endif %}
                - variables:
                    priority_still_running: 1
          - repeat:
              count: >
                {{ automations | count }}
              sequence:
                - condition: template
                  value_template: >
                    {% set currentautomationpriority = int((labels(automations[int(repeat.index)-1]) | reject("match", label_name(coordinationtag)) | list)[0]) %}
                    {% if currentautomationpriority < priority and priority_still_running == 0 %}
                      {{true}}
                    {% else%}
                      {{false}}
                    {% endif %}
                - service: automation.turn_on
                  target:
                    entity_id: >
                      {{ automations[int(repeat.index)-1] }}
    default: []
mode: single
1 Like