Turn on light at different times depending on Day

Dang - Lots of good info in this thread. You guys are getting to the crux of why I asked my question. I have several automations that could be streamlined just like @Mutt and @DavidFW1960 are talking about. I figured if I start small with the weekend/weekday switches, I can figure out how to adjust my other automations. I will give these a try and report back.

Thanks for the advice on formatting my code too.

Not sure we needed to give you any advice on that end.
I only wish more people would submit code cleaned and presented as yours
But yes, about 80% of my first pass mistakes are spacing so check, check oh and check :smile:

Just a note: I screwed up the second template. I changed it to > 4 instead of 5. 0-4 are weekdays, 5-6 is weekend so if it was >5 it would only trigger when d=6 sunday. >4 will work for the weekend.

Bugger, I noticed that but then got caught up on the or condition.
Sorry David we could have killed two birds with one stone

Ha! I was lying in bed trying to sleep when I thought of this last night. lol. Sometimes I think my short-term-memory is a problem… other times not so much…

Just as an example… here is one of my most complex automations to turn my coffee machine on…

- id: '1507434167220'
  alias: Coffee Maker On
  trigger:
  - platform: time
    at: '07:00:00'
  - platform: time
    at: '07:15:00'
  - platform: time
    at: '08:00:00'
  - platform: time
    at: '12:00:00'
  - platform: time
    at: '19:00:00'
  - platform: time
    at: '20:00:00'
  - entity_id: person.liz
    from: 'not_home'
    platform: state
    to: 'home'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'input_boolean.homeandawayauto'
        state: 'on'
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '07:00' and now().weekday()
              < 5  and (states('input_boolean.holiday') == 'off') }}"
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '07:15' and ((as_timestamp(now())
              - as_timestamp('2017-09-24 00:00:00')) / 86400)|int % 28 == 0 }}"
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '08:00' }}"
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '12:00' and ((as_timestamp(now())
              - as_timestamp('2017-09-24 00:00:00')) / 86400)|int % 28 == 0 }}"
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '19:00' and (states('input_boolean.overtime') == 'off') }}"
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '19:00' and now().weekday() > 4 }}"
          - condition: template
            value_template: "{{ now().strftime('%H:%M') == '20:00' and (states('input_boolean.overtime') == 'on') and now().weekday() < 5 }}"
          - condition: and
            conditions:
              - condition: template
                value_template: "{{ now().weekday() < 5 }}"
              - condition: time
                after: '18:00:00'
                before: '19:00:00'
              - condition: state
                entity_id: person.liz
                state: 'home'
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.sonoff12914
  - condition: and
    conditions:
      - condition: template
        value_template: "{{ now().weekday() < 5 }}"
      - condition: time
        after: '18:00:00'
        before: '19:00:00'
      - condition: state
        entity_id: person.liz
        state: 'home'      
  - service: notify.david_ios_notify
    data:
      message: 'Coffee Maker On'
      title: 'Liz is home'

So on weekends, I want it to come on at 8am in the morning
On weekdays, I want it on at 7am in the morning.
Every 4th Sunday, I want it to come on at 7:15am (and another automation turns it off at 8:30) Then I want it back on at 12pm midday.
Every day I want it on at 7pm again (unless wife doing overtime or if she comes home before 7)

These only fire if my input_boolean Home and Away is on… so if we are going away I turn the input_boolean off and it won’t fire. The next conditions check the time/day and also detect 4th sunday (calculated from an arbitrary point in time that was a sunday I wanted to be a 4th) Next, I have another input_boolean that is on if my wife is working overtime so it then comes on later than 7pm Lastly, weejkdays, when she comes home between 6 and 7pm it will switch on immediately.

Action also sends a notification for when the Wife state is the trigger. (So I know what time it turned on - it takes 30 minutes for coffee maker to warm up before I can make coffee)

So everything is in the one automation to turn that device on. I have a separate automation to switch it off at set times on certain days. Goal is I never have to manually touch the device.

Thank you Sir @DavidFW1960. Your suggested automation without delays worked perfectly this morning. I have to read more about “template.”

Also - that coffee automation is bananas, but well thought out.

1 Like

Hey ! That’s ‘Minion’ Copyrighted ! :crazy_face:

1 Like

It started off very simple but I added stuff… hope it gives you a few ideas and shows you what is possible.

Hello Davie your template is great, I was wondering if you could help me i am doing the same but with pir sensors that turn on lights,
pir detect motion from 6:30 to 21:00 mon-fri
and 9:00-22:00 sat and sun
This is what i have so far but dont know how to keep the automation runing to the end time

  • id: ‘1600548919557’
    alias: Alansbedroom pir turn light on
    description: Alansbedroom pir turn light on
    trigger:
    • device_id: 0f0df3f055834e3685a7ee72f42b8f48
      domain: binary_sensor
      entity_id: binary_sensor.alansroom_pir
      platform: device
      type: motion
      condition:
    • condition: state
      entity_id: light.alans_light
      state: ‘off’
    • condition: and
      conditions:
      • condition: state
        entity_id: alarm_control_panel.konnected_alarm
        state: disarmed
      • condition: or
        conditions:
        • condition: template
          value_template: ‘{{ now().strftime(’‘%H:%M’‘) == ‘‘07:45’’ and now().weekday()
          < 5 }}’
        • condition: template
          value_template: ‘{{ now().strftime(’‘%H:%M’') == ‘‘09:00’’ and now().weekday()

          4 }}’
          action:

    • data: {}
      entity_id: light.alans_light
      service: light.turn_on

Hi, Welcome

As you are actually after a new solution to a slightly different problem. it would be best if you started a new thread with an appropriate title so members can apply themselves to it.
Also, when solved, other looking for similar can view and adapt your solution.

This expands the body of knowledge we have, and will allow the use of more recent code structures etc.
By all means tag David in your first post to that thread like this @Ian85 (but using David’s username.

Good luck with your issue

Hi,

How can I turn on a light based on a state of the day before?
eg. if yesterday thee light was ON at 11:00 PM today don’t turn on at 11:00 PM

thanks

I would think you need to use a helper and base you automation off that.