Time pattern switch toggle - turn off last activation

Hello
I have a VMC machine that exchanges air in a room.
I will like it to run every 20 minutes each hour from a given time in the morning to a given time in the evening.
I made a simple automation that toggles the VMC switch every 20 minutes, it works fine but I will like it to turn off the switch at the end of the activation period.
How can I accomplish this task?
Here is my automation

alias: turn on VMC every 20 min
description: ""
trigger:
  - platform: time_pattern
    minutes: /20
condition:
  - condition: time
    after: "07:00:00"
    before: "21:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - service: switch.toggle
    metadata: {}
    data: {}
    target:
      entity_id: switch.vmc_switch
mode: single

You could add another automation that uses a time trigger to trigger at 21:00:01 and switch off the switch. This is the simplest way.

Or you could add that trigger to your existing automation and choose what action to take based on the trigger that occurred.

Something like this:

alias: toggle VMC every 20 min
trigger:
  - platform: time_pattern
    minutes: /20
    id: toggle
  - platform: time
    at: "20:59:00"
    id: turn_off
condition:
  - condition: time
    after: "07:00:00"
    before: "20:59:30"
action:
  - service: switch.{{ trigger.id }}
    target:
      entity_id: switch.vmc_switch
mode: single
3 Likes

Seems fine, Testing it

Thanks!

There was an error: the second id should be turn_off not off. Edited above.