Combine automations to turn light off at different time depending on day of week

Hi

I am just starting out with Home Assistant but want to do it correct from the start to avoid lots of unnecessary automations that will clog up my system.

I currently have 2 automatons set up to turn off my kitchen lights. first automation turns them off a 7pm on Wednesday, Saturday and Sunday. The second automation turns off the same lights at 7:30pm on Monday, Tuesday, Thursday and Friday


alias: Kitchen Diner Lights Off Wed/Sat/Sun
description: ""
trigger:
  - platform: time
    at: "19:00:00"
condition:
  - condition: time
    weekday:
      - wed
      - sat
      - sun
action:
  - service: light.turn_off
    data: {}
    target:
      device_id:
        - 3b9f8e36f9fed64f4c0826a40fa99d14
        - cf06f537fc927526a7015ece0971fbbb
mode: single


alias: "Kitchen Diner Lights off Mon/Tue/Thur/Fri "
description: ""
trigger:
  - platform: time
    at: "19:30:00"
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - thu
      - fri
action:
  - service: light.turn_off
    data: {}
    target:
      device_id:
        - 3b9f8e36f9fed64f4c0826a40fa99d14
        - cf06f537fc927526a7015ece0971fbbb
mode: single

The 2 automations are above but this seems like a lot of code just to turn lights off at different times on different days. I have tried to copy code from a different post and wondered if the below would work.


- id: Kitchen Diner Lights off
  alias: 'Kitchen Diner Lights off'
  trigger:
    - platform: time
      at: '19:00:00'
  action:
    - delay: "{{ '00:00:00' if now().weekday() in (0,1,3,4) else '00:30:00' }}"
    - service: light.turn_off
      data: {}
      target:
        device_id:
          - 3b9f8e36f9fed64f4c0826a40fa99d14
          - cf06f537fc927526a7015ece0971fbbb

To use the delay is an option, but if you want you can also do it fully in UI:

alias: Kitchen Diner Lights Off Wed/Sat/Sun and the rest
description: ""
trigger:
  - platform: time
    at: "19:00:00"
    id: wedsatsun
  - platform: time
    at: "19:30:00"
    id: therest
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - wedsatsun
          - condition: time
            weekday:
              - wed
              - sun
              - sat
        sequence:
          - service: light.turn_on
            data: {}
            target:
              device_id:
                - 3b9f8e36f9fed64f4c0826a40fa99d14
                - cf06f537fc927526a7015ece0971fbbb
      - conditions:
          - condition: trigger
            id:
              - therest
          - condition: time
            weekday:
              - mon
              - tue
              - thu
              - fri
        sequence:
          - service: light.turn_on
            data: {}
            target:
              device_id:
                - 3b9f8e36f9fed64f4c0826a40fa99d14
                - cf06f537fc927526a7015ece0971fbbb
mode: single

There are many ways to accomplish your goal, but it is best to avoid prolonged delays and waits whenever possible.

- id: Kitchen Diner Lights off
  alias: 'Kitchen Diner Lights off'
  trigger:
    - platform: time
      at: '19:00:00'
      variables:
        days: "{{ [0, 1, 3, 4] }}"
    - platform: time
      at: '19:30:00'
      variables:
        days: "{{ [2, 5, 6] }}"
  condition:
    - "{{ now().weekday() in days }}"
  action:
    - service: light.turn_off
      target:
        device_id:
          - 3b9f8e36f9fed64f4c0826a40fa99d14
          - cf06f537fc927526a7015ece0971fbbb

Hi

Thank you for the response. i can see the trigger ID’s but there doesn’t seen to be any lines to target the actual days?

Cheers

Hi

Thanks for the response, when I pasted this into HA I got an error:

Message malformed: extra keys not allowed @ data[‘0’]

Cheers
Dan

You would need to add something like the following:

  - choose:
      - conditions:
          - condition: trigger
            id: wedsatsun
          - condition: template
            value_template: '{{ now().strftime("%a")|lower in "wedsatsun" }}'



Check the indents in the action section.

@Didgeridrew thanks for the updates, sorted the indents and that solved it

1 Like

Sorry…added