Automation with time

Hello,

I newbie here, i want automation turn on light on at 21:00:00 then turn off at 05:00:00 everyday,
how should i do config ? i don’t need trigger anything, just turn on and turn off everyday

Thanks

Write an one automation to turn the lights on and another to turn them off. Use time triggers.

Here is a full example from my config:

1 Like

thanks for quick reply, is there anything to group it to 1 automation ?

Thanks you so much

you have to have a trigger. that’s what makes the automation run in the first place.

If you want it to always turn the lights on then off at exactly the same time every day then you could use one automation with a delay

trigger:
  platform: time
  at: '21:00:00'
action:
  - service: light.turn_on
    entity_id: light.your_light
  - delay: '08:00:00'
  - service: light.turn_off
    entity_id: light.your_light

thanks you so much

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
3 Likes

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
4 Likes

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

  • platform: time
    at:
    - ‘21:10:00’
    - ‘05:30:00’

Yes. The documentation includes an example of a Time Trigger containing hours and minutes.

@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
3 Likes

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