Basic config for turnins a switch off to on or on to off based on a sensor

Hi there guys,

I have a switch i want to have turn off or on based on the state of a sensor. I have this automation as a starting point but dont know how to make it go back on if the sensor changes back to clased.

alias: Central Heating Polly's Room
description: if window open, vent closes
trigger:
  - platform: state
    entity_id:
      - binary_sensor.contact_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition: []
action:
  - type: turn_off
    device_id: 460889b2f597950b24625c79eeea0441
    entity_id: 7eab1d56c83ea1bea799d6dda29410ef
    domain: switch
mode: single

Simplest way is to write another automation with the trigger changed to: 'off' and the action changed to turn on the switch.

alias: Central Heating Polly's Room
description: if window open, vent closes
trigger:
  - platform: state
    entity_id:
      - binary_sensor.contact_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "off"
  - platform: state
    entity_id:
      - binary_sensor.contact_sensor_door
    to: "off"
    id: "on"
condition: []
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.entity_name
mode: single

Replace the switch entity_name to yours.

Thanks Ledge!