How to obtain sensor with relative_time to input_datetime?

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:

{{ states.input_datetime.ikea_button_2_battery.state }}  # For debugging
{{ relative_time( states.input_datetime.ikea_button_2_battery.state )}}
{{ relative_time( as_timestamp(states.input_datetime.ikea_button_2_battery.state) )}}
{{ relative_time(states.input_datetime.ikea_button_2_battery.last_changed )}}

But they lead to the wrong things

2020-11-12
2020-11-12
1605135600.0
19 minutes

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:

- platform: template
  sensors:
    ikea_button_1_battery:
      friendly_name: Ikea button 2 battery relative changed
      value_template: "{{ relative_time(states.input_datetime.ikea_button_2_battery.last_changed )}}"
      icon_template: mdi:clock-outline
1 Like

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.

{{ relative_time(strptime(states('input_datetime.ikea_button_2_battery'), '%Y-%m-%d')) }}

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.

- platform: template
  sensors:
    ikea_button_1_battery:
      friendly_name: Ikea button 2 battery relative changed
      device_class: timestamp
      icon_template: mdi:clock-outline
      value_template: >
        {{ strptime(states('input_datetime.ikea_button_2_battery'), '%Y-%m-%d') }}
5 Likes

Nice. I did not know that.

Was that part of the recent template listener changes?

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.

1 Like

That worked, thanks for your help!

For now I used:

- platform: template
 sensors:
   ikea_button_1_battery_relative_timestamp:
     friendly_name: Ikea button 2 battery relative changed
     device_class: timestamp
     icon_template: mdi:clock-outline
     value_template: >
       {{ strptime(states('input_datetime.ikea_button_2_battery'), '%Y-%m-%d') }}

And for lovelace:

type: entities
entities:
  - entity: sensor.ikea_button_1_battery_relative_timestamp

I will later try to get this in a custom:multiple-entity-row card, but I doubt that will work :frowning: