I need a automation that turn on one switch at 21pm and turn off at 6am, i have an example, but is with 2 automations and i want all in just one automation. this is a example of 2 automations:
With regards to the logic behind it all, 2 automations is the easiest.
If you want a single automation, youâll have to have 2 triggers (one for each time)
and set some service_template that takes into account the trigger, extracts its value to make your condition and call the correct command.
It makes it difficult to troubleshoot when you have issues, so although I know itâs less text to author 1 single automation, itâs actually much faster to have 2 just like youâve done.
My 2 centsâŚ
There is another option which is to use switch.toggle (as descibed in the docs). But I agree with @lolouk44 just stick to 2 automations, itâs way easier.
To answer your first question, do you mean at 9:15 p.m. instead of 9:00 p.m.? If so, just change '21:00:00' to '21:15:00'.
Regarding it working at 21h but not at 6h, I guess Iâm not sure how that is possible. Are there any errors? Are you sure the automation is still turned on at 6h? Could there be something else running at 6h trying to turn the switch on? I would think this would either work for both times or neither.
trigger.now is the datetime object that caused the automation to trigger. See details here. A datetimeâs hour attribute is just that, the hour part of the time. So, trigger.now.hour will be 21 when the automation triggers at 21:15:00, and it will be 6 when the automation triggers at 06:00:00. And the template {{ 'on' if trigger.now.hour == 21 else 'off' }} evaluates to âonâ when trigger.now.hour is 21, and evaluates to âoffâ when trigger.now.hour is not 21 (e.g., when it is 6.)
FWIW, I use this exact construct in one of my automations:
automation:
- alias: Period of Day
trigger:
- platform: time
at: '05:45:00'
- platform: time
at: '22:00:00'
action:
service_template: >
{% if trigger.now.hour == 5 %}
script.pod_morning
{% else %}
script.pod_night
{% endif %}
which works just fine.
And just to be sure, I tested the service_template in the template editor:
{% set trigger = {'now': strptime('21:15:00','%H:%M:%S')} %}
switch.turn_{{ 'on' if trigger.now.hour == 21 else 'off' }}
{% set trigger = {'now': strptime('06:00:00','%H:%M:%S')} -%}
switch.turn_{{ 'on' if trigger.now.hour == 21 else 'off' }}
results in:
switch.turn_on
switch.turn_off
So, Iâm at a loss as to why this isnât working for you. If none of my previous questions help you determine why it isnât working, then I guess you should just stick with the two automations that do work for you.
Hi all,
I just changed the first code segment to read as below. Makes editing a few seconds quicker by using the second to trigger the event and allows better switching for anything down to 1 mimute events.
Seems to work reliably for me so farâŚ
#######################################
##### Timed events
###### Use the seconds to control the switch. "== 1 is ON, == 0 is OFF"
- alias: 'Livingroom Lights on 07:05-08:30'
trigger:
- platform: time
at: '07:05:01'
- platform: time
at: '08:30:00'
action:
service_template: "switch.turn_{{ 'on' if trigger.now.second == 1 else 'off' }}"
entity_id: switch.lights
To follow up on my posts above (about the use of trigger.now), the behavior of that variable changed with HA release 0.81. See this post for more details.
i came across this thread while i was looking for exact the same automation.
So i felt very happy
But when i apply the section for this automation, the following error will be emitted: 2019-11-27 09:52:47 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 91). Please check the docs at https://home-assistant.io/integrations/automation/
My configuration looks as follows:
- alias: SomeLighting
trigger:
- platform: time
at: "17:00:00"
- patform: time
at: "22:30:00"
action:
service_template: "switch.turn_{{ 'on' if trigger.now.hour == 17 else 'off' }}"
data:
entity_id: switch.somelighting
If i remove the second time trigger, the error will be gone. Running hass 0.102.1 on a pi within a virtualenv.
Hope anybody can help.
Your error is telling you where to look. Itâs saying it cannot find âplatformâ. Looking at the code you posted, your platform for 22:30:00 is misspelled.
in regards to errors:
the last most item in the series of words data['trigger'][1]['platform'] is the missing key. In regards to yaml:
I noticed all the examples used the âatâ expression. At 9pm turn light on, and at 6am turn light off, but what happens if there is a reboot at 10pm?
Is there a way to set up an animation to turn switch on if time is after 9pm, and turn switch off if time is after 6am but before 9pm?
Thanks for the suggestion. I found a similar work around to what you mentioned. I am using two automations, one to turn on and the other to turn off.
The turn on automation has two triggers (only one has to be met for the automation to run). One trigger is at start up, the other trigger is at 06:00:01 . Condition uses the after 6am before 9am parameters. This way at one second after 6am the trigger checks the conditions, notices it is after 6am so it turns on the switch.
At reboot (re start) it always checks the conditions. If within the window switch turns on, otherwise switch remains off.
The second automation to turn off the switch is much more basic. It just runs everyday at 9am and turns switch off.
I was able to set up those automations without writing any Yaml code. I just used the autmoations section in HA. For other switches (watering switches) seems I will have to do a bit of yaml coding since the UI for automations doesnât support every other day option.
That would create an endless loop of triggers. Because it would be âtrueâ every second during that time. You could stop that by making an input Boolean and use it as a condition. Has to be off to pass, and set it On in the action. Define it as off on restart that way on a restart the automation would pass.