When I change my batteries, I change an input_datetime object to the current date (has_time: false, if that matters). I would like to display the relative_time to this input_datetime in Lovelace.
I thought it had to work with one of the following configs:
The last option is semi-acceptable, but I do not want to use the last_changed option, since I sometimes change the input_datetime manually later than the specified date.
Eventually, I want to show it in a custom:multiple-entity-row card, so I want to store this in a sensor like this:
Relative time only works with datetime objects. The only thing that is a datetime object in the things that you tried is last_changed. The battery state one is a string of a date. as_timestamp() of the battery state is a float. So, if you want to get this information into a datetime object, you need to use a method that creates a datetime object. strptime does that and it’s local to your coordinate system.
However, this will not update in the frontend. So you have 2 options…
{% set t = now() %}
{{ relative_time(strptime(states('input_datetime.ikea_button_2_battery'), '%Y-%m-%d')) }}
Or you can make a template sensor using the timestamp device_class. This only works in specific UI elements though. But you don’t need to use relative_time. The UI handles the relative_time calculation.
Nope, that’s been there for a LONG time. Problem is, most of the front end doesn’t use it, which is stupid. If you’re just going to use the basic entities card, you’ll be good to go. Just plop a timestamp in the UI with device_class: timestamp on a sensor and you’re good to go.