Hi this projoct is most wanted and most searched project type. I’am a retired man but I like programming. My carrier is, economics and software development but now I’m reteried old man
but this codes work !
Our goal:
A program that turns the lights (or relay) on X hours before sunset or Y hour after sunset, and also turns the lights off Z hours before sunrise or T hour after sunrise.
Main problems:
There are not enough components, in fact there are but no one that does this job easily
You need to try different ways to reach your goal. While everyone wants the same thing, I couldn’t find a complete solution in my research.
Requirements:
an ESP board with wifi and internet connection, of course
a relay
connection and power cables, power supply and some coffee
Main features:
espHome setting: minimum req, works with internet connected wifi and ESP-01
HA independent (for standalone work, but everyone know HA is the easy way to achieves our goal)
time sensitive,
full automatic
easy use for users
easy customize
settings can be controlled via web interface, or HA or both of them
only esphome’s built in components, no extra or other, or github or component or script used
can be invert funciton, like if you want to close relay when sunset and open relay on sunrise, easy with one switch (Invert func)
1- time: (input lat, long in yaml)
---->
2- sun: get info from time: → output sunrise, sunset time, evelation and azimuth
---->
3- datetime template gets sunrise and sunset time → and calculate relay open and close time delay with number: component even with delay
----->
4- no one can’t get calculated time (HH:MM) with normal properties like, .state value for this reason use middle in the man strategy, we use another datetime template and some lambda, first ones push data to second one, second one is push text template to dispaly turn off time to user at UI,
----->
5- datetime component on_time: section open relay or close with state of (TRUE/FALSE) switch: wich is run close or open at the time like INVERT func
Conclusion:
I tested and works, Does enyone interest this type of code
That’s a lot of code! Lots of debug clauses scattered everywhere indicates you spent quality time on this. Glad it works for you.
I didn’t understand a few of the Turkish terms but Google translate will take care of that.
Check your spacing in the yaml code for your esp8266, around the wifi section to see if it is the cut-and-paste forum spacing indentation that is an issue or in your code.
Check breaking changes in the change log for the November 2025 HomeAssistant updates to see if the state value functionality needs a tweak.
Once you have ascertained the values for each of your four cities, “Ankara”, “İstanbul”, “İzmir” and “Adana”, such as latitude, longitude etc, do you have to extract them again each time your code is run, or did you do that so you can make it easier to add more cities in your choices?
We have 81 cities in our country, all cities lat, long coordinates are set in YAML file, to avoid confussion I did not sent to you. And esp8266 can’t handle this size code very well. then I add every item in select , to avoid mess, & memory problems, separate setting rule with cities names first letter
Useally, While wirting code,allways I use turkish terms for parameter & varibles. This easy can track logic and never look to mind, what is the ‘close-time-hour’. Addittonally using english laguage for coding is advantage to non native english speakers, bucause mind allways use native language for logic decissions. At least I think this way. If I used Turkish terms for variables, it shows that I put a lot of effort into these variables
I hate yaml and python typing rule and structure. you right about spaces in my yaml. But who cares white spacess. The compiler does not take them seriously while compiling anyway.
By the way, I always like wrote useless applications. But I wrote & algorithim this code for reel benefits of people.
It is amusing to ask people who have migrated to my country, some for many years, which language do you count in, inside your mind, when you are adding up? Yes, it takes are very long time and lots of practise to become truly multilingual, and Google translate has come a long way to make it easier.
Unfortunately yaml code can be fussy with spacing where you least expect it. From many years of coding, I know the missing comma, the wrong indentation can cause lots of grief and hair pulling. Programmers cursing is an universal attribute.
Don’t underestimate your capabilities. What seems trivial and useless for you may be the missing link for others to make their lives empowered and enjoyable. This is where documentation and comments in the code, like you have done, make it far easier to follow, understand, debug, modify, and adapt, language limitations and all.