Help with relative_time

Hi all
i ask your help
i have a sensor that print this value
2021-03-18T17:33:16Z

i need to convert this into a datetime obj for using with relative_time, I want print something like this:
15 Minutes left

i have tried with

{{ relative_time(strptime('2021-03-18T17:33:16Z', '%Y-%m-%dT%H:%M:%S%z')) }}

but print
2021-03-18 17:33:16+00:00
instead of
15m left

Just use relative_time and the sensor, that should work. Like so:

{{ relative_time(states.sensor.yourSensor.last_updated) }}

Hi, thanks for your support but this is my sensor

{{ states.sensor.lavatrice_washer_completion_time }}

print

<template TemplateState(<state sensor.lavatrice_washer_completion_time=2021-03-18T17:33:16Z; friendly_name=Lavatrice Washer Completion Time, device_class=timestamp @ 2021-03-18T16:22:16.631914+01:00>)>

I don’t need to know when the sensor was last updated, I need to know the status data inside the sensor

To use relative_time the datetime object must be in the past, not the future.

If it’s in the future, relative_time will simply show the datetime as opposed to the relative time.

The first line of the following example contains a time that is in the future for my local time zone.
The second line contains a time that is in the past.

You appear to be trying to use relative_time to report the washer’s completion time (a time in the future) and it’s not designed to work with future times.

1 Like

oh damn
now I understand

BTW in the lovelace dashboard this sensor is automatically printed as:
“in 15 minutes”
is there a way to have the same string “in 15 minutes” inside an automation?
do I have to calculate the difference between the timestamp and the current time?

1 Like

I don’t understand. If the Lovelace Ui already shows the sensor’s value as “in 15 minutes” why do you need to create another way to show the same thing? Isn’t it already showing you what you want?


If you do this:

{{ strptime('2021-03-18T17:33:16Z', '%Y-%m-%dT%H:%M:%S%z') - now() }}

the result will be timedelta object. If you use the the timedelta object’s seconds method, it can be combined timestamp_custom to produce the time in a preferred format.

NOTE

I provided a rudimentary example. It also has to handle the case of a zero or negative timedelta value (i.e. projected completion time is no longer in the future and is now in the past).

2 Likes

Hey! No fair!

I was working on a post in the other thread where you asked the same question but then deleted it.

but so my work wasn’t completely wasted here is another solution that would have gotten you what you wanted (but I didn’t know tht you already had the correct time displayed in the correct format in the card tho):

{% set time_diff =  ((as_timestamp(strptime('2021-03-18T14:50:16Z', '%Y-%m-%dT%H:%M:%S%z').replace(tzinfo=none)) - as_timestamp(now())) / 60) | int %}
{% if time_diff > 60 %}
  {{(time_diff / 60) | int}} hours, {{(time_diff % 60) }} minutes
{% else %}
  {{time_diff}} minutes
{% endif %}

you’ll need to add to it if you want days too.

4 Likes

Hi finity
I am trying to show Daylight Remaining by using your code above, but I get an error
image

{% set time_diff =  ((as_timestamp(strptime(state_attr("sun.sun", "next_dusk"), 
   '%Y-%m-%dT%H:%M:%S%z').replace(tzinfo=none)) - as_timestamp(now())) / 60) | int %}
{% if time_diff > 60 %}
  {{(time_diff / 60) | int}} hours, {{(time_diff % 60) }} minutes
{% else %}
  {{time_diff}} minutes
{% endif %}

Any help would be greatly appreciated!

Solved

{% set time_diff =  ((as_timestamp(strptime(state_attr("sun.sun", "next_dusk"), 
   '%Y-%m-%dT%H:%M:%S%z')) - as_timestamp(now())) / 60) | int %}
{% if time_diff > 60 %}
  {{(time_diff / 60) | int}} hr {{(time_diff % 60) }} min
{% else %}
  {{time_diff}} min
{% endif %}
{% set t = (state_attr('sun.sun', 'next_dusk') | as_datetime - now()).total_seconds() %}
{{ t | timestamp_custom('%-H hr %-M min' if t > 3600 else '%-M min', false) }}

Example of its operation in the Template Editor:

6 Likes