Coordinate multiple automations / simple choreography by priority

This coordinates multiple automations that are identified by their tag and priority.
It works in that way that automations with higher priority turn off automations with lower priority. When these automations finish they will turn back on automations with lower priority.
You can coordinate as many automations and priority levels you need.

This is usefull 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.

Please rename your automations that you want to coordinate so that this automation can identify them. The name of all automations of a specific group must follow this pattern
[automation_name]tag:[automationtagname]:priority:[decimal_number].
For example basic_airquality_tag:airquality:priority:1 manual_fresh_airflow_tag:airquality:priority:2

You can extend more automations and priority levels later and create multiple coordination automations by using different tags.

blueprint:
  name: _Automation Coordination
  description: This coordinates multiple automations that are identified by their tag and priority. It works in that way that automations with higher priority turn off automations with lower priority. When they finish all automations will be enabled again. You can coordinate as many automations and priority levels you need. Please rename your automations that you want to coordinate so that this automation can identify them. The name of all automations of a specific group must follow this pattern -automation_name tag:automationtagname:priority:decimal_number. For example basic_airquality_tag:airquality:priority:1 manual_fresh_airflow_tag:airquality:priority:2
  domain: automation
  input:
    automations:
      name: Automation_Entities
      description: The name of the automations that are coordinated  must follow this pattern -automation_name tag:automationtagname:priority:decimal_number . You have to put in the all the automation entities seperated by a semikolon (e.g. basic_airquality, manual_fresh_airflow  You can also change or extend this after the blueprint automation is created.
      selector:
        entity:
          domain: automation
trigger:
  - platform: state
    entity_id: !input automations
condition: []
action:
  - variables:
      priority: >
        {% set priority =
        trigger.to_state.attributes.friendly_name.split('priority:')[1]%}  {% if
        priority is not none %}
          {{ priority }}
        {% else %}
          false    
        {% endif %}
      tag: >
        {% set tag =
        trigger.to_state.attributes.friendly_name.split('tag:')[1]%}  {% set tag
        = tag.split(':priority:')[0]%} {% if tag is not none %}
          {{ tag }}
        {% else %}
          false    
        {% endif %}
  - service: group.set
    data:
      name: priorityautomation_{{tag}}
      entities: !input automations
      object_id: priorityautomation_{{tag}}
  - choose:
      - conditions:
          - condition: template
            value_template: >
              {% if trigger.from_state and trigger.to_state.attributes.current
              == 1 %}
                true
              {% else %}
                false    
              {% endif %}
        sequence:
          - repeat:
              count: >-
                {% set groupname = 'group.priorityautomation_'+tag %} {{    
                expand([trigger,groupname]) | count }}
              sequence:
                - condition: template
                  value_template: >-
                    {% set groupname = 'group.priorityautomation_'+tag %} {%-
                    set currentautomationpriority =
                    int(state_attr(expand([trigger,groupname])[int(repeat.index)-1].entity_id,'friendly_name').split('priority:')[1])-%}
                    {%- if currentautomationpriority < priority -%}{{true}}{%-
                    else-%}{{false}}{%- endif -%}
                - service: automation.turn_off
                  target:
                    entity_id: >
                      {% set groupname = 'group.priorityautomation_'+tag %} {{
                      expand([trigger,groupname])[int(repeat.index)-1].entity_id
                      }}
      - 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: >-
                {% set groupname = 'group.priorityautomation_'+tag %} {{    
                expand([trigger,groupname]) | count }}
              sequence:
                - condition: template
                  value_template: >-
                    {% set groupname = 'group.priorityautomation_'+tag %} {%-
                    set currentautomationpriority =
                    int(state_attr(expand([trigger,groupname])[int(repeat.index)-1].entity_id,'friendly_name').split('priority:')[1])-%}
                    {%- if currentautomationpriority >= priority -%} {%- elif
                    state_attr(expand([trigger,groupname])[int(repeat.index)-1].entity_id,'current')==1
                    -%} {{true}}{%- else-%}{{false}}{%- endif -%}
                - variables:
                    priority_still_running: 1
          - repeat:
              count: >-
                {% set groupname = 'group.priorityautomation_'+tag %} {{    
                expand([trigger,groupname]) | count }}
              sequence:
                - condition: template
                  value_template: >-
                    {% set groupname = 'group.priorityautomation_'+tag %} {%-
                    set currentautomationpriority =
                    int(state_attr(expand([trigger,groupname])[int(repeat.index)-1].entity_id,'friendly_name').split('priority:')[1])-%}
                    {%- if currentautomationpriority < priority and
                    priority_still_running == 0 -%}{{true}}{%-
                    else-%}{{false}}{%- endif -%}
                - service: automation.turn_on
                  target:
                    entity_id: >
                      {% set groupname = 'group.priorityautomation_'+tag %} {{
                      expand([trigger,groupname])[int(repeat.index)-1].entity_id
                      }} 
    default: []
mode: single