With a single Automation, how do you turn lights on between two set times and then the lights off?

Hey guys, so for about a year I’ve been running 2 separate automations below to turn my lights on and off between times and I’d very much like to simply that into a single automation. Can someone point me in the right direction as I can’t seem to find what I need by simply searching?

 - alias: Turn Downstairs Lights on Between 11:00 and 19:00 
    hide_entity: true
    trigger:
      - platform: homeassistant
        event: start
      - platform: time
        at: '11:00:00'
    condition:
    - condition: time
      after: '11:00:00'
      before: '19:00:00'
    action:
      - service: switch.turn_on
        entity_id: switch.lights_ds 
        
  - alias: Turn Downstairs Lights off Between 19:00 and 11:00
    hide_entity: true
    trigger:
      - platform: homeassistant
        event: start
      - platform: time
        at: '19:00:08'
    condition:
      condition: time
      before: '10:59:55'
      after: '19:00:05'
    action:
      - service: switch.turn_off
        entity_id: switch.lights_ds

You couls use a template in the action part to determine if the switch should be turned on or off. Something like this:

- alias: Turn Downstairs Lights on Between 11:00 and 19:00 
  hide_entity: true
  trigger:
    - platform: homeassistant
      event: start
    - platform: time
      at: '11:00:00'
    - platform: time
      at: '19:00:00'
  action:
    - service_template: >
        {% if now().hour > 18 or now().hour < 11 %}
          switch.turn_off
        {% else %}
          switch.turn_on
        {% endif %}
      entity_id: switch.lights_ds
1 Like

Help me understand your first automation.

It’s called 'Turn Downstairs Lights on Between 11:00 and 19:00 ’ but it will only turn on the lights at one time, 11:00, because of the single Time Trigger. The condition is superfluous because the time trigger only fires at one time.

The only purpose, that I can think of, for the inclusion of the homeassistant.start trigger is for the case where Home Assistant is stopped before 11:00 and restarted after 11:00. This situation would cause the Time Trigger to miss its appointed time (11:00) but the start trigger will fire and ensure the light is turned on. Is that why you added the homeassistant.start trigger?

So the basics is at 11am the light will turn on when the server is running, it will then turn on at any given point between the times of 11:00 and 19:00 if the server had to restart. So it was redundancy that I created because sometimes there are power failures or I needed to restart the server.

Thank you!

I’m getting an error though:
“while scanning for next token found character ‘%’ that cannot start any token”

I think the indentation was wrong with this one (typed it on the phone), I corrected it in my post, can you please try again?

1 Like

I don’t get any errors now but the automation didn’t fire at 11 though.

I realized now that I mixed up turn on/off. I corrected my post again.

1 Like

Thank you so much, the light has now turned on at the tested time of 11:12!

I just want to do a test for the off state now, I’ve looked under Templates and I see now().minute, but unsure how to add that to run a temporary test to say the light will be on between 11 and then off after 11:25.

Can you please tell me the exact timeframes when the light should be on and when it should be off?

On only between 11-11:30

Edit: So it did turn the light on after 11:12 and then off after 13:00, which is how I had it set for testing purposes.

My next question which my two automatons did cover before was a situation of, if the power is off say from 18:00 to 19:30 and the server restarts, the automation to turn the lights off would kick in as it’s after 19:00, I don’t see how this one would be able to do that?

So on from 11:00 to 11:30 and off the rest of the time?

This will still work as the automation triggers on HA start as well, this means once HA is started again, it checks the time, and turns off the light if it’s after 19:00 or before 11:00.

Ok perfect, thank you

Is your server not protected from power outages by a UPS?

@Burningstone I’ve just started working with templates for myself and I managed to merge my AMP turning on and off into one automation now thanks to your help :slight_smile:

 - alias: Turn on Amp when TV is on
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: sensor.hifi_draw
        above: 110
        for:
          seconds: 3
      - platform: numeric_state
        entity_id: sensor.hifi_draw
        below: 110
        for:
          seconds: 3
    action:
      - service_template: >
          {% if states('sensor.hifi_draw') | float > 110 %}
            switch.turn_on
          {% else %}
            switch.turn_off
          {% endif %}
        entity_id: switch.AMP

Try something like this:

- alias: Turn Downstairs Lights on Between 11:00 and 19:00 
  hide_entity: true
  trigger:
    - platform: homeassistant
      event: start
    - platform: time
      at: '11:00:00'
    - platform: time
      at: '11:30:00'
  action:
    - service_template: >
        {% set time_now = as_timestamp(now()) | timestamp_custom('%H:%M') %}
        {% if time_now > '10:59' and time_now < '11:30' %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: switch.lights_ds

Didn’t work

I don’t see a flaw in Burningstone’s automation so what was the test you performed that caused it to fail?

Returning to this, I do have a perfectly neat working automation:

 - alias: Turn US Lights on Between 11:00 and 19:00 
    hide_entity: true
    trigger:
      - platform: homeassistant
        event: start
      - platform: time
        at: '11:00:00'
      - platform: time
        at: '19:01:00'
    action:
      - service_template: >
          {% if now().hour > 18 or now().hour < 11 %}
            switch.turn_off
          {% else %}
            switch.turn_on
          {% endif %}
        entity_id: switch.lights_us

What I’m wondering is it possible to add something like a “&& now().minute 1”… something along those lines (I have of course tried this with my limited python knowledge to no avail", so if the hours is above 18 and minutes are above 1, i.e. the parameters are between 11:00 and 19:02 then.

I’ve read this three times and it makes no sense, your times in the triggers do not match the times in the action so how will they ever act together ?

Also I’m not sure what you are aiming at with the && now() bit

You say you want the times between 11:00 and 19:02
So why not adopt @Burningstone 's solution and amend the times accordingly ?