How to template a date time part of a sensor

I did create some template sensors.
That is working fine.
But now I like to retamplate the sensor for the date time part.
I’m not very well used to do templating
I did create this sensor template:

    clean_start:
      value_template: '{{ state_attr("vacuum.xiaomi_vacuum_cleaner", "clean_start") }}'
      icon_template: mdi:clock-start
      unit_of_measurement: ""
      friendly_name: "Starttijd"

The output of “clean_start” looks like:
2021-02-17 01:45:08

But I like to see a shorter output deleting the seconds.
like: 2021-02-17 01:45
How to create a template syntax to get that fixed?
Thanks in advance

Try this:

value_template: '{{ state_attr("vacuum.xiaomi_vacuum_cleaner", "clean_start")[:15] }}'
2 Likes

Or:

value_template: '{{ state_attr("vacuum.xiaomi_vacuum_cleaner", "clean_start")[0:-3] }}'

Which means keep the string from position 0 (first character) but discard the last three

2 Likes

Is the attribute a date/time object? Then this could work, too:

value_template: '{{ state_attr("vacuum.xiaomi_vacuum_cleaner", "clean_start").strftime("%Y-%m-%d %H:%M") }}'
1 Like

So many ways to skin this cat.

Thanks for the ideas. I do understand the principe of all suggestions.
I will try and come back with the results

EDIT: I tried the “.strftime(”%Y-%m-%d %H:%M")"
This is working, thanks!

EDIT2:
Is there somewhere a tutorial for working with templating?

Hi there,

I’m trying to extract just the tme from my next alarm so that i can play it over tts when my bedtime script is triggered.

{{ state_attr(“sensor.matthew_new_p30_pro_next_alarm”, “Local Time”).strftime("%H:%M") }}

I have this but it gives the following response, any ideas anyone, please?

UndefinedError: ‘str object’ has no attribute ‘strftime’

Your attribute will not be called “Local Time”, but probably something like “local_time”. Look under Developer Tools / States — PSA: You can find any entity in developer tools, states page

Also, please format code properly with the </> button or by surrounding it with three backticks — How to help us help you - or How to ask a good question

Hi Troon,

These are the attributes I was working with from Developer Tools / States;

Local Time: Wed May 10 08:15:00 GMT+01:00 2023
Package: com.android.deskclock
Time in Milliseconds: 1683702900000
device_class: timestamp
icon: mdi:alarm
friendly_name: Matthew new p30 pro Next Alarm

As it happens the state actually shows the time in it, i would like to be able to extract it as to something like this 8:15 AM tomorrow, if that’s possible?

Huh, turns out it can be called Local Time. I didn’t think spaces were permitted, but I have an entity just like that.

So you need to convert the time string to a datetime object, then format that as the time you want. Like this, with line breaks just for readability:

Tue May 09 06:06:48 GMT+01:00 2023
%a_ %b_ %d %H:%M:%S _%Z%z____ %Y

For you, that template is:

{{ strptime(state_attr("sensor.matthew_new_p30_pro_next_alarm", "Local Time"), "%a %b %d %H:%M:%S %Z%z %Y").strftime("%I:%M %p") }}

That will just return the 12-hour clock reading of whatever’s in that attribute, regardless of whether it’s in the past or future; and it’ll break if the attribute doesn’t match the format I’ve specified. In particular, I don’t know if the month is abbreviated (%b) or full (%B) because we’re in May and they’re the same.

Documentation is here, and here for strftime. You should probably provide a default value to strptime.

Thank you that’s exactly what i wanted.

I have setup the service call;

service: tts.cloud_say
data:
  cache: false
  entity_id: media_player.sitting_room
  message: >-
    Your alarm is set for {{
    strptime(state_attr("sensor.matthew_new_p30_pro_next_alarm", "Local Time"),
    "%a %b %d %H:%M:%S %Z%z %Y").strftime("%I:%M %p") }} which is in ...

I’m hoping to retrieve the amount of hours and minutes from ‘now’ to time of the next alarm, it shows the hours in this card but i can’t see where i can find the value to attempt to extract it.

2023-05-09_16h50_57

See this topic: Get difference from now() to last changed in minutes - Configuration - Home Assistant Community (home-assistant.io), although you want the other way around as your date’s in the future.

The work is being done for you in the card: if you click the “next alarm” text then the cog, it’ll show you what entity it’s working from.