Combining automations - with/without delay

Hi,

I have this set of two automatons that work and i have another 6 sets of similar ones. They take up a lot of space and i was wondering if it was possible to combine each set into 1 automation.

#Turns on landing lights when motion
  - alias: Landing light motion on
    trigger:
      platform: state
      entity_id: binary_sensor.landing_motion
      to: 'on'
    action:
    - service: homeassistant.turn_on
      entity_id: light.landing_light

#Turns off landing lights after 2 mins when no motion
  - alias: Landing light motion off
    trigger:
      platform: state
      entity_id: binary_sensor.landing_motion
      to: 'off'
      for:
        minutes: 2
    action:
    - service: homeassistant.turn_off
      entity_id: light.landing_light

i thought something like this but don’t know how to code the delay. is it possibel without setting up a seperate script?

#landing lights when motion
  - alias: Landing light motion
    trigger:
      platform: state
      entity_id: binary_sensor.landing_motion
    action:
    - service_template: >
      {% if states('binary_sensor.landing_motion') == 'on' %}
        light.turn_on
      {% else %}
        light.turn_off
      {% endif %}
    entity_id: light.landing_light
  - alias: Landing light motion on and off
    trigger:
    - platform: state
      entity_id: binary_sensor.landing_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.landing_motion
      to: 'off'
      for:
        minutes: 2
    action:
    - service_template: "light.turn_{{ trigger.to_state.state }}"
      entity_id: light.landing_light
2 Likes

interesting - thanks

Is there any uniformity in how they are named? For example, you have landing_motion and landing_light. They have ‘landing_’ in common. Do the others share this format where a common word precedes either motion or light (separated by an underscore)?

If they do then there’s an opportunity to uses Phil’s example to control all of them (i.e. one automation to rule them all).

so i have these other sets of automation’s. im assuming i could combine the 1st group into 1 automation as you suggest but no idea how to do it.

1st group

#Landing motion light
  - alias: Landing light motion
    trigger:
    - platform: state
      entity_id: binary_sensor.landing_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.landing_motion
      to: 'off'
      for:
        minutes: 2
    action:
    - service_template: "light.turn_{{ trigger.to_state.state }}"
      entity_id: light.landing_light

#kitchen lights when motion
  - alias: Kitchen light motion
    trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.kitchen_motion
      to: 'off'
      for:
        minutes: 10
    action:
    - service_template: "light.turn_{{ trigger.to_state.state }}"
      entity_id: light.kitchen


#dining lights when motion
  - alias: dining light motion
    trigger:
    - platform: state
      entity_id: binary_sensor.dining_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.dining_motion
      to: 'off'
      for:
        minutes: 10
    action:
    - service_template: "light.turn_{{ trigger.to_state.state }}"
      entity_id: light.dining

then different but still combine-able now with phils code

2nd group

#Turns on/off guest light with door
  - alias: Guest light
    trigger:
      platform: state
      entity_id: binary_sensor.guest_door
      to: 'on'
    action:
    - service: homeassistant.turn_on
      entity_id: light.guest

#Turns off guest light when door closes
  - alias: Guest light door close
    trigger:
      platform: state
      entity_id: binary_sensor.guest_door
      to: 'off'
    action:
    - service: homeassistant.turn_off
      entity_id: light.guest

and then 3rd group

#Turns off lamps when shield is playing
  - alias: shield Lamps off
    trigger:
      platform: state
      entity_id: media_player.shield
      to: playing
    action:
    - service: homeassistant.turn_off
      entity_id: light.lamps

#Turns on lamps to 1% when shield stops playing
  - alias: shield Lamps on
    trigger:
      platform: state
      entity_id: media_player.shield
      from: playing
    action:
    - service: homeassistant.turn_on
      entity_id: light.lamps
      data:
        brightness_pct: 1

#Hall lamp on
  - alias: Hall lamp on
    trigger:
      platform: sun
      event: sunset
      offset: "-00:45:00"
    action:
      service: homeassistant.turn_on
      entity_id: light.hall_lamp

#Hall lamp off
  - alias: Hall lamp off
    trigger:
    - at: '00:00'
      platform: time
    action:
      - service: light.turn_off
        entity_id: light.hall_lamp

There’s not much commonality in the way the entities are named so the idea I had in mind won’t work.

@pnbruckner @123

do you know if there is any documentation/guides on how to combine automation for beginners?

Also, i have these automation to run different services on my beside light depends on they type of button push. is there anyway to combine them?

#' Zigbee2MQTT bedrooom buttons automation
  - alias: Single Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: "{{ 'single' == trigger.payload_json.click }}"
    action:
      entity_id: light.bedroom_stephenlamp
      service: light.toggle


  - alias: Double Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: "{{ 'double' == trigger.payload_json.click }}"
    action:
    - service: light.turn_on
      entity_id: light.bedroom_stephenlamp
      data:
        brightness_pct: 1


  - alias: Triple Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: "{{ 'triple' == trigger.payload_json.click }}"
    action:
    - service: light.turn_on
      entity_id: light.bedroom_stephenlamp
      data:
        brightness_pct: 100

  - alias: quad Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: "{{ 'quadruple' == trigger.payload_json.click }}"
    action:
    - service: light.toggle
      entity_id: light.bedroom_lamps

  - alias: Long Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: "{{ 'long_release' == trigger.payload_json.click }}"
    action:
    - service: light.toggle
      entity_id: light.bedroom_light

Not that I’m aware of. You just have to read the docs, experiment, etc.

Try this:

#' Zigbee2MQTT bedrooom buttons automation
  - alias: Single/Quad/Long Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: >
        {{ trigger.payload_json.click in ('single', 'quadruple', 'long_release') }}
    action:
      data_template:
        entity_id: >
          light.{{ {'single': 'bedroom_stephenlamp',
                    'quadruple': 'bedroom_lamps',
                    'long_release': 'bedroom_light'}[trigger.payload_json.click] }}
      service: light.toggle

  - alias: Double/Triple Click x
    hide_entity: true
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/0x00158d0001e80911'
    condition:
      condition: template
      value_template: "{{ trigger.payload_json.click in ('double', 'triple') }}"
    action:
    - service: light.turn_on
      entity_id: light.bedroom_stephenlamp
      data_template:
        brightness_pct: "{{ 1 if trigger.payload_json.click == 'double' else 100 }}"

Thank you - much better.

I’v been trying to learn. You managed to condense my original post from 2 automation to 1 but i should have said i has 2 other sets of 2 automation for 2 different rooms doing the same things. would the following 2 automatons work to turn on/off the lights independently in each of my 3 rooms?

  - alias: experiment - motion lights on
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion
          - binary_sensor.dining_motion
          - binary_sensor.landing_motion
        to: 'on'
    action:
      - service: light.turn_on
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: experiment - motion lights off
    trigger:
    - platform: state
      entity_id:
        - binary_sensor.kitchen_motion
        - binary_sensor.dining_motion
      to: 'off'
      for: '00:10:00'
    - platform: state
      entity_id:
        - binary_sensor.landing_motion
      to: 'off'
      for: '00:02:00'
    action:
      - service: light.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

3 posts were split to a new topic: IFTTT applet skipped

Close, but not really. trigger.entity_id is the entity_id of the entity that caused the automation to trigger. But those are binary_sensor’s, not lights. So you’d be trying to use the light turn_on/turn_off services to turn binary_sensors on or off, which of course would not work.

Given what you provided earlier, I think you can handle all of them in one automation. What would make it much easier is if you changed light.landing_light to light.landing. I’ll assume you do this. Then you can do this:

  - alias: experiment - motion lights on
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion
          - binary_sensor.dining_motion
          - binary_sensor.landing_motion
        to: 'on'
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion
          - binary_sensor.dining_motion
        to: 'off'
        for: '00:10:00'
      - platform: state
        entity_id:
          - binary_sensor.landing_motion
        to: 'off'
        for: '00:02:00'
    action:
      - service_template: "light.turn_{{ trigger.to_state.state }}"
        data_template:
          entity_id: "light.{{ trigger.to_state.object_id.replace('_motion','') }}"
1 Like

There’s the approach I had in mind. :+1:

If there’s uniformity and commonality in the naming of motion sensors and corresponding lights, a template can easily parse their names (without any undue complexity).

So if I understand that correctly - that automation deals with both on and off( with delay)?

1 automation to rule them all - nice!!!

1 Like

Yes but, like I stated earlier, there’s an opportunity to use Phil’s one automation to rule them all. It all depends on how you have named the entities … and if you’re willing to rename them in order to take advantage of that opportunity.

If the naming follows a fixed, consistent pattern like location_function (e.g landing_motion and landing_light) then the template has an easy time of manipulating them (see Phil’s example above).

Just be aware that there has been a bug in the for: option that will prevent this from working correctly in all situations. However I fixed that bug and the fix is in 0.96.

thanks for your help guys.

Iv been trying to learn while am away from home having some down time but its hard to test because I’m not in my house.

I think i managed to convert 2 separate on/off automation into the one below. hopefully it will work

#Hall lamp on
  - alias: Hall lamp
    trigger:
      platform: sun
      event: sunset
      offset: "-00:60:00"
    action:
    - service: light.turn_on
      entity_id: light.hall_lamp
    - wait_template: "{{ states.sensor.time.state == '00:00' }}"
    - service: light.turn_off
      entity_id: light.hall_lamp

but I’m having trouble trying to make 1 automation to turn on/off lamps for my shield depending on playing state. I want it to switch off when it starts playing and switch on when paused/idle but to 1% brightness - strugling to add data into the automation.

#Turns off lamps when shield is playing
  - alias: shield Lamps off
    trigger:
    - platform: state
      entity_id: media_player.shield
      to: playing
    - platform: state
      entity_id: media_player.shield
      from: playing
    action:
    - service_template: >
        {% if is_state('media_player.shield', 'playing') %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
    - data_template: >
        {% if is_state('media_player.shield', 'playing') %}
          light.turn_off
        {% else %}
          light.turn_off
        {% endif %}
    entity_id: light.lamps

these work below

#Turns off lamps when shield is playing
  - alias: shield Lamps off
    trigger:
      platform: state
      entity_id: media_player.shield
      to: playing
    action:
    - service: homeassistant.turn_off
      entity_id: light.lamps

#Turns on lamps to 1% when shield stops playing
  - alias: shield Lamps on
    trigger:
      platform: state
      entity_id: media_player.shield
      from: playing
    action:
    - service: homeassistant.turn_on
      entity_id: light.lamps
      data:
        brightness_pct: 1

hi ,
I’m following your advice,
this is my automation, do you think should work?

- alias: Luce muro esterno  on and off su movimento
  trigger:
  - platform: state
    entity_id: binary_sensor.0x000b57fffe91ee74_occupancy
    to: 'on'
  - platform: state
    entity_id: binary_sensor.0x000b57fffe91ee74_occupancy
    to: 'off'
    for:
      minutes: 2
  condition:
    condition: time
    after: '01:00:00'
    before: '06:00:00'    
  action:
  - service_template: "homeassistant.turn_{{ trigger.to_state.state }}"
    entity_id: switch.luci_muro_esterno
  - delay: '00:00:02'
  - service_template: "homeassistant.turn_{{ trigger.to_state.state }}"
    entity_id: switch.parentesi     

Looks ok to me. Does it work the way you want?

Just checked… works perfect! I didn’t had time to verify because I wrote the automation at office , so I ad a dubit. Thank you

1 Like