I am trying to figure out how to use an if statement (or some other method) to have lights turn on at a different time in the morning based on the day of the week. For instance, the code below is what I currently implemented that turns the lights on every morning at 5:25 AM. But I’d like the lights to turn on at 5:25 AM during the weekdays (Mon-Fri) and 6:30 AM on the weekends (Sat-Sun).
- id: TurnOnTPL
alias: 'Turn On TPL'
initial_state: 'on'
trigger:
platform: time
at: '05:25:00'
action:
service: homeassistant.turn_on
entity_id: group.tpl
I’ve searched the forum, but I only see individuals asking about using an “if statement” for dimming the lights based on the time of day.
I’m sure it is simple, but any help would be appreciated.
Or don’t use a condition as that will mean multiple automations.
Have an automation run at say 05:24 every day, have it generate a random number 1 to 120 (or whatever) have it then delay for that period, switch the light on, wait 30 mins, and turn it off again.
Sorry just re-read you want weedday/weekend
You need z template, let me get to a computer (20 mins)
Yep I would normally agree but look at his ‘first post’
The automation is nicely formatted and ordered and though he has used the AE he’s tidied it and managed to post it correctly formatted.
Okay, here you go, it’s just like your first code but it has a delay with a template in it.
Basically it fires at 05:25 and if it’s a weekday (0=mon, 1, 2, 3, 4=fri) it will set the delay to 0 if it’s NOT mon-fri it will set the delay to 55 65 minutes (just as you asked)
You will need to copy and paste this in ‘Over’ your old automation and do it via an editor (notepad, atom etc. This assumes you have samba or can use ssh to then use nano)
- id: TurnOnTPL
alias: 'Turn On TPL'
trigger:
- platform: time
at: '05:25:00'
action:
- delay: >
{% set offset = '00' if now().weekday() in (0,1,2,3,4) else '65' %}
"{{ '00:' ~ offset ~':00' }}"
- service: homeassistant.turn_on
entity_id: group.tpl
Note that your old indentation was ‘slightly’ wrong (all spacing should be in multiples of two)
Also I’ve made your trigger and actions into lists (so you can add more (worry about that later))
I edited the above as the difference wasn’t 55 but 65 (yes, Ive had 300 minutes in these slots before now)
Well I actually have an automation that turns a switch on at different times weekdays or weekends and also if it’s every 4th sunday a different time again … so it works and it’s flexible enough if there are other conditions added and it will work if HA restarts. Generally delays longer than a minute or so are a bad idea.
Why? I used to have 6 automations to control one switch but was able to streamline them into one with this kind of approach. Why would you want to use more than 1 if you don’t have to?
Eh for something like this I guess it doesn’t bother me. I do like yours though.
Plus this was in the context of someone who (appears) to have recently started using Home Assistant. Templates might not be the most friendly thing to dig into yet. Just my opinion, not saying your way is wrong.