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
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 and your sanity when trying to maintain it all
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.
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.
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
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