Merge Automations

Hello,

Is it possible to ‘merge’ automatisch to one file/automation? I have for example 7 wall outlet automations that maybe could be in 2 or 3 automations

Example code:
Turn off:

---
#
# Walli Outlet (PHEV) - Automatically Turn Off the Power at Night after Charging
# Zet Snachts de Walli Outlet PHEV Uit wanneer het Opladen is Voltooid, Walli Aan is en Automatisch Uischakelen is Aangezet.
#

id: '1646420240190'
alias: Walli Outlet (PHEV) - Automatically Turn Off the Power at Night after Charging
description: Zet Snachts de Walli Outlet PHEV Uit wanneer het Opladen is Voltooid.

trigger:
  - type: power
    platform: device
    device_id: bcce1523eb2c9354856874b001134309
    entity_id: sensor.walli_outlet_phev_electric_consumption_w
    domain: sensor
    below: 100
    for:
      hours: 0
      minutes: 15
      seconds: 0

# Als Walli Outlet (PHEV) Aan staat voor minimaal 15 minuten && Als Walli Outlet (PHEV) Automatische On/Off Mode is ingeschakeld && 
# Het na 1u Snachts is (Zodat de Automatische On/Off Mode Vaste Automatisering gepaseerd is, dus auto aan het laden was nog.)
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_on
        device_id: bcce1523eb2c9354856874b001134309
        entity_id: switch.walli_outlet_phev
        domain: switch
        for:
          hours: 0
          minutes: 15
          seconds: 0
      - condition: state
        entity_id: input_boolean.walli_outlet_phev_mode_auto_off
        state: 'on'
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - condition: time
        after: 01:05:00
        before: 09:00:00
        weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
        - sat
        - sun

# Zet de Walli Outlet (PHEV) Uit & Zet de Automatische OFF Mode Uit & Zet de Automatische ON Mode Aan
action:
  - type: turn_off
    device_id: bcce1523eb2c9354856874b001134309
    entity_id: switch.walli_outlet_phev
    domain: switch
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.walli_outlet_phev_mode_auto_off
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.walli_outlet_phev_mode_auto_on

mode: single

Turn off night:

---
#
# Walli Outlet (PHEV) - Automatically Turn Off the Power
# Zet Snachts Automatisch de Walli Outlet (PHEV) Uit wanneer er Niet wordt Opgeladen, Walli Aan is en Automatisch Uischakelen is Aangezet.
#

id: '1646414671039'
alias: Walli Outlet (PHEV) - Automatically Turn Off the Power at Night
description: Zet Snachts de Walli Outlet (PHEV) Uit wanneer er Niet wordt Opgeladen en Automatisch Uischakelen is Aangezet.

# Check de tijd die gezet is in de bijbehoorende input_datetime sensor.
trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.walli_outlet_phev_turn_off_time.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'

# Als Walli Outlet (PHEV) Aan staat voor minimaal 10 minuten && Als Walli Outlet (PHEV) Automatische On/Off Mode is ingeschakeld.
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_on
        device_id: bcce1523eb2c9354856874b001134309
        entity_id: switch.walli_outlet_phev
        domain: switch
        for:
          hours: 0
          minutes: 10
          seconds: 0
      - condition: state
        entity_id: input_boolean.walli_outlet_phev_mode_auto_off
        state: 'on'
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - condition: device
        type: is_power
        device_id: bcce1523eb2c9354856874b001134309
        entity_id: sensor.walli_outlet_phev_electric_consumption_w
        domain: sensor
        below: 100    
        for:
          hours: 0
          minutes: 10
          seconds: 0

# Zet de Walli Outlet (PHEV) Uit & Zet de Automatische OFF Mode Uit & Zet de Automatische ON Mode Aan
action:
  - type: turn_off
    device_id: bcce1523eb2c9354856874b001134309
    entity_id: switch.walli_outlet_phev
    domain: switch
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.walli_outlet_phev_mode_auto_off
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.walli_outlet_phev_mode_auto_on

mode: single

Turn on:

---
#
# Walli Outlet (PHEV) - Automatically Turn On the Power
# Zet Smorgens Automatisch de Walli Outlet PHEV weer Aan wanneer Walli Uit is en Automatisch Inschakelen is Aangezet.
#

id: '1646414344322'
alias: Walli Outlet (PHEV) - Automatically Turn On the Power at Morning
description: Zet Smorgens de Walli Outlet (PHEV) weer Aan wanneer Automatisch Inschakelen is Aangezet.

# Check de tijd die gezet is in de bijbehoorende input_datetime sensor.
trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.walli_outlet_phev_turn_on_time.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'

# Als Walli Outlet (PHEV) Uit staat voor minimaal 10 minuten && Als Walli Outlet (PHEV) Automatische On/Off Mode is ingeschakeld.
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: bcce1523eb2c9354856874b001134309
        entity_id: switch.walli_outlet_phev
        domain: switch
        for:
          hours: 0
          minutes: 10
          seconds: 0
      - condition: state
        entity_id: input_boolean.walli_outlet_phev_mode_auto_on
        state: 'on'
        for:
          hours: 0
          minutes: 5
          seconds: 0

# Zet de Walli Outlet (PHEV) Aan & Zet de Automatische ON Mode Uit & Zet de Automatische OFF Mode Aan
action:
  - type: turn_on
    device_id: bcce1523eb2c9354856874b001134309
    entity_id: switch.walli_outlet_phev
    domain: switch
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.walli_outlet_phev_mode_auto_on
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.walli_outlet_phev_mode_auto_off

mode: single

Could maybe be in 1 file. but i do not know how to do it.

Can someone please help or give me an example how to put automations into one :slight_smile:

Thanks

One way to merge many automations into a single automation is as follows:

  1. Add all the required triggers from all the automations being merged as triggers for the new automation.
  2. Give each trigger a meaningful Trigger ID.
  3. Don’t enter any conditions in the main Conditions area.
  4. Add a Choose action to the Actions area.
  5. Add an option to the Choose action for each automation being merged. For each option:
    a. The first condition will be ‘triggered by’ with the appropriate Trigger ID.
    b. Additional conditions can be added to the option to mimic the original automation being merged.
  6. Add actions to the option to mimic the actions of the automation being merged.

I hope this helps.

2 Likes

Sadly there isn’t a “merge automation” option corresponding to the “duplicate automation” one. You have to bite the bullet and rewrite them.

As @templeton_nash says, one simple approach is to have multiple triggers, followed by a choose block with one option for each of the triggers. This makes sense when the triggers are similar (like your “Turn power on/off”).

If you have several automations with the same trigger entity but different trigger values, another option is to leave the trigger with no values defined - for example a state trigger with the from and to fields empty. This will fire every time the state of the entity updates, whatever the change, and you can use conditions in a choose block to define what happens each time.

Mostly I start a project with several small automations and often I don’t bother to merge them - there’s nothing much to be gained by having a single large one and six months later it will be harder to maintain.

2 Likes

Never knew about the choose option, i will have a look at it! thanks, wil post the results if i get one done :slight_smile:

@templeton_nash @Stiltjack

Long time ago :stuck_out_tongue: but i created a test:

alias: AllergyInfo - Air Quality Status Notification
description: Luchtkwaliteit Status Melding
trigger:
  - platform: state
    entity_id:
      - sensor.air_quality_today_names
    to: Slecht
    from: null
    id: air_change
  - platform: state
    entity_id:
      - sensor.air_quality_today_names
    to: Ongezond
    from: null
    id: air_change
  - platform: state
    entity_id:
      - sensor.air_quality_today_names
    to: Zeer Ongezond
    from: null
    id: air_change
  - platform: state
    entity_id:
      - sensor.air_quality_today_names
    to: Gevaarlijk
    from: null
    id: air_change
  - platform: time
    at: "12:00"
    id: time
    enabled: true
action:
  - choose:
      - conditions:
          - condition: trigger
            id: air_change
        sequence:
          - service: notify.rick_android
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.air_quality_today_names') }} Momenteel
              data:
                channel: Allergie Info
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: time
              - condition: or
                conditions:
                  - condition: state
                    entity_id: sensor.air_quality_today_names
                    state: Slecht
                  - condition: state
                    entity_id: sensor.air_quality_today_names
                    state: Ongezond
                  - condition: state
                    entity_id: sensor.air_quality_today_names
                    state: Zeer Ongezond
                  - condition: state
                    entity_id: sensor.air_quality_today_names
                    state: Gevraarlijk
        sequence:
          - service: notify.rick_android
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.air_quality_today_names') }} Vandaag
              data:
                channel: Allergie Info

However, when I look at the tracing it shows 3 options

I don’t understand why that is…

There is a default option for if no condition (blocks) match. You have no actions in your default option so the line goes straight to the end.

1 Like

Ah oke I understand.
There is one ‘error’ every time I reload/restart HA it wil trigger. Any option to prevent this?
It is happening because of the status change ‘from any to xxx’.

You can avoid those unwanted triggers by using not_from to reject from states you aren’t interested in… in the case of reloads/restarts that would be “unavailable”. You can also merge your state triggers and conditions into one of each by listing the desired states.

alias: AllergyInfo - Air Quality Status Notification
description: Luchtkwaliteit Status Melding
trigger:
  - platform: state
    entity_id:
      - sensor.air_quality_today_names
    to: 
      - Slecht
      - Ongezond
      - Zeer Ongezond
      - Gevaarlijk
    not_from: unavailable
    id: air_change
  - platform: time
    at: "12:00"
    id: time
action:
  - choose:
      - conditions:
          - condition: trigger
            id: air_change
        sequence:
          - service: notify.rick_android
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.air_quality_today_names') }} Momenteel
              data:
                channel: Allergie Info
      - conditions:
          - condition: trigger
            id: time
          - condition: state
            entity_id: sensor.air_quality_today_names
            state: 
              - Slecht
              - Ongezond
              - Zeer Ongezond
              - Gevaarlijk
        sequence:
          - service: notify.rick_android
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.air_quality_today_names') }} Vandaag
              data:
                channel: Allergie Info
1 Like

Thank you! this also looks much nicer :smiley:

I have no idea what i should put as a defaultcondition, i tried “stop” but it seems to ignore that.

I don’t think you need to do anything. In this setup, if any trigger fires but it’s respective conditions are not met, nothing will happen.

1 Like

Seems it still gives me an error when the value is “Slecht”, “Ongezond” etc.

I tried it like this:

  - platform: state
    entity_id:
      - sensor.air_quality_today_names
    to: 
      - Slecht
      - Ongezond
      - Zeer Ongezond
      - Gevaarlijk
    not_from: 
      - unavailable
      - unknown
      - onbekend
    id: air_change

but the trace now removes all not_from options and still gives a notification when restarting HA

Post the error log and trace.



air3

No error log, but unwanted notification, after restart the value wil be ‘onbekend’/‘unknown’. and changes to “slecht” and triggers

---
#
# Send Notification on Air Quality Status Change
#
# Send Notification on Air Quality Status Today  (12:00u)
#

id: "accu_allergies_air_quality_status_notification"
alias: "Accu Allergies - Air Quality Status Notification"
description: "Stuur een melding over de luchtkwaliteit."

trigger:
  - platform: state
    entity_id:
      - sensor.acc_air_today_names
    to:
      - Redelijk
      - Slecht
      - Ongezond
      - Zeer Ongezond
      - Gevaarlijk
    not_from: 
      - unavailable
      - unknown
    id: air_change
  
  - platform: time
    at: "12:00"
    id: time

condition: []

action:
  - choose:
      - conditions:
          - condition: trigger
            id: air_change
        sequence:
          - service: notify.all_phones
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.acc_air_today_names') }} Momenteel
              data:
                channel: "Allergie Info"
      
      - conditions:
          - condition: trigger
            id: time
          - condition: state
            entity_id: sensor.acc_air_today_names
            state:
              - Slecht
              - Ongezond
              - Zeer Ongezond
              - Gevaarlijk
        sequence:
          - service: notify.rick_android
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.acc_air_today_names') }} Vandaag
              data:
                channel: "Allergie Info"

mode: single

@Didgeridrew
I have found it, it was “Onbekend” i needed to add, with capital O :sweat_smile:

1 Like

The State trigger is very literal, so make sure you are using upper and lower case letters as shown in the Developer Tools > States tool… the History graph will often show a “prettier” version of an entity’s state and as such is not a reliable source.

When sharing Traces, download then copy/paste the trace json object so we can access all the data without you having to post 20 different screenshots.

1 Like

Thanks, did not know about the trace log

{
  "trace": {
    "last_step": "action/0/choose/1/conditions/0",
    "run_id": "ff0f4a2cd5e84caf023bec6377b70c1f",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-06-23T14:42:30.037273+00:00",
      "finish": "2023-06-23T14:42:30.051964+00:00"
    },
    "domain": "automation",
    "item_id": "accu_allergies_air_quality_status_notification",
    "trigger": null,
    "trace": {
      "trigger": [
        {
          "path": "trigger",
          "timestamp": "2023-06-23T14:42:30.039738+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.accu_allergies_air_quality_status_notification",
              "state": "on",
              "attributes": {
                "last_triggered": "2023-06-23T14:40:07.736701+00:00",
                "mode": "single",
                "current": 0,
                "id": "accu_allergies_air_quality_status_notification",
                "friendly_name": "Accu Allergies - Air Quality Status Notification"
              },
              "last_changed": "2023-06-23T14:41:27.976921+00:00",
              "last_updated": "2023-06-23T14:41:27.976921+00:00",
              "context": {
                "id": "01H3MB7XD8M15JJQ1YXC6MJKRF",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "platform": null
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2023-06-23T14:42:30.046330+00:00",
          "changed_variables": {
            "context": {
              "id": "01H3MB9T0NH2FYGXTAD6XYZZA4",
              "parent_id": "01H3MB9T0BYQ5H6JZQAJW77FPG",
              "user_id": null
            }
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2023-06-23T14:42:30.047251+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2023-06-23T14:42:30.048639+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1": [
        {
          "path": "action/0/choose/1",
          "timestamp": "2023-06-23T14:42:30.048834+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0",
          "timestamp": "2023-06-23T14:42:30.048895+00:00",
          "result": {
            "result": false
          }
        }
      ]
    },
    "config": {
      "id": "accu_allergies_air_quality_status_notification",
      "alias": "Accu Allergies - Air Quality Status Notification",
      "description": "Stuur een melding over de luchtkwaliteit.",
      "trigger": [
        {
          "platform": "state",
          "entity_id": "sensor.acc_air_today_names",
          "to": [
            "Redelijk",
            "Slecht",
            "Ongezond",
            "Zeer Ongezond",
            "Gevaarlijk"
          ],
          "not_from": [
            "unavailable",
            "unknown",
            "Onbekend"
          ],
          "id": "air_change"
        },
        {
          "platform": "time",
          "at": "12:00",
          "id": "time"
        }
      ],
      "condition": [],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "air_change"
                }
              ],
              "sequence": [
                {
                  "service": "notify.all_phones",
                  "data": {
                    "message": "{{ states('sensor.acc_air_today_info') }}",
                    "title": "Allergie Info - Luchtkwaliteit is {{ states('sensor.acc_air_today_names') }} Momenteel",
                    "data": {
                      "channel": "Allergie Info"
                    }
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "time"
                },
                {
                  "condition": "state",
                  "entity_id": "sensor.acc_air_today_names",
                  "state": [
                    "Slecht",
                    "Ongezond",
                    "Zeer Ongezond",
                    "Gevaarlijk"
                  ]
                }
              ],
              "sequence": [
                {
                  "service": "notify.rick_android",
                  "data": {
                    "message": "{{ states('sensor.acc_air_today_info') }}",
                    "title": "Allergie Info - Luchtkwaliteit is {{ states('sensor.acc_air_today_names') }} Vandaag",
                    "data": {
                      "channel": "Allergie Info"
                    }
                  }
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H3MB9T0NH2FYGXTAD6XYZZA4",
      "parent_id": "01H3MB9T0BYQ5H6JZQAJW77FPG",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Accu Allergies - Air Quality Status Notification",
      "message": "triggered",
      "source": null,
      "entity_id": "automation.accu_allergies_air_quality_status_notification",
      "context_id": "01H3MB9T0NH2FYGXTAD6XYZZA4",
      "when": 1687531350.040033,
      "domain": "automation"
    }
  ]
}

But indeed it was the “Onbekend” and also i added them all with quotes now (Not sure if that is needed/standard)

It is working correctly now :smiley: Thanks

---
#
# Send Notification on Air Quality Status Change
#
# Send Notification on Air Quality Status Today  (12:00u)
#

id: "accu_allergies_air_quality_status_notification"
alias: "Accu Allergies - Air Quality Status Notification"
description: "Stuur een melding over de luchtkwaliteit."

trigger:
  - platform: state
    entity_id: sensor.acc_air_today_names
    to:
      - "Slecht"
      - "Ongezond"
      - "Zeer Ongezond"
      - "Gevaarlijk"
    not_from: 
      - "unavailable"
      - "unknown"
      - "Onbekend"
    id: air_change
  
  - platform: time
    at: "12:00"
    id: time

condition: []

action:
  - choose:
      - conditions:
          - condition: trigger
            id: air_change
        sequence:
          - service: notify.all_phones
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.acc_air_today_names') }} Momenteel
              data:
                channel: "Allergie Info"
      
      - conditions:
          - condition: trigger
            id: time
          - condition: state
            entity_id: sensor.acc_air_today_names
            state:
              - "Slecht"
              - "Ongezond"
              - "Zeer Ongezond"
              - "Gevaarlijk"
        sequence:
          - service: notify.rick_android
            data:
              message: "{{ states('sensor.acc_air_today_info') }}"
              title: >-
                Allergie Info - Luchtkwaliteit is {{
                states('sensor.acc_air_today_names') }} Vandaag
              data:
                channel: "Allergie Info"

mode: single