Save now() to a input_datetime and use with relative_time()?

I’m trying todo what seams like it should be simple. I want to preserve a date time value past reboots so I created an input_datetime which I’ve tried to set to different values, both datetime and timestamp and the take its value in a template and use it in relative_time(), but it just won’t work. If I access .last_updated it works, so it must be something with the value.

Could someone please point me in the right direction on how to achieve this?

Set value

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.ip_last_changed
data:
  datetime: "{{ now().strftime('%Y-%m-%dT%H:%M:%S%z') }}"

This is the template:

last changed {{ strptime('states.input_datetime.ip_last_changed.state', '%Y-%m-%dT%H:%M:%S%z', 0)}} ago

I’ll reply to myself and hopefully someone find this useful at some point.

This works.

service: input_datetime.set_datetime
  target:
entity_id: input_datetime.ip_last_changed
  data:
timestamp: "{{ now().timestamp() }}"
last changed 
{{ relative_time(states.input_datetime.ip_last_changed.state | as_datetime) }} 
ago
5 Likes