Convert 2 automations to 1

Guys, i have a lot of automations like below, quite stupid… is there a way to convert them to 1 ?


- alias: Sensor offline
  initial_state: 'on'
  trigger:
  - entity_id: binary_sensor.smappee_ping 
    platform: state
    from: 'on'
    to: 'off'
  action:    
  - service: !secret notify_ha_fabio
    data:
      title: "Smappee"
      message: "Connectie weggevallen!!"
      data:
        channel: Smappee
        priority: high
        ttl: 0  

- alias: Sensor online
  initial_state: 'on'
  trigger:
  - entity_id: binary_sensor.smappee_ping 
    platform: state
    from: 'off'
    to: 'on'
  action:    
  - service: !secret notify_ha_fabio
    data:
      title: "Smappee"
      message: "Connectie online!!"
      data:
        channel: Smappee
        priority: high
        ttl: 0

Use two triggers in your automation and assign each a different trigger ID. In the action part you can use a choose or if condition that checks which trigger ID was fired and execute your actions accordingly.

indeed, found something similar:

Something like this maybe:

alias: Tado device connection
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroom_trv_connection_state
      - binary_sensor.bedroom_trv_master_connection_state
      - binary_sensor.bedroom_trv_slave_connection_state
      - binary_sensor.front_room_thermostat_connection_state
      - binary_sensor.front_room_trv_connection_state
      - binary_sensor.2_bedroom_trv_connection_state
      - binary_sensor.hall_trv_connection_state
      - binary_sensor.kitchen_trv_connection_state
      - binary_sensor.spare_room_trv_connection_state
      - binary_sensor.tado_boiler_control_connection_state
      - binary_sensor.toilet_trv_connection_state
    id: lost device link
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 40
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.bathroom_trv_connection_state
      - binary_sensor.bedroom_trv_master_connection_state
      - binary_sensor.bedroom_trv_slave_connection_state
      - binary_sensor.front_room_thermostat_connection_state
      - binary_sensor.front_room_trv_connection_state
      - binary_sensor.2_bedroom_trv_connection_state
      - binary_sensor.hall_trv_connection_state
      - binary_sensor.kitchen_trv_connection_state
      - binary_sensor.spare_room_trv_connection_state
      - binary_sensor.tado_boiler_control_connection_state
      - binary_sensor.toilet_trv_connection_state
    id: device link back
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition:
  - condition: state
    entity_id: input_boolean.dnd_person1
    state: "off"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: lost device link
          - condition: state
            entity_id: input_boolean.do_not_disturb_on_off
            state: "off"
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.telegram_notifications
                state: "on"
            then:
              - service: notify.telegram_person1
                data_template:
                  message: >-
                    WARNING {{ trigger.to_state.attributes.friendly_name }}
                    shows a disconnection problem.
      - conditions:
          - condition: trigger
            id: device link back
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.telegram_notifications
                state: "on"
            then:
              - service: notify.telegram_person1
                data_template:
                  message: >-
                    NOTICE - {{ trigger.to_state.attributes.friendly_name }}
                    shows to be back on the network.
    default: []
trace:
  stored_traces: 10
mode: single

This is part of a much bigger automation that reboots network devices etc, so I have stripped out only the parts that I think would be relevant to your scenario. Hope it helps in some way.

yes , helpfull, thnx for sharing!

Can i also put the ID on/off in a condition template lke below? Instead of the trigger …

- alias: Velux regen detectie
  initial_state: 'on'
  description: ''
  mode: single
  trigger:
    - platform: state
      entity_id: cover.velux_links
  condition:
    - condition: template
      value_template: "{{ is_state_attr('cover.velux_links', 'limitation_min', 93) }}" 
      id: 'on' 
    - condition: template
      value_template: "{{ is_state_attr('cover.velux_links', 'limitation_min', 0) }}" 
      id: 'off'         
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: 'on'
          sequence:
            - service: input_boolean.turn_on
              entity_id: input_boolean.velux_regen             

      default: []
    - choose:
        - conditions:
            - condition: trigger
              id: 'off'
          sequence:
            - service: input_boolean.turn_off
              entity_id: input_boolean.velux_regen 
      default: []

Yes, that is also an option :+1:

hmm, seems that doesnt work in example above

this example below, works, when i put the ID in the trigger part
but when i put the ID in condition part, it doesnt work

- alias: Smappee PING Sensor
  initial_state: 'on'
  description: ''
  mode: single
  trigger:
  - entity_id: binary_sensor.smappee_ping 
    platform: state
    from: 'off'
    to: 'on'
    id: 'on'    
  - entity_id: binary_sensor.smappee_ping 
    platform: state
    from: 'on'
    to: 'off'
    id: 'off'
  condition: []
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: 'on'
          sequence:
            - service: !secret notify_ha_fabio
              data:
                title: "Smappee"
                message: "Smappee connectie online!!"
                data:
                  channel: Smappee
                  priority: high
                  ttl: 0              

      default: []
    - choose:
        - conditions:
            - condition: trigger
              id: 'off'
          sequence:
            - service: !secret notify_ha_fabio
              data:
                title: "Smappee"
                message: "Smappee connectie offline!!"
                data:
                  channel: Smappee
                  priority: high
                  ttl: 0
      default: []

i receive this error then:

2023-02-03 11:20:54.373 ERROR (MainThread) [homeassistant.components.automation] Automation with alias 'Velux regen detectie' could not be validated and has been disabled: extra keys not allowed @ data['condition'][0]['id']. Got 'on'

Sorry my bad, I did not look at the conditions, you cant use the trigger ID in the template conditions like this i don’t believe.

As soon as you need to work with ids and the cumbersome choose logic I woul prefer the two over one automation in your use case.

That said, in your specific example you should be able to be handle this neatly:

- alias: Send notification following smappee sensor # Food for thought: Be more explicit in your aliases
  trigger:
  - entity_id: binary_sensor.smappee_ping 
    platform: state
  action:    
  - service: !secret notify_ha_fabio
    data:
      title: "Smappee"
      # try this first:
      message: "Connectie {{ trigger.to_state.state }}"
      # then this:
      message: "Connectie {{ 'online' if trigger.to_state.state == 'on' else 'weggevallen' }}!!"
      data:
        channel: Smappee
        priority: high
        ttl: 0  

thats indeed a possibility, but i also have automations where i dont want to use trigger to state :slight_smile: one

like these below, based on on/off i want todo complete different services…
how can i use a condition there?

- alias: Xiaomi Sensor Badkamer
  initial_state: 'on'
  mode: single
  trigger:
  - entity_id: binary_sensor.xiaomi_sensor_3
    platform: state
    from: 'off'
    to: 'on'
    id: 'on'
  condition:
    - condition: numeric_state
      entity_id: sensor.xiaomi_sensor_3
      below: 20     
  - entity_id: binary_sensor.xiaomi_sensor_3 
    platform: state
    from: 'on'
    to: 'off'
    for:
      minutes: 1
    id: 'off'

I think that depends on your view point and more importantly you technical ability.

I personally don’t find using trigger ID’s and/or choose options to be cumbersome.
I find them to be a very useful tool for anyone that relies on the Ui for creating automations and scripts.

You can ofcourse minimise most of your automations and/or scripts with templates but I always (maybe wrongly) assume that when people ask questions about if or how to combine multiple automations they are most likely using the Ui and even more likely to have limited yaml and/or template knowledge.

Also from a new or novice user point of view I cant help but think so many of the answers given in yaml / templates are simply cut and pasted by the OP’s and this can lead to limited knowledge improvement therefore leading to them having to ask next time they want to achieve similar results.

Maybe I am over thinking this as with all things HA there are always multiple ways to achieve the same end results, but I always try to pitch any solutions to the somewhat assumed level of knowledge shown by the OP.

Having said all that, your solution is of course elegant :wink:

1 Like

can anyone help me setting the ID to on / off based on an condition?

- alias: testing
  initial_state: 'on'
  mode: single
  trigger:
    - platform: state
      entity_id: cover.velux_links
  condition:
    - condition: template
      value_template: "{{ state_attr('sensor.dobiss', '4100') == '01' }}"
      id: 'on'
    - condition: template
      value_template: "{{ state_attr('sensor.dobiss', '4100') == '00' }}"
      id: 'off'         
  action:
    - choose:
....

or someething like this:

- alias: Xiaomi Sensor Badkamer
  initial_state: 'on'
  mode: single
  trigger:
  - entity_id: binary_sensor.xiaomi_sensor_3
    platform: state
    from: 'off'
    to: 'on'
    id: 'on'
  condition:
    - condition: numeric_state
      entity_id: sensor.xiaomi_sensor_3
      below: 20     
  - entity_id: binary_sensor.xiaomi_sensor_3 
    platform: state
    from: 'on'
    to: 'off'
    for:
      minutes: 1
    id: 'off'
  condition: []
  action:

You cant assign a trigger ID to a condition - only a trigger.

You can of course have the automation choose what to do based on the conditions by simply moving the conditions to the action section a bit like this:

trigger:
  - platform: state
    entity_id: cover.velux_links
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ state_attr('sensor.dobiss', '4100') == '01' }}"
        sequence:
          - service: ""
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ state_attr('sensor.dobiss', '4100') == '00' }}"
        sequence:
          - service: ""
            data: {}

thnx, that worked :slight_smile:

can you also apply the same trick for my xiaomi automation? because there i have a condition and a state change from on/off or off/on … its a mixed setup

With random id and “better” indentation. Uses a template in the message. The from and to lists are to stop triggers if/when it goes unknown or unavailable.

- alias: Smappee notification
  id: 3de6df48-f5d1-45b2-a2e8-6d5150306958
  trigger:
    - platform: state
      entity_id: binary_sensor.smappee_ping 
      from:
        - 'on'
        - 'off'
      to:
        - 'off'
        - 'on'
  action:    
    - service: !secret notify_ha_fabio
      data:
        title: "Smappee"
        message: "Connectie {{ iif(bool(states('binary_sensor.smappee_ping')), 'online', 'weggevallen') }}!!"
        data:
          channel: Smappee
          priority: high
          ttl: 0  

I am not sure what you are trying to achieve in this one but you cant have a condition like that under the trigger heading.

Hope that makes sense, if not let us know what it is you want to achieve OR where you want the condition (what trigger).

1 Like

Ok, let’s try that, thnx for feedback!!! Appreciated

1 Like