Combine on/off automations into one automation?

I have 2 automatons that are simply to turn on and turn off a couple of lights at set times. Can they be combined into 1 automation? My gut tells me that they can’t be… but I wanted to ask since if possible it would greatly cut down on the number of automations to build.

Here is the code:

- id: "Bathroom lights on at 5:30 weekdays"
  alias: 'Bathroom lights on at 5:30 weekdays'
  trigger:
    - platform: time
      at: '05:30:00'
  condition:
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
    - service: light.turn_on
      data:
        entity_id: light.bathroom_1,light.bathroom_2
        brightness: 255

- id: "Bathroom lights off at 8:10 weekdays"
  alias: 'Bathroom lights off at 8:10 weekdays'
  trigger:
    - platform: time
      at: '08:10:00'
  condition:
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
  - service: light.turn_off
    data:
      entity_id: light.bathroom_1,light.bathroom_2

Thanks for your input!
Matt

I would just add an appropriate "delay: ‘02:40:00’ " to the first action automation then turn off the lights.

so…

  action:
    - service: light.turn_on
      data:
        entity_id: light.bathroom_1,light.bathroom_2
        brightness: 255
    - delay: '02:40:00'
    - service: light.turn_off
      data:
        entity_id: light.bathroom_1,light.bathroom_2
1 Like

That seems much more elegant… would one be able to reference sunrise + X minutes in a similar fashion?

I have so many sets of on at a certain time and off at a time or sunrise + and the opposite for evening. I am trying to figure out how to cut down on the sheer number of automations and the places they need changed when I update them.

I at least have them broken down into yaml files by room or device under an included automation directory.

You can use scenes or groups and more conditions like sun below horizon or above, sunset, sunrise, also an input_boolean based on a device tracker (so the lights don’t come on at all if you are away). So lots of options but you need to decide what works best for you and your family :slight_smile: and your sanity when trying to maintain it all :stuck_out_tongue:

I get a similar thing when switching lights on and off. Its the same automation but just reversed.

- alias: Study - lights on
  trigger:
    platform: state
    entity_id: switch.study_lights_switch
    to: 'on'
  action:
    service: light.turn_on
    entity_id:
      - light.ikea_of_sweden_tradfri_bulb_e26_opal_1000lm_fe8c1227_1
      - light.ikea_of_sweden_tradfri_bulb_e26_opal_1000lm_fe481ca1_1

- alias: Study - lights off
  trigger:
    platform: state
    entity_id: switch.study_lights_switch
    to: 'off'
  action:
    service: light.turn_off
    entity_id:
      - light.ikea_of_sweden_tradfri_bulb_e26_opal_1000lm_fe8c1227_1
      - light.ikea_of_sweden_tradfri_bulb_e26_opal_1000lm_fe481ca1_1

you may be able to replace both of those with one by only using state change (so remove the to: on) and then toggle the lights.

1 Like

@mattlward try this:
add both triggers and modify the template section to your days/times

Closes/opens living room blinds

  • id: curtains_shades_automation
    alias: Curtains shades automation
    trigger:
    • platform: sun # close shades week
      event: sunset
      offset: -05:00:00
    • platform: sun # close shades weekend
      event: sunset
      offset: -03:00:00
    • platform: sun # open shades
      event: sunset
      offset: -00:15:00
    • platform: time # close curtains
      at: ‘21:30:00’
      action:
    • service_template: ‘{% if now().weekday() in (6,0,1,2,3) and now().hour >=11 and now().hour < 14 %} homeassistant.turn_off {% endif %}’
      entity_id: switch.shades
    • service_template: ‘{% if now().weekday() in (4,5) and now().hour >=14 and now().hour < 17 %} homeassistant.turn_off {% endif %}’
      entity_id: switch.shades
    • service_template: ‘{% if now().weekday() in (0,1,2,3,4,5,6) and now().hour >=17 and now().hour < 19 %} homeassistant.turn_on {% endif %}’
      entity_id: switch.shades
    • service_template: ‘{% if now().weekday() in (0,1,2,3,4,5,6) and now().hour >=21 and now().hour < 22 %} homeassistant.turn_off {% endif %}’
      entity_id: switch.curtains

Ok, I can follow that up to the templates. But it looks to me like the platform statements do actions on their own and the templates fine tune and add actions that are at different times and days? So the platforms happen every day at the given times…

Ok, add both trigges, time for on and off
on the templates do
service_template: ‘{% if now().weekday() in (0,1,2,3,4,5) and now().hour >=5 and now().hour < 6 %} homeassistant.turn_on{% endif %}’
entity_id: light.bathroom_1,light.bathroom_2
service_template: ‘{% if now().weekday() in (0,1,2,3,4,5) and now().hour >=8 and now().hour < 9 %} homeassistant.turn_off{% endif %}’
entity_id: light.bathroom_1,light.bathroom_2

Great idea. Let me test this out. There is a possibility of the lights and switch falling out of sync with each other, I will see if I can figure something out.

I’m a bit reluctant at using delays for anything but short intervals, as any restart will make HA forget about the pending action and leave the light/fan/heater on.

1 Like

In which case it makes sense to have independent automations. The only other concept would be to allow HA to suspend and resume automations on restart.

Very valid point…

I have 2:30 hr delays that are no problem, however I haven’t tried much over that so give it a try and see what happens :slight_smile:

Do you mean that if you do a restart during an automation, the automation continues?

I don’t think anyone is worried that delays in automations don’t work, just that some of us tinker with config alot of many restarts a day can be common :slight_smile:

I am a tinkerer, I could have sworn that I tested this case. The timer may have restarted after the reboot. Doesn’t HA retain state thru reboot?

I wouldn’t expect the automation to continue over a restart as the trigger conditions will have changed and I doubt HA can be expected to continue an automation that’s could already be over by the time you restarted :stuck_out_tongue:

Hi!
How can I combine simple switch in one automation?

- alias: OFF
  initial_state: true
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f2a813
      click_type: long_click_press
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: light.bathroom_light
      state: 'on'
  action:
    service: light.turn_off
    entity_id: light.bathroom_light


- alias: ON
  initial_state: true
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f2a813
      click_type: long_click_press
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: light.bathroom_light
      state: 'off'
  action:
    service: light.turn_on
    entity_id: light.bathroom_light

use the service “light.toggle” and ditch the condition.