Single automation to turn on/off a switch in two different time periods

Hello all,

I’m trying to do an automation that will turn on and off my switch in two different time periods: from 13 to 15 and from 22 to 6. At first I decided to make two automations and the result is that first one is working great (ON from 13 till 15) but the second one isn’t even starting (I checked and both automations are turned on). Is there a possibility to write the code in one automation? What am I doing wrong?

- alias: 'Załączamy Gniazdko Łazienka 230VAC pomiędzy 13:00 - 15:00'
  trigger:
    - platform: time
      at: '13:00:00'
    - platform: time
      at: '15:00:00'
  action:
    service_template: "switch.turn_{{ 'on' if trigger.now.second == 1 else 'off' }}"
    entity_id: switch.xiaomi_miio_switch

and

- alias: 'Załączamy Gniazdko Łazienka 230VAC pomiędzy 22:00 - 06:00'
  trigger:
    - platform: time
      at: '22:00:00'
    - platform: time
      at: '06:00:00'
  action:
    service_template: "switch.turn_{{ 'on' if trigger.now.second == 1 else 'off' }}"
    entity_id: switch.xiaomi_miio_switch

- alias: 'Załączamy Gniazdk' 
  trigger:
    - platform: time
      at: '06:00:00' 
    - platform: time
      at: '13:00:00'
    - platform: time
      at: '15:00:00'
    - platform: time
      at: '22:00:00' 
  action:
    service_template: "switch.turn_{{ 'on' if now().hour in [13, 22] else 'off' }}"
    entity_id: switch.xiaomi_miio_switch

Wow, thank You for fast respond - I will test the updated code from You right away :slight_smile:

1 Like