Turn on light at different times depending on Day

Hello,

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.

Add a time condition in the current automation that includes only weekday: with Monday through Friday.

Then create a second automation with a time trigger at 6:30 and a weekday time condition that includes Saturday and Sunday.

E Z P Z :wink:

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)

For a beginner, an automation with templates might not be the most friendly solution. Feel free to still share of course.

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)

I’d do this:

- id: TurnOnTPL
  alias: 'Turn On TPL'
  initial_state: 'on'
  trigger:
  - platform: time
    at: '05:25:00'
  - platform: time
    at: '06:30:00'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '05:25' and now().weekday() < 5 }}"
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '06:30' and now().weekday() > 4 }}"
  action:
   service: homeassistant.turn_on
   entity_id: group.tpl
4 Likes

heck, we could even make it a one line template

- id: TurnOnTPL
  alias: 'Turn On TPL'
  trigger:
    - platform: time
      at: '05:25:00'
  action:
    - delay: "{{ '00:00:00' if now().weekday() in (0,1,2,3,4) else '01:05:00' }}"
    - service: homeassistant.turn_on
      entity_id: group.tpl
1 Like

David, That would work but why would you do that ?

delays suck if for any reason HA restarts.

1 Like

I still like my two automation idea :stuck_out_tongue:

3 Likes

apart from delays sucking?

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.

1 Like

Tediore, yes your two automations would work, would be simple to maintain and within the grasp of most newbies :smiley:

David, I agree about the restarts but how likely is that at ‘ungodley’ hour in the morning ? :man_shrugging:

Mainly I just love the idea that my second offering is 9 lines long the same as the OP’s :crazy_face:

So I agree there are issues with mine, ultimatelty it’s down to the OP

Power failures… any number of reasons but unlikely I agree. It is still a bad idea to use delays in automations unless there is no other way.

It’s also one of my favourite times to do HA updates so I’d get caught all the time with that delay.

Don’t know why I bother quoting preformatted it comes out really ugly

David the docs say that format for OR is : -

condition:
  condition: or
  conditions:
    - condition: state
      entity_id: 'device_tracker.paulus'
      state: 'home'
    - condition: numeric_state
      entity_id: 'sensor.temperature'
      below: 20

Are you sure yours is correct ?

2 Likes

Yep, that adjustment is needed

It is now :slight_smile:
I copied it out of an automation with and’s as well… I fixed it as well as the formatting in case OP copies/pastes.

1 Like

Always good practice.
The less cutting and pasting of duff code the better :+1:

1 Like