jcwillox / lovelace-canary

🐤 Adds many useful extensions to lovelace, such as templating secondary info, stacking within a card and more!
MIT License
97 stars 4 forks source link

Please help with formatting to Relative_time #19

Closed Stooovie closed 2 years ago

Stooovie commented 2 years ago

Hi, Im using Canary to get a last-completed date/time of a different entity.

entity: xxx
secondary row:
  entity: sensor.xiaomi_vacuum_cleaner_last_clean_end

It works properly:

2021-11-05T10:07:40

but I'm unable to format it into a nice relative time (either "11.5. 10:07" or "xx days/hours ago"). The sensor entity is "sensor.xiaomi_vacuum_cleaner_last_clean_end".

I have asked on HA forums but to no avail. I'm not a programmer and templating is just inscrutable for me. Thanks!

jcwillox commented 2 years ago

Well templating is the only real way to make that happen, but luckily it's quite easy with Home Assistant's Jinja2 templates, something like the following should work. You could also use Jinja to format the time however you want.

# ...
secondary_info: {{ relative_time(as_datetime(states("sensor.home_assistant_uptime"))) }}
Stooovie commented 2 years ago

Thank you, it works. What threw me off was that I had to convert he sensor state to datetime first before getting the relative time.

jcwillox commented 2 years ago

Awesome, yeah I forgot that as well, was confused for a hot second 😁