Hey guys, so for about a year I’ve been running 2 separate automations below to turn my lights on and off between times and I’d very much like to simply that into a single automation. Can someone point me in the right direction as I can’t seem to find what I need by simply searching?
- alias: Turn Downstairs Lights on Between 11:00 and 19:00
hide_entity: true
trigger:
- platform: homeassistant
event: start
- platform: time
at: '11:00:00'
condition:
- condition: time
after: '11:00:00'
before: '19:00:00'
action:
- service: switch.turn_on
entity_id: switch.lights_ds
- alias: Turn Downstairs Lights off Between 19:00 and 11:00
hide_entity: true
trigger:
- platform: homeassistant
event: start
- platform: time
at: '19:00:08'
condition:
condition: time
before: '10:59:55'
after: '19:00:05'
action:
- service: switch.turn_off
entity_id: switch.lights_ds
It’s called 'Turn Downstairs Lights on Between 11:00 and 19:00 ’ but it will only turn on the lights at one time, 11:00, because of the single Time Trigger. The condition is superfluous because the time trigger only fires at one time.
The only purpose, that I can think of, for the inclusion of the homeassistant.start trigger is for the case where Home Assistant is stopped before 11:00 and restarted after 11:00. This situation would cause the Time Trigger to miss its appointed time (11:00) but the start trigger will fire and ensure the light is turned on. Is that why you added the homeassistant.start trigger?
So the basics is at 11am the light will turn on when the server is running, it will then turn on at any given point between the times of 11:00 and 19:00 if the server had to restart. So it was redundancy that I created because sometimes there are power failures or I needed to restart the server.
Thank you so much, the light has now turned on at the tested time of 11:12!
I just want to do a test for the off state now, I’ve looked under Templates and I see now().minute, but unsure how to add that to run a temporary test to say the light will be on between 11 and then off after 11:25.
Edit: So it did turn the light on after 11:12 and then off after 13:00, which is how I had it set for testing purposes.
My next question which my two automatons did cover before was a situation of, if the power is off say from 18:00 to 19:30 and the server restarts, the automation to turn the lights off would kick in as it’s after 19:00, I don’t see how this one would be able to do that?
So on from 11:00 to 11:30 and off the rest of the time?
This will still work as the automation triggers on HA start as well, this means once HA is started again, it checks the time, and turns off the light if it’s after 19:00 or before 11:00.
@Burningstone I’ve just started working with templates for myself and I managed to merge my AMP turning on and off into one automation now thanks to your help
- alias: Turn on Amp when TV is on
initial_state: 'on'
trigger:
- platform: numeric_state
entity_id: sensor.hifi_draw
above: 110
for:
seconds: 3
- platform: numeric_state
entity_id: sensor.hifi_draw
below: 110
for:
seconds: 3
action:
- service_template: >
{% if states('sensor.hifi_draw') | float > 110 %}
switch.turn_on
{% else %}
switch.turn_off
{% endif %}
entity_id: switch.AMP
Returning to this, I do have a perfectly neat working automation:
- alias: Turn US Lights on Between 11:00 and 19:00
hide_entity: true
trigger:
- platform: homeassistant
event: start
- platform: time
at: '11:00:00'
- platform: time
at: '19:01:00'
action:
- service_template: >
{% if now().hour > 18 or now().hour < 11 %}
switch.turn_off
{% else %}
switch.turn_on
{% endif %}
entity_id: switch.lights_us
What I’m wondering is it possible to add something like a “&& now().minute 1”… something along those lines (I have of course tried this with my limited python knowledge to no avail", so if the hours is above 18 and minutes are above 1, i.e. the parameters are between 11:00 and 19:02 then.