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