Create Sensor: Hour since timestamp

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

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.


Sure, please see attached.

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.

Thanks a lot.

Somehow, I am not getting any output for the above.

What am I missing?

Post a screenshot of the Template Editor containing the template you tested.

There you go

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) }}

Sorry, got confused. :wink:

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

Did so - still no value:

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.

TemplateSyntaxError: unexpected ')'

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.

- sensor:
      - name: last cleaned
      2025-04-21T13:34:14+00:00
      2025-04-21 13:34:14+00:00
      44 minutes

The test confirms that the following template works correctly and reports “44 minutes”.

{{ time_since(states('sensor.saros_10_letztes_reinigungsende') | as_datetime) }}

Agree. :slight_smile:
However, I am not getting a result when trying to add a binary sensor template

  • It will work in a Template Sensor.

  • It will never work in a Template Binary Sensor.

A Binary Sensor can only report “on” or “off”, not a string like “44 minutes”.

Got it, thanks a lot mate!

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.