Denno
April 21, 2025, 12:48pm
1
Hi there,
I do have an entity that shows a timestamp since last cleaning finish from my roborock.
I’d like to use this value and create a sensor/template, that shows hours since the above mentioned timestamp.
Can anyone help on how to achieve this?
Thanks in advance
123
(Taras)
April 21, 2025, 1:17pm
2
Post an example of the entity’s timestamp.
If the timestamp is in an appropriate format, you can create a template using the time_since
function.
Denno
April 21, 2025, 1:21pm
3
Sure, please see attached.
123
(Taras)
April 21, 2025, 1:24pm
4
Copy-paste the following template into the Template Editor and confirm it reports the correct elapsed time.
{{ time_since(states('sensor.saros_10_letztes_reinigungsende') | as_datetime) }}
If it meets your needs, you can use it in a new Template Sensor.
From the documentation:
time_since(datetime, precision)
converts a datetime object into its human-readable time string. The time string can be in seconds, minutes, hours, days, months, and years. precision
takes an integer (full number) and indicates the number of units returned. The last unit is rounded. For example: precision = 1
could return “2 years” while precision = 2
could return “1 year 11 months”. This function can also be used as a filter. If the datetime is in the future, returns 0 seconds. A precision of 0 returns all available units, default is 1.
Denno
April 21, 2025, 1:33pm
5
Thanks a lot.
Somehow, I am not getting any output for the above.
What am I missing?
123
(Taras)
April 21, 2025, 1:38pm
6
Post a screenshot of the Template Editor containing the template you tested.
123
(Taras)
April 21, 2025, 1:44pm
8
That’s a screenshot of the Template Editor?
It looks like a screenshot of a Template Sensor’s YAML configuration (which contains incorrect indentation).
NOTE
I made a correction to my initial example. I forgot to include as_datetime
(which converts a datetime string to a datetime object).
{{ time_since(states('sensor.saros_10_letztes_reinigungsende') | as_datetime) }}
123
(Taras)
April 21, 2025, 1:49pm
10
OK, now add the missing as_datetime
filter to the template as shown in my previous post.
For future reference, it’s preferable to test a template in the Template Editor before using it in a Template Sensor.
Developer tools → Template
123
(Taras)
April 21, 2025, 1:57pm
12
Please copy-paste this into the Template Editor and tell me what it reports. It should report three values.
{{ states('sensor.saros_10_letztes_reinigungsende') }}
{{ states('sensor.saros_10_letztes_reinigungsende') | as_datetime }}
{{ time_since(states('sensor.saros_10_letztes_reinigungsende') | as_datetime) }}
The Template Editor is in Developer Tools → Template.
Denno
April 21, 2025, 2:00pm
13
TemplateSyntaxError: unexpected ')'
123
(Taras)
April 21, 2025, 2:15pm
14
Please check what you had copied to what is posted above.
Look near the end of the second line and ensure it doesn’t contain a )
character.
Denno
April 21, 2025, 2:18pm
15
- sensor:
- name: last cleaned
2025-04-21T13:34:14+00:00
2025-04-21 13:34:14+00:00
44 minutes
123
(Taras)
April 21, 2025, 2:21pm
16
The test confirms that the following template works correctly and reports “44 minutes”.
{{ time_since(states('sensor.saros_10_letztes_reinigungsende') | as_datetime) }}
Denno
April 21, 2025, 2:23pm
17
Agree.
However, I am not getting a result when trying to add a binary sensor template
123
(Taras)
April 21, 2025, 2:26pm
18
A Binary Sensor can only report “on” or “off”, not a string like “44 minutes”.
Denno
April 21, 2025, 2:32pm
19
Got it, thanks a lot mate!
123
(Taras)
April 21, 2025, 2:34pm
20
You’re welcome!
Before you go, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps other users find answers to similar questions.