How exactly are you getting those values? Are you using the value being returned in day_length, or are you calculating them from sunrise and sunset? And if the latter, how exactly are you obtaining sunrise and sunset?
Of course everything I said depends on the website actually functioning correctly.
What I have now I just set up about four hours ago, so it hasnât even gone through a complete day cycle. I expect the values to update shortly after midnight local. Whatever happens, I can report back tomorrow night after itâs gone a full day.
ditto for today and tomorrow.
Weird thing too is that if I put the URL into a browser yesterday is correct length.
But I think what you are missing (?) is that itâs giving you the UTC day/date so if the UTC date is for the wrong day itâs going to be wrong. You arenât specifying a date so itâs always going to be today I think and correct.
Thatâs weird. What you have seems correct. I wonder if that is pointing to another issue. Are you getting any errors? What is the last_updated and last_changed for these entities? (You donât have to tell us. Just saying you might want to look at that.) Also note that you have scan_interval set to 60 minutes, which is fine, but that means that when the website does switch/shift days, it can take up to that long before your entities will show that. Just something to keep in mind.
Not true. It doesnât matter if UTC is tomorrow morning, because that translates to tonight (assuming, again, for me, weâre in the 7:00 PM to midnight local time period.) Just because the UTCâs date is tomorrowâs date, doesnât mean itâs representing tomorrow local. For me, 8:00 PM local today is the same as 1:00 AM UTC tomorrow.
Anyway, enough on that. If the website is reporting the day_length for today, then it doesnât matter whether youâre using formatted or not, or whether the sunrise and sunset are reported in local or UTC. Again, assuming the website is working correctly.
yeah I thought that might be it⌠it might have missed the 10am switchover amd might refresh at 11ishâŚ
I did change to 1 hour instead of 60 minutes too before I restarted.
but hang onâŚ
itâs getting the info for the UTC date. The UTC date will change from yesterday to today at 10am here (we are +10) so before 10AM if I execute the URL I would expect it to show a different result to if I do it after 10am because the UTC date has changed.
Interestingly, if I search for Sydney on that site, it shows totally different day lengths and sunrise/sunset times in the table! (only a few minutes different but still different)
No, itâs getting the info for the lat/lon you give. At least thatâs what I assume. The date of the UTC representation of those times doesnât matter. Itâs the date of the local time representation of those times that matters. Let me make this concrete. In your case, where the local timezone is +10:00, the sunset for your âtodayâ, represented in the local timezone, will have your local todayâs date, and the time will be what you expect. But, if you take that exact same date/time, and represent it in UTC, the date will be one less (âyesterdayâ), and the time will be +14 hours. So, e.g., 7:00 AM âtodayâ local is the same date and time as 9:00 PM âyesterdayâ UTC. It doesnât matter that the UTC date is yesterday, the date/time still represents your today. Oh well, thatâs the best I can do in trying to explain it. If we still disagree, then I guess weâll have to agree to disagree.
Yep, I definitely see that. Iâm just not sure I understand why. It doesnât make sense to me.
BTW, the fact that yesterdayâs day length didnât change at the same time as the others could simply be explained by the fact that it takes three separate exchanges to get all three values, and the yesterday one might have hiccuped.
Iâll report back what I see on my setup tomorrow. Iâll go ahead and add sensors for yesterday and tomorrow, too, to make it more like yours.
You know, I was just looking at the code for the standard sun component, and the annoying part of all of this is, it could provide the current dayâs sunrise and sunset as attributes (in addition to the others.) It seems to have all the information available to it. Whoever wrote it, though, apparently just decided not to include them.
It uses the Python astral package. And there are functions provided in homeassistant.helpers.sun that can be used to do this (pretty much the same ones that sun.py uses.) Maybe Iâll write a custom component for this. The plus is it doesnât need to query any website; it appears to be based on some data and equations. Add one to the project listâŚ
(Yeah, I know, this should be done via an issue or pull request. Still really donât know how to do that.)
Yes, thatâs kind of the whole point to this topic, right? I.e., the standard sun component does not provide the dayâs sunrise and sunset for the entire day, so that you canât just calculate the length of the day at anytime throughout the day.
So I think I owe you an apology. I was so stuck on how I thought the website should work I was explaining away the results you were seeing. You are clearly right. It seems to select the date by what the date is in Greenwich (i.e., UTC.) So I went back and looked at the documentation on the website again, and so far Iâm not sure how to get around that, at least with the REST Sensor (that doesnât allow for templates in the resource URL.)
I really think the right way to attack this problem is to enhance the standard sun component to provide the current (local) dayâs sunrise and sunset for the entire (local) day as additional attributes. Iâm going to do that by copying the standard code and turning it into a custom component. If I get that working Iâll probably post it in a new topic and reference this one. The other advantages to doing it this way is that it can update right a midnight, and it doesnât require polling any external website.
I think I might have finally found a solution to this method!
Instead of using yesterday, today and tomorrow, (for your +10:00 timezone) use â-14hoursâ, â+10hoursâ and â+34hoursâ. I havenât really tested this yet, other than using those strings in the query, but I think this has a chance of working. So, again for your timezone, the URLs become (for yesterday, today and tomorrow, respectively):
So, e.g., at your midnight, when itâs 2:00 PM the day before UTC, it will add 10 hours to UTC and get midnight UTC with the same date as your local timezone date, and therefore, should switch days at midnight local.
Iâm going to update my URLs accordingly (except, of course, since my timezone is -05:00, Iâll use â-29hoursâ, â-5hoursâ and â+19hoursâ) and see what happens at 7:00 PM tonight and at midnight!
I have a feeling this is how the website is intended to be used. Sure would be nice if they had a small note about it.
FWIW, I made a custom sun component that adds (local) todayâs sunrise and sunset as attributes which should stay constant throughout the entire (local) day. It was actually pretty simple. Iâm testing it out now and, like I said, if it seems to work Iâll probably create a new topic to share it. Iâm really surprised someone else hasnât already done this (or maybe they have and I havenât seen it.)
Itâs interesting⌠my sensors havenât ticked over today yet - still showing the same data as yesterday afternoon. All saying they were updated 16 hours ago.
I had been thinking overnight of toying with testing the UTC and local to see if they were the same day and then taking a different URL⌠basically creating 4 dummy sensors and then depending on the test creating real ones but yours is better.
Note: 10:39am here and just now yesterday and tomorrow updated but today is still old data⌠I think I might also change the refresh time to 10 minutes!
used -14,+10 and +34 and set refresh to 10 minutes and restarted. All correct now. Itâs maddening you have to hammer their api like that!!! Iâd prefer to minimize the calls but if itâs failing updating then Iâm just going to have 10 minutes!
@DavidFW1960 and @chrisw (and whoever else is interested), you can give it a try if you like. Just place a copy in a directory named custom_components in your HA config directory, and this one will take precedence over the standard component.
I know I probably should have forked it or something, but after using several other version control systems over the years, I just donât have the strength to learn yet another one!