Time periods for switches

Hello,

is there a way to define time periods for a switch to be on our off? Like 11am to 8pm on and 8pm to 11am off. I only found on_time, which seems to act just cron like, which is okay in most situations, but not every.

Background: I want to use a 16 relay ESP32 to run my salt water tank. If something is up like power loss, reboot of the ESP, time sync problem or whatever I want to make sure that a switch e.g. for the lights is also turned on or off after the switching time like in my example at 11am or 8pm. Everything else is an additional risk I want to avoid.

Thanks and kind regards

If it is important you should create and run the automation on the ESP32. HA can call the automation and the esp32 can run the automation at startup. Time could be an issue but I dont know about that.

Basically make the action function independent of HA state.
Make it fail into the safest position to prevent damage until problem fixed.

I want. And will. The idea was only having HA as a logging component and for changing switches manually. Maybe running the same automations and setting switches to bei 200% sure. But the ESP should run completely without HA and/or network. I also have a RTC1307 attached.

Yes. I also noticed that I can set restore options for the switches. But yet I do not have an idea if the hardware is capable. I need to check/test.

Anyway, the question remains: Can I set a time period for switches which also triggers when it is after the time the switch should turn on or off?

I’m not sure if the most elegant way to achieve your goal, but you could look at combining a interval trigger with a lamda based time condition and the switch action.

https://esphome.io/components/interval/

You can make on_boot automation to check the actual time and turn the switch on/off accordingly.

Another option would be on_time trigger lets say every 5min for the whole on or off period.

on_time:
      # Every 5 minutes
      - seconds: 0
        minutes: /5
        hours: 11-20
        then:
          - if:
              condition:
                switch.is_off: my_switch
              then:
                - switch.turn_on: my_switch

In practice you want to add also some condition for manual control.

1 Like

Thank you for all the ideas and examples.

So there is really nothing out-of-the-box to achieve this? If not, I would definitely go down this path to achieve my goal.

I’m asking because this sounds like a standard use case to me. :man_shrugging:

Yeah sure. I have to think about how to add this. :slight_smile:

Out of the box in what sense? You can build whatever automations with conditions id(time).now().