Set time of a light to turn on and off

I’m working on setting a light that turns on at 9pm and turns off at 2:30am on 3 nights a week wed fri and sat. Will this work?

Just getting started in homeassistant so hoping for some help.

'alias: Turn on Stair Light when the top opens
description: ‘’
trigger:

  • platform: time
    at: ‘21:00:00’
    condition:
  • condition: time
    after: ‘02:30:00’
    weekday:
    • wed
    • fri
    • sat
      before: ‘21:00:00’
      action:
  • type: turn_on
    device_id: cf0b4ecc3f7da16788d36ba282f0452a
    entity_id: switch.upstairs_light_inside
    domain: switch
    mode: single’

Hello and welcome to the forum. Please edit your post and format your pasted code as per point 11 here.

As @tom_I has stated you really need to format your automations. Yaml is very picky about format and without doing this it is hard for people to help you!

I modified this from what I do for my robot vac…

alias: Turn on and off lights
description: turn off and on lights
trigger:
  - platform: time
    at: '21:00:00'
  - platform: time
    at: '02:30:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '21:00:00'
            weekday:
              - wed
              - fri
              - sat
            before: '23:59:00'
        sequence:
          - type: turn_on
            device_id:  cf0b4ecc3f7da16788d36ba282f0452a
            entity_id: switch.upstairs_light_inside
            domain: switch
      - conditions:
          - condition: time
            after: '02:30:00'
            weekday:
              - thu
              - sat
              - sun
            before: '02:31:00'
        sequence:
          - type: turn_off
            device_id: cf0b4ecc3f7da16788d36ba282f0452a
            entity_id:  switch.upstairs_light_inside
            domain: switch
    default: []
mode: single

you can make input variables that can substitute for the hard coded times and are set in the lovelace interface.

input_datetime:
  up_light_start:
    name: Upstairs Start Time
    has_date: false
    has_time: true
#   initial: 21:59:00  # <- value is set if you want it to always reset to this value on reboot
  up_light_end:
    name: Upstairs End Time
    has_date: false
    has_time: true
#   initial: 23:56:00

EDITS: Fixed a bunch of typos and omissions

No, not the way it’s currently written. There needs to be an explicit service call to turn off the light but the automation only contains a service call to turn on the light. In addition, it triggers at 21:00 but the Time Condition checks if the current time is after 02:30. Naturally, the time can’t be after 02:30 if it just triggered at 21:00 so the automation’s action will never be executed.

Try this:

alias: Scheduled Lights
trigger:
  - platform: template
    value_template: "{{ now().hour == 21 and now().isoweekday() in [3, 5, 6] }}"
    id: 'on'
  - platform: template
    value_template: "{{ (now().hour, now().minute) == (2,30) and now().isoweekday() in [4, 6, 7] }}"
    id: 'off'
action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.upstairs_light_inside
6 Likes

@123

That is incredibly elegant and brief! Awesome.

I’ve been using this automation to turn on/off air purfier at specified time which can also be controlled by two helpers.

# Air Purifier ON/OFF automation
- id: '1671329124616'
  alias: 'Automation: Air Purifier ON/OFF'
  description: Turns air purifier on/off at times specified in the helpers
  trigger:
  - platform: time
    at: input_datetime.air_purifier_on
  - platform: time
    at: input_datetime.air_purifier_off
  condition:
  - condition: state
    entity_id: input_boolean.air_purifier_automation
    state: 'on'
  action:
  - choose:
    - conditions:
      - condition: time
        before: 00:00:00
        after: input_datetime.air_purifier_on
        weekday:
        - sat
        - fri
        - thu
        - wed
        - tue
        - mon
        - sun
      sequence:
      - type: turn_on
        device_id: 323dc33f0bda5cde3de396fb6d32de67
        entity_id: switch.sonoff_s31_lite_zb_switch
        domain: switch
    - conditions:
      - condition: time
        before: '12:00:00'
        after: input_datetime.air_purifier_off
        weekday:
        - sun
        - mon
        - tue
        - wed
        - thu
        - fri
        - sat
      sequence:
      - type: turn_off
        device_id: 323dc33f0bda5cde3de396fb6d32de67
        entity_id: switch.sonoff_s31_lite_zb_switch
        domain: switch
  mode: single

HTH

# Air Purifier ON/OFF automation
- id: '1671329124616'
  alias: 'Automation: Air Purifier ON/OFF'
  description: Turns air purifier on/off at times specified in the helpers
  trigger:
  - platform: time
    at:
      - input_datetime.air_purifier_on
      - input_datetime.air_purifier_off
  condition:
  - condition: state
    entity_id: input_boolean.air_purifier_automation
    state: 'on'
  action:
  - service: "switch.turn_{{ trigger.to_state.object_id.split('_')[2] }}"
    target:
      entity_id: switch.sonoff_s31_lite_zb_switch
  mode: single

I’m unable to understand this syntax and cannot test it either in the template console.

switch.turn_{{ trigger.to_state.object_id.split('_')[2] }}

Do you mind explaining or guiding how to test it within developer tools?

Looks very interesting and may simplify several other automation for me!

You can’t test it in the Template Editor because the trigger variable only exists when an automation is triggered.

However, you can simulate it in the Template Editor like this:

{% set object_id = 'air_purifier_on' %}
switch.turn_{{ object_id.split('_')[2] }}

The split method transforms the string
air_purifier_on
into a list containing three items
['air', 'purifier', 'on']
Lists are zero-indexed so the index of the last item in the list is 2

2 Likes

Just a couple of tweaks to make this work:

alias: Scheduled Lights
trigger:
  - platform: template
    value_template: "{{ (now().hour == 21 and now().isoweekday() in [3, 5, 6] }}"
    id: 'on'
  - platform: template
    value_template: "{{ (now().hour, now().minute) == (2,30) and now().isoweekday() in [4, 6, 7] }}"
    id: 'off'
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.upstairs_light_inside

Your “couple of tweaks” to the perfectly functional automation have rendered it inoperable.

Hi there,

i´m using the code above to turn on/off the light - pretty cool.
But i don´t get the point to extend it with weekdays AND weekend like so…:

- alias: Turn plug on and off at preset times and weekdays
  trigger:
    - platform: template
      value_template: "{{ (((now().hour, now().minute) == (19,25) and now().isoweekday() in [1, 2, 3, 4, 5]) or ((now().hour, now().minute) == (19,30) and now().isoweekday() in [6, 7])) }}"
      id: "on"
    - platform: template
      value_template: "{{ (((now().hour, now().minute) == (19,30) and now().isoweekday() in [1, 2, 3, 4, 5]) or ((now().hour, now().minute) == (19,35) and now().isoweekday() in [6, 7])) }}"
      id: "off"
  action:
    - service: switch.turn_{{ trigger.id }}
      target:
        entity_id: light.kugellampe_buro

It fired at 19:25 two times with “OFF”… Puhhh…

Thanks

EDIT: I tested with dev-tools/template successfully. The conditions shows true if i gave the actual time - so…