Relative_time template extension

Around version 0.29.5 I had a template that would count up the time since a entity was last updated.

"{{ relative_time(states.sensor.power_line_watts.last_updated) }}"

It would count up from seconds and then to minutes since the sensor was updated.

This has not been working for some time and I know there was a breaking change in one of the recent releases. On the website it looks like this relative_time(timestamp)

If I use that method the state remains unknown.

If I use my original template the state stays at 0 seconds but if I put my original template into the template dev-tool it shows that it is working as it used to. As you can see by the “23 seconds”

Can anyone point me in the right direction here?
I would like to continue to use the format “x seconds” into “y minutes”.

Thanks!

1 Like

I have the same problem:

binary_sensor:
 - platform: template
   sensors:
      bedroom_movement:
        friendly_name: Bedroom Movement
        value_template: '{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.bedroom_motion.last_changed) < 1800)}}'
        sensor_class: motion

Template renders fine and gives the expected result - however the sensor does not reflect that. It always stays On

@snizzleorg

This has to be a bug or something might have to go over to github to report.

I tried the setup you have and same result. The dev-tool shows the correct time but frontend never updates.

"{{(as_timestamp(now())-as_timestamp(states.sensor_mysensor.last_updated)) | int }}"

@snizzleorg

@ReneTode

Helped me fix my template sensor.

It uses the date_time sensor.

- platform: template
      sensors:
        plw_sensor_last_update:
          value_template: "{{(as_timestamp(now())-as_timestamp(states.sensor.power_line_watts.last_updated)) | int }}"
          entity_id:
            - sensor.date__time
1 Like

This solved the issue for me:

sensor:
  - platform: time_date
    display_options:
      - 'time'

binary_sensor:
 - platform: template
   sensors:
      bedroom_movement:
        friendly_name: Bedroom Movement
        value_template: '{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.bedroom_motion.last_changed) < 1800)}}'
        sensor_class: motion
        entity_id: sensor.time
2 Likes

I know I’m resurrecting an old thread here, but I finally got around to trying this and used your setup as a model, but I am confused about the value result. Is it showing minutes? Seconds? ??

It is in seconds.

You can use the this to show Seconds: "{{(as_timestamp(now())-as_timestamp(states.sensor.power_line_watts.last_updated)) | int }} Seconds"

Or you can pass the unit_of_measurement: sec parameter for proper history graphing.

You can do this for Minutes: "{{(as_timestamp(now())-as_timestamp(states.sensor.power_line_watts.last_updated)) | int //60 }} Minutes"

Or this to have it change as it counts up. You will have to create a sensor to monitor that sensor * The entity used here is for a different sensor: "{% if states('sensor.derick_location_for') | int <= 60 %}{{ states.sensor.derick_location_for.state}} Seconds{%elif states('sensor.derick_location_for') | int < 3600 %}{{ states.sensor.derick_location_for.state | int // 60 }} Minutes{%elif states('sensor.derick_location_for') | int >= 3600 %}{{ states.sensor.derick_location_for.state | round(common) | int * 0.000277 | round(4) }} Hours{%else%}??{%endif%}"

Hope this helps!

6 Likes

It helps tons; thanks so much!!

1 Like

Anytime! Happy to help!

1 Like

If anybody is looking at this thread now, please be aware that the relative_time issue has been fixed in the recent versions.

This works great now.

    sensors:
      living_room_movement:
        friendly_name: Living Room Movement
        value_template: '{{ relative_time(states.sensor.aeotec_multisensor_burglar_2_10.last_changed)}}'```
4 Likes

Thanks for the update!

  1. What time format relative_time() understands?
    Tried to pass:
  • timestamp
  • timestamp|float
  • 2000-07-01T00:00:00+00:00
    Nothing works :joy_cat:
  1. How can I format timedelta (in seconds) into smt like «23 minutes»?
    Checked jinja’s docs but still can’t figure it out.

If relative time is working again them something like this would go from seconds to minutes like it used to.

"{{ relative_time(states.sensor.power_line_watts.last_updated) }}"

1 Like

Yeah that works but I need to use my own value (e.g. response from REST), does it only understands last_updated state?

Have you managed to figure this out?

I need to calculate relative_time within a template sensor, but I cannot figure out the correct time format…

No but in some cases you can use device_class = timestamp

Hello hello,

This is not working for me as well but i found a dirty workaround :sweat_smile:

first my code:

  - platform: template
    sensors:
      aquarium_vibration_timer:
        friendly_name: 'Last fed'
        value_template: "{{relative_time(states.binary_sensor.aquarium_vibration.last_changed)}}"
        device_class: timestamp
        entity_id:
          - sensor.date_time
          - binary_sensor.aquarium_vibration

Then what appears in Lovelace if i open the sensor:
Screen Shot 2021-08-26 at 14.58.39

What appears in Lovelace in the page:
Screen Shot 2021-08-26 at 15.02.37

And this is actually what I need… thing is it doesn’t update right?

I noticed it if I call a service to reload the template entities, it does update on the Lovelace page (still not in the entity state though, but that’s fine for me.

So yeah, every minute automation or node-red big timer or whatever you wanna use to call the service and it works for me.

Maybe I have something wrong in my template, can’t really figure it out :confused:

Anyway, that’s my two cents!

What your template returns is a string, not a timestamp. So you cannot apply that device_class.

Just this will do for a timestamp class.

value_template: "{{states.binary_sensor.aquarium_vibration.last_changed}}"

so… now the timer is stuck…

Screen Shot 2021-08-26 at 16.53.06

So my workaround solution is still useful.