I’m learning Python and developing Home Assistant integrations and would like to understand what the proper way is to deal with time and dates in sensor attributes.
Is there a special format for timestamps that I should use?
What is the right way to format the timestamp data when I display this attribute in Lovelace?
How can the Lovelace display consider the locale settings of the user?
Currently, the integration formats the timestamp to what I would like to see in the frontend, including locale / language setting. My feeling is, that this is not the best way to do it, as it hardwires everything. However, putting in some sort of generic timestamp leaves me with the challenge of getting the formating right in the frontend, especially if I would like to use existing cards.
I couldn’t find any pointers in the development docs. Any insight?
You should be using Datetime objects for attributes or the state if you’re using device_class: timestamp. Just make sure the datetime object is timezone aware.
if you’re forced to use a string, use .isoformat() from the datetime object, which outputs to the ISO8601 standard that tom_i pointed out.