Hi,
It looks like the attribute is not correctly fetched on this line:
{% set entity_id = 'sensor.last_updated' %}
{% set last_updated = as_timestamp(strptime(entity_id), '%Y-%m-%d %H:%M:%S')) %}
I think I need to somehow fetch the value, as now I think the entity_id is somehow not fetching the right value. The formatting parts works correctly (tried in Python console).
Hi guys, I got it to work after some fiddling, most of which was due to my own lack of understanding of how HA works.
Here is the final solution I used:
- platform: time_date
display_options:
- 'time'
- platform: template
sensors:
car_last_connected:
value_template: >-
{% set value = states('sensor.SENSORNAMEHERE') %}
{% set last_updated = as_timestamp(strptime(value, "%Y-%m-%d %H:%M:%S")) %}
{% set seconds = now().timestamp()-last_updated %}
{% set hours = seconds / 3600 %}
{% if seconds / ( 60 * 60 ) > 1 %}
{{ seconds // ( 60 * 60 ) }} hours ago
{% else %}
{{ seconds // 60 }} minutes ago
{% endif %}
Edit: Thanks so much for the help, hopefully someone else finds this useful too.
Also, a tip for others struggling with Templates: remember to use the template testing tool instead of repeatedly editing / committing / pushing / pulling your config file like an idiot (like I did).
Thanks for the reply @petro. So neither the old version or this new version you posted would retain the info after reboot? Is there any method that would?
Hi guys, I’m not a programming expert, so sorry for my dump question. It might be some really simple code I guess. I have a Shelly plug that shows the seconds since may dryer is running. I want to show the running time on my dashboard - the time needs to be converted to hours, minute and seconds. Can anyone help me on that peace of code and where I need to add it on HASS? I wasn’t successful till now.
Thanks Vic
Hi there, sorry for the necrobump.
I was wondering if you could help me with a very similar issue. I need the same thing but I cannot adapt your code to my format string that results in the state: 2023-01-26T14:13:17+00:00.
Having “x minutes ago” or “x hours ago” would be perfect for me using the sensor.time to make the calculation.
I tried something like this:
{% set value = states('sensor.bagno_ping_last_changed') %}
{% set last_updated = as_timestamp(strptime(value, "%Y-%m-%d %H:%M:%S")) %}
{% set seconds = now().timestamp()-last_updated %}
{% set hours = seconds / 3600 %}
{% if seconds / ( 60 * 60 ) > 1 %}
{{ seconds // ( 60 * 60 ) }} hours ago
{% else %}
{{ seconds // 60 }} minutes ago
{% endif %}
But I have this error as result:
ValueError: Template error: strptime got invalid input '2023-01-26T14:13:17+00:00' when rendering template '{% set value = states('sensor.bagno_ping_last_changed') %}
{% set last_updated = as_timestamp(strptime(value, "%Y-%m-%d %H:%M:%S")) %}
{% set seconds = now().timestamp()-last_updated %}
{% set hours = seconds / 3600 %}
{% if seconds / ( 60 * 60 ) > 1 %}
{{ seconds // ( 60 * 60 ) }} hours ago
{% else %}
{{ seconds // 60 }} minutes ago
{% endif %}
This is fantastic and it should be a built in feature in HA to compare two input_datetime helpers if you ask me.
Sorry for reviving the thread, but I am getting a decimal as well as brackets and quotes when comparing two time stamps – eg.- ['55.0 minutes'], and this code is a little too complex for me to figure out where I need to slice the string in order to have something that’s TTS friendly. I’ve been staring at it for a while now and I think I’m stumped.
Any chance you could help me out? Thanks in advance!
Edit:
I think I figured it out: {%- set ns.result = ns.result + [ num|float|round(0) ~ ' '~ desc ~ ('s' if num > 1 else '') ] %}
Thanks for the code in any case
Sweet! I’m sure this will come in handy in the future. I’m using your last version to tell me how long I’ve been in bed when I get up in the morning rather than uptime and I plan to set something similar up to report how long I’ve been absent from home when I go away for days or weeks at a time.
Aside from being more streamlined, are there any advantages of using your new method? I already have your previous work doing a great job with that first task so I’m wondering if I should bother replacing that older code in what I’ve done so far.
Thanks again!
I was looking how to do this and found this old thread and after a lot of troubleshooting, I figured out that you can simply use the relative_time function.
Just keep in mind that relative_time does not work with dates that are in the future. That’s why we have to create the large template for future events.