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().

Sorry for replying so late. I had a lot of other things in mind and on my todo list.

What I called “time period”.

Yeah I know that I can set a switch when booting the device and also by running some kind of cron every minute or so.

But what I thought is that something like time periods should be something, almost everybody would love and use, when it comes to use cases like this.

Like “switch 1 is ON from 7:00 to 23:00” or “switch 2 is OFF from 23:00 to 7:00”.

You would set one statement and everything is done. when you restart the device, it gets checked as soon as the devices has a valid date/time. And also it gets checked when I manually turn change the state of the switch.

That’s what I’m having in mind here. :slight_smile:

Esphome is event based, it would waste resources to run
if (now.hour >= 7 && now.hour < 23)
on every loop cycle, while in reality you only need to check that every x minutes (10-100k loop cycles).

Okay I understand.

So my solution is to program it twice?

First as a “normal” event, when a certain time is hit. And second every minute (which would be enough for my needs) in a certain time period, to avoid problems when the device was rebooted just when it usually had to turn on/off/whatever a switch.

This didn’t work for you?

Thank you! I’m sure this will work. I just wanted to make sure that this is how I would handle it best in ESPHome.

It’s not necessarily best, on_connect automation would waste resources even less (just fire once when esphome gets time).
In any case it works only if esp has valid system time. For situations it doesn’t, you should make some failback… Like “if no valid time, use interval”.

Yes, I am planning on using a RTC device. Which is already connected and should work.

If you want to run something like a salt water aquarium with such a device, there should be some fail over implemented. Also, as written, I do not want to rely on HA, as the network needs to work, HA needs to work and so on.

It has to work without everything except power.

If the time differs some minutes, not so important. But if the lights are off, the corals are dead after one or two days. Yes, either we are at home or the fish sitter comes along once per day. But then we are on holiday and cannot fix everything.

This is what I have setup:

esphome:
  on_boot:
    - priority: 800
      then:
        - ds1307.read_time: # read the RTC time once when the system boots

time:
  - platform: ds1307
    id: ds1307time
    update_interval: never
    on_time:
      - seconds: 0
        minutes: 55
        hours: 8
        days_of_week: MON-SUN
        then:
          - switch.turn_on: relay1
      - seconds: 0
        minutes: 30
        hours: 9
        days_of_week: MON-SUN
        then:
          - switch.turn_off: relay1
  - platform: homeassistant
    id: homeassistanttime
    on_time_sync:
      then:
        ds1307.write_time: # update RTC on successful synchronization

By principle, did I miss something or does this look okay?

No, it doesn’t look like what you were asking in the first place. If there’s power outage during 8:55, your light will stay off until next day.

Sorry, I meant with using time from RTC and HA and also syncing it. I will add the code from above to it.

But when I take a second look, I need to sync from time to time (once per day for example) HA to RTC. As at the moment it only gets synced once initiating HA time. If I understand it correctly.

Btw, how can I achieve minutes, too? Like 11:30 to 20:15.

It’s not important if you have 1307 time or HA time or SNTP time as long as you have time. And Esp keeps the time well enough for months without additional synch. Only case to investigate is that power goes off when automation was supposed to trigger.

Nohow with this simple “trick”.
But you could do it with interval component or just on_connect or on_boot automation.

1 Like