How can i toggle a switch to turn on and off every 15 minutes between two times?

I would like to turn on a switch at 09:00 and then have it toggle every 15 minutes on/off. Then at 19:00 i want to turn that switch off.

I have written this but it does not work, and i assume it wont know if the swtich is on or off at 9am, which is a problem because i dont want the switch to be left on all night.

- id: heatingleftbedroomday
  alias: Left Bedroom Heating
  trigger:
  - minutes: /15
    platform: time_pattern
  condition:
    after: 09:00
    before: '19:00'
    condition: time
  action:
  - data:
      entity_id: switch.sonoff_second_house_left_bedroom
    service: switch.toggle

Any help appreciated

- id: heatingleftbedroomday
  alias: Left Bedroom Heating
  trigger:
    - platform: time_pattern
      minutes: '/15'  
  condition:
    - condition: time
      after: '08:59'
      before: '19:01'
  action: 
    - service: "switch.{{ 'turn_on' if now() == today_at('9:00') else 'toggle' }}"
      target:
        entity_id: switch.sonoff_second_house_left_bedroom

Many thanks !

If i wanted to change this so that it only comes on 15 minutes every hour is that possible? Perhaps by adding a delay to the toggle?

Two automations would be simplest.

- id: heatingleftbedroomday_on
  alias: Left Bedroom Heating On
  trigger:
    - platform: time_pattern
      minutes: 0
  condition:
    - condition: time
      after: '08:59'
      before: '18:01'
  action: 
    - service: switch.turn_on
      target:
        entity_id: switch.sonoff_second_house_left_bedroom
- id: heatingleftbedroomday_off
  alias: Left Bedroom Heating Off
  trigger:
    - platform: state
      entity_id: switch.sonoff_second_house_left_bedroom
      to: 'on'
      for:
        minutes: 15
  action: 
    - service: switch.turn_off
      target:
        entity_id: switch.sonoff_second_house_left_bedroom

Many thanks,

Is there then a way to group these automations? I have a list of “slide/toggle” switches on an entities card that turn on or off the automation. Seeing as this is two automations ideally would only want one switch to turn them on or off. I put the automations in a group but that does not seem to help me…

If you want to enable/disable an automation, add a State Condition that checks an Input Boolean. If the Input Boolean is on the automation is effectively enabled. If the Input Boolean is off the automation is effectively disabled.

The same Input Boolean can be referenced by multiple automations thereby allowing you to control them all at once.

Ok so i tried to make something but it throws an error in the confg when starting up:

- id: LeftBedroom Day
  alias: LeftBedroom Day Test
  trigger:
  - platform: time
    at: 08:30:00
  - platform: time
    at: 19:30:00
  action:
  - service: 'switch.turn_on'
    entity_id: switch.sonoff_second_house_left_bedroom
  - alias: "Turn off switch after 20 minutes"
    trigger:
      platform: time
    condition:
      condition: time
      after: '{{ now().strftime("%H:%M:%S") }}'
      before: '{{ (now() + timedelta(minutes=20)).strftime("%H:%M:%S") }}'
    action:
      - service: switch.turn_off
        entity_id: switch.sonoff_second_house_left_bedroom
  - alias: "Turn on switch after 40 minutes"
    trigger:
      platform: time
    condition:
      condition: time
      after: '{{ (now() + timedelta(minutes=40)).strftime("%H:%M:%S") }}'
      before: '{{ (now() + timedelta(minutes=60)).strftime("%H:%M:%S") }}'
    action:
      - service: switch.turn_on
        entity_id: switch.sonoff_second_house_left_bedroom

Any ideas why?

The time trigger requires an at: time to be specified.

The time condition before: and after: options don’t accept templates.