Can I have one automation for switch schedule with both on and off parameters?

I have a turtle tank with basking lights plugged into a smart outlet plug. I would like to lights to turn on at sunrise and turn off at sunset. My current automation will turn lights on but not off :frowning:

Is it possible to get this one automation to do both on and off?

- id: '1634102183865'
  alias: Action - Switch - Schedule - Lucy Tank
  description: ''
  trigger:
  - platform: sun
    event: sunrise
  condition: []
  action:
  - service: switch.turn_on
    target:
      entity_id: switch.lucy_tank
  - wait_for_trigger:
    - platform: sun
      event: sunset
  - service: switch.turn_off
    target:
      entity_id: switch.lucy_tank
  mode: single

Since 2021.7:

- id: '1634102183865'
  alias: Action - Switch - Schedule - Lucy Tank
  description: ''
  trigger:
  - platform: sun
    event: sunrise
    id: 'on'
  - platform: sun
    event: sunset
    id: 'off'
  condition: []
  action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.lucy_tank
  mode: single
2 Likes

Nice one :wink:

- id: '1634102183865'
  alias: Action - Switch - Schedule - Lucy Tank
  description: ''
  trigger:
    - platform: state
      entity_id: sun.sun
  condition: []
  action:
    - service: "switch.turn_{{ 'on' if trigger.to_state.state == 'above_horizon' else 'off' }}"
      entity_id: switch.lucy_tank
  mode: single

EDIT

Correction. Changed below_horizon to above_horizon as per tom_l’s comment below.

1 Like

Shouldn’t that test be for above_horizon?

They want the lights on during the day.

1 Like