wow, this really is practically the only topic I could find calculating a ‘last’ day. Sorry to revive, but since its really connected to your post here, Id rather not take it elsewhere.
using a generic DST template currently, which iterates over all days in a given range (spanning more than a year to catch some variance), would it be possible to adapt this to find the next 2 ‘last Sunday’ occurrences in month March and October.
Since these are the fixed dates for DST change, that might be more efficient? only 2 months to check, finding each last Sunday?
just having changed DST, I would like the template to output October and March next.
Otoh, If it wouldn’t be much faster, there s is no use?
Why did you switch from the DST template I gave you 2 years ago? It’s still working for me and calculates the transition down to the minute. It’s also light weight for it’s operations.
It searches ahead for the change in the day, then it searches ahead for the hour in the day, then the minute. At most it’ll be 365 + 24 + 60 iterations. In reality, most DSTs change within 182 days, 2 hours and 0 minutes. Making it around 184 iterations.
I’m not sure how you could have had ‘difficulty’ with them when HA does all the work. Also, keep in mind that all timestamp sensors are like this, i.e. ones that come from integrations that you have no control over.
EDIT: And once you create the sensor, you’ll see the T is in the state too. HA does this beyond the template.
The old template is wrong and does not work. You can’t trust it. The new template works. As for the entities card, that’s what HA does with timestamps, that has nothing to do with the template itself.
the template is correct…
Keep in mind that 2am is 3am when DST is not applied. So the timestamp is correct it just doesn’t say ‘3am’, however the number of days, hours, and minutes from now to dst is correct.
If your template is returning a timestamp that should be displayed in the frontend (e.g., as a sensor entity with device_class: timestamp), you have to ensure that it is the ISO 8601 format (meaning it has the “T” separator between the date and time portion). Otherwise, frontend rendering on macOS and iOS devices will show an error. The following value template would result in such an error:
Yes, but you’re not understanding what HA does under the hood.
device_class:timestamps REQUIRE a datetime object being returned. Not a string. This is a requirement. So using isoformat() will result in an error in the logs. After the template is resolved, HA will take the datetime object and format it in isoformat. Just look at the state of the sensor in developer tools instead of arguing. You’ll see that the state has a T in it. The template in the template editor will not have the T because it hasn’t been resolved by the template sensor code.
No because you aren’t using isoformat. You’re just outputting the datetime as a string. Remember device_class: timestamp has extra steps to turn datetimes into iso formatted datetime strings. Attributes do not, so you have to do it.
It does not work, it will be off by an hour. There is no way to get the correct time of the second transition due to the nature of DST. It will always be off by 1 hour.
EDIT: Well there probably is a way but it would be a ton of work for no gain. You’d have figure out your next loss/gain and inverse that value on the following loss/gain.