Making a robust lighting timer

Want to set up a dependable lighting cycle for an aquarium: on for 4 hours at midday, off for 2 hours, then on for 4 hours. Have some lights plugged into Tapo plugs.

I know the obvious way to do this with automations (at 12:00, switch on), but wondering if there’s a more bullet-proof method which will periodically check that the lights are doing the ‘right’ thing.

Have a time sensor set up, but not sure if this will be effective or efficient - will this just be triggering constantly…?!

Something like:

alias: 'Timer: Aquarium Lights On'
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.time_formatted
    above: '12.00'
    below: '16.00'
    id: aq_period_01
  - platform: numeric_state
    entity_id: sensor.time_formatted
    id: aq_period_02
    above: '18.00'
    below: '22.00'
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id:
        - switch.tapo_15
        - switch.tapo_16
mode: single

Or is it better to simply have another round of Automations that just check ‘if light off and between 12:00 and 16:00, switch light on’?

Maybe add a trigger on the light as well, and add time to conditions.
If, for any reason, the light is switched off, it would be turned on again?

At what times do you want to set the switch to on and `off’?

Is this what you want:

  • On at 12:00 and 18:00.
  • Off at 16:00 and 22:00.

Exactly. Two periods of four hours on, with two hours off between, or:

On: 12:00 - 16:00
Off: 16:00 - 18:00
On: 18:00 - 22:00
Off: 22:00 - 12:00

Yes. Good plan, though I don’t know if this would cover any HA downtime, for example. I know there’s been some debate about how, or if, HA can ‘catch-up’ with any offline events…

Just add homeassistant.start to the triggers and that should cover it.

This automation will set the switches according to your desired schedule. It will also set them correctly whenever Home Assistant is restarted. That’s useful should Home Assistant ever go offline at any of the scheduled times. When it starts again, the automation will ensure the switches are set to the correct state.

alias: 'Timer: Aquarium Lights On'
description: ''
trigger:
  - platform: time
    at:
    - '12:00:00'
    - '16:00:00'
    - '18:00:00'
    - '22:00:00'
  - platform: homeassistant
    event: start
condition: []
action:
  - service: "switch.turn_{{ 'on' if (12 <= now().hour < 16) or (18 <= now().hour < 22) else 'off' }}"
    target:
      entity_id:
        - switch.tapo_15
        - switch.tapo_16
mode: single

NOTE

You might find this tutorial interesting:

2 Likes

Oh, that’s nice. Makes sense. Thank you!