WTH is entity history not stored with iso time

Iso time seems to be equal to my local time? The time stored with the entities is something like cet-wintertime. Now it is difficult for me to purge all sensor history but keep the last value of the day for example or group by day with a sql-tool

Or do I misunderstand something ?

Are you sure itā€™s not UTC itā€™s stored as?
That is very common when you use dates and times in programming because it gives you a timezone without DST and can easily be converted.

Thats indeed the correct term for it I suppose. I can understand why it is stored this way, but when using this db for other aplications it is a pain in the *****. When using Grafana for example, but also when I want to clean up the db too.So it would be nice when local time was stored too. Losing one of the other time stamps would not be a problem for me, because is seems that the fields ā€œlast_changedā€ en ā€œlast_updatedā€ have the same value.
Strange thing : these values have an earlier timestamp than the value in field ā€œcreatedā€

The problem with local time is the DST.
Sure you can store it that way but then comes the headache of outputting it.
Especially if you want to compare one time against another, and perhaps compare it against a different countries time, which may have a different DST date.
You are asking to go against all wisdom that has been collected during the past 50-ish years.

I highly doubt you will get anywhere in this.

The ā€œrulesā€ are:
-store in UTC.
-compare in UTC.
-when you output you add the local timezone and DST.

Since you are pulling data from the database yourself then you are the one who should add timezone and DST if you need it.
That is always a ā€œfrontendā€ thing.
In the ā€œbackā€ everything is in UTC.

I doubt this will ever change. Storing with Unix time is always favored in every application because you only need to convert to local. Imagine if you move 1 time zone away, youā€™d need to know the context of the timestamp to convert to that local time, and it would cause you to rewrite most things. Where Unix time is universal for all time zones.

What issues do you have with grafina? Maybe itā€™s worth a wtf to fix that workflow with HA instead of focusing on the time stamps in the database.

1 Like

I donā€™t doubt there are good reasons to store it the way it is. The only thing is it gives me dififculties to do the things I want to do :slight_smile: . Perhaps there is a fix for Grafana but that does not solve the main problem which is selecting the last value of the day. When you take a look at my screenshot you can see that the state changes from 5.54 at 2020-09-01 21:58:53.080849 to 0.00 at 2020-09-01 22:00:00.143042, that is because it changes from local day t to local day t+1.
I will try to make a join between last_changed and ISO-time, or perhaps there is a function that adds 2 hours to ā€œlast_updatedā€ Thank you for your answers

Isnā€™t that when the local time became next day?
And the thing you are trying to extract (based on the name) resets to 0 at midnight I guess?
So that means everything is correct?
The problem is that you are in the ā€œbackā€ using ā€œfrontā€ times.

What happens if you query on ā€œbackā€ times?

That is what i meant with t and t+1, sorry for not being clear.
The value doesnā€™t allways reset to 0 for all sensors. Some sensors are allways accumulating.

I donā€™t understand what you mean by "back"times. Do you mean google that word or using it in an SQL-query.?

When you are developing in the back that is when you are using a backend software.
You are doing queries, building the framework, displaying the core of an app.
Then there is the frontend developer. They populate the empty spaces left by the backend developer to fill in numbers.

You are now in the back. You are doing the queries.
That is done using backend data, meaning UTC timezone.
Generally if you have an ā€œappā€/frontend to do queries in the times are converted to UTC before it goes in the query.

I donā€™t use grafana but I assume you are in direct contact with the database thus you need UTC timezones.

Pretty much every database in the world stores time as UTC times. Iā€™m sure thereā€™s ways to do queries using local time and have it work properly.

Hereā€™s a google search for ā€œSQL Query local timeā€

Not sure if thatā€™s what your after, but thatā€™s the first google search I did. Iā€™m sure with a little google foo you can find your answer if that one doesnā€™t work for your goal.

I am sure there must be something to convert it to local time. Thank you

1 Like

For those who might be interested: itā€™s very simple :slight_smile:
This gives the correct result for example, query in sqlite:
Select * ,
datetime(last_changed, ā€˜localtimeā€™) as localdatetime
from states
where entity_id = ā€˜sensor.balance_daily_powerā€™
order by (datetime(last_changed));

Thank you all for pointing me into the right direction.