I’ve been trying to convert an Epoch date to a format that has the exact same properties as the entities listed here: System Monitor - Home Assistant
The benefit of systemmonitor entities is they are stored as a date like “April 29, 2023 at 11:28 AM” but when adding to an entity card they display as “2 weeks ago”
For my epoch date, I have successfully format it to any datestamp I could think of, but when adding to an entity card it displays the date itself, rather than “2 weeks ago”
This is my current query (it’s from a template in the Rest API)
@petro, I think I got this from you, but I am not able to locate that post, documentation, etc.
{{ ((now() | string | as_datetime | string) [2:] ) }}
# [2:] removes the first 2 characters
{{ ((now() | string | as_datetime | string) [:2] ) }}
# [:2] removes everything right of the 2nd character
Can anyone point to where the string [2:] or string [:2] is documented? I am currently trying to learn how to parse and merge results back together. Normally, I would read a book, but there are none available on YAML. I would appreciate if anyone can give me additional syntax or links.
Thanks to All in advance
The : is the slicing operator. So, as per the comments, [2:] skips the 0th and 1st characters in the string and give you everything from the 2nd to the end. The end is the length of the string and implied when no end index is given.
@parautenbach, Thanks for the reply. I have reviewed your link, thanks again! I am new to python, and had no idea what language the Template was calling. So, if I understand correctly, that slicing operator is developed to work with a dimensional array. HA doesn’t support arrays, correct?
what would be the python to midstr$ of an element. extracting the 4th to the 9th characters?
Thanks!
As D explained (while I was sleeping), the templating language is Jinja. It’s linked to from the template editor under the dev tools. There’s a close relationship between Jinja and Python. I just find the Python docs a bit more comprehensive in some cases — like this one.
Since you’re on the learning path: It does, but it depends. Objects such as dictionaries and arrays (lists) can be stored in sensor attributes. States are always strings, so you need to cast it as necessary (even though they might look like numbers or datetimes).
Need to parse only hour and minutes, nothing else… otherwise status will be allways “False” as date will be not correct… How to get from both values only hour:minutes?
I’m trying to figure out how to use the Entity or Entities card to set seconds on an input_datetime entity.
When I add the entity to the entities card I get an input field that only shows HH:MM, but the entity is made up of HH:MM:SS. I can make changes to the seconds using the States developer tool, but I’m trying to keep everything on one card.
I’m trying to do this because I have a working dual rate electricity meter (based on HA Glow) and I’ve got 2 sets of meter readings working perfectly and staying in sync with whats on the meter. I’ve noticed a slight drift due to the fact that meter is synced to its own clock which drifts from internet time by some 30 seconds a month. At the moment this is controlled by changing the trigger times in a Energy Meter Helper, but I’ve like to put all the offsets in one place (Entities Card).
Any guidance or comments would be really appreciated.