no problem.
just be aware that you need to add at least an "alias: " to that for it to work.
no problem.
just be aware that you need to add at least an "alias: " to that for it to work.
also if anything happens in that 8 hours the second part of the automation won’t be triggered as the delay will be cancelled…
You can still group it into one automation if you like with something like this:
- alias: Turn light on and off at preset times
trigger:
- platform: time
at: '21:00:00'
- platform: time
at: '05:00:00'
action:
- service_template: >
{% if now().hour == 21 %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
entity_id: light.my_light
Another typical enhancement is to deal with HA restarts. (Otherwise, if HA is not running at one of these times, the light won’t be changed.) So something like this:
- alias: Turn light on and off at preset times
trigger:
- platform: time
at: '21:00:00'
- platform: time
at: '05:00:00'
- platform: homeassistant
event: start
action:
- service_template: >
{% set n = now() %}
{% if n.hour >= 21 or n.hour < 5 %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
entity_id: light.my_light
This works great. how do you put Monday, Tuesday, Wednesday, Thursday, Friday
The following automation turns on the light at 21:00 and turns it off at 5:00 only on Monday to Friday (ISO Weekdays 1 through 5). Also handles the situation where Home Assistant might be turned off at the scheduled start and/or stop times and is restarted later.
- alias: Scheduled Light
trigger:
- platform: time
at:
- '21:00:00'
- '05:00:00'
- platform: homeassistant
event: start
condition: "{{ 1 <= now().isoweekday() <= 5 }}"
action:
- service: "light.turn_{{ 'on' if now().hour >= 21 or now().hour < 5 else 'off' }}"
target:
entity_id: light.whatever
This is what I did and It works. Thank you
- id: '000000000000000000'
alias: Turn Off Light/fan at 6:30
trigger:
- platform: time
at: 06:30:00
condition:
- condition: time
weekday:
- mon
- wed
- fri
action:
- service: switch.turn_off
data: {}
target:
entity_id:
- switch.my_fan
- switch.my_light
initial_state: true
mode: single
Please format your code with the </> button. See point 11 here.
You don’t need the after:
and before:
lines in the condition.
As shown above, it can be done with a single automation that’s shorter than the example you posted that only turns the lights off. Plus it can handle missed events such as when Home Assistant might be offline at 06:30. The example you posted will fail to turn off the lights if Home Assistant is offline at the scheduled time and won’t turn them off after it is running again.
You might be interested in this topic:
@123 , Thanks for this code, is it possible to add minutes to the time? e.g
@123 , thanks for your quick reply, I have read the documentation already .
I am using 2 different automations, 1 to off a light and another to on it at a certain time but I like your version which merged on and off together in one automation, it also include days condition.
e.g
- alias: Scheduled Light
trigger:
- platform: time
at:
- '21:30:00'
- '05:30:00'
- platform: homeassistant
event: start
condition: "{{ 1 <= now().isoweekday() <= 5 }}"
action:
- service: "light.turn_{{ 'on' if now().hour >= 21:30 or now().hour < 5.30 else 'off' }}"
target:
entity_id: light.whatever
The following template is incorrect and will not work the way you want.
now().hour
produces an integer value (a whole number between 0 and 23). It cannot be compared to 21:30
(the comparison will always evaluate to false
).
now().hour
can be compared to a floating-point number like 5.3
but that value is not equivalent to the time 5:30
.
I suggest the following which compares time represented as a tuple. In this case, the tuple contains two items where the first represents the hour and the second is minutes. So the time string 21:30
is represented as the time tuple (21,30)
.
action:
- variables:
t: "({{ now().hour }},{{ now().minute }})"
- service: "light.turn_{{ 'on' if t >= (21,30) or t < (5,30) else 'off' }}"
target:
entity_id: light.whatever
Thanks so much, just what I wanted.
Regards
Also if you want to trigger every (for example) 20 minutes instead of at a specific time (useful if your conditions are complicated):
trigger:
- platform: time_pattern
alias: check every 20 minutes
minutes: "/20"
(edit: thanks to Taras - updated the original post to make sure it is correct)
Your example triggers at 20 minutes after the hour, every hour.
00:20
01:20
02:20
03:20
etc
If you use /20
it will trigger for any minute in an hour that is evenly divisible by 20.
00:00
00:20
00:40
01:00
01:20
01:40
02:00
02:20
02:40
03:00
etc
thanks for that correction - you’re right it should be:
trigger:
- platform: time_pattern
alias: check every 20 minutes
minutes: "/20"
Hi,
I need an advice with my automation. I have a script which I want to run twice a month (1st and 15th of each month), but but for both days on different time (i.e. 1st day run at 9:00am, 15th day run at 12:30).
here is my code:
- id: '1712514481790'
alias: ATM_QNAPOFF
description: ''
trigger:
- platform: time
at: 09:00:00
- platform: time
at: 12:30:00
condition:
- condition: template
value_template: >-
{{ ( now().day == 1 and now().hour == 12) or ( now().day == 15 and now().hour == 9) }}
action:
- service: script.qnap_off
data: {}
- service: notify.whatsapp_maros
data:
message: 'QNAP device is shutting down'
mode: restart
The automation wont start. It looks that there is an issue with second time trigger but I can’t figure out…can somone help?
try quoting your time
- platform: time
at: '12:30:00'
and your hours are switched between your post and automation
You can specify multiple times in the trigger:
trigger:
- platform: time
at:
- "09:00"
- "12:30"
condition:
- "{{ (now().day,now().hour) in ((1,9),(15,12)) }}"
action:
- service: script.qnap_off
data: {}
- service: notify.whatsapp_maros
data:
message: 'QNAP device is shutting down'
mode: restart
Also shortened the condition including the use of shorthand notation.