Unable to parse and format sensor.next_alarm

Hi everyone

I’d like to translate the next alarm sensor in a markdown card but this is driving me crazy.

I got inspiration from this topic : Date in local language - #8 by 123

First I could not get the weekday(), day, month attributes from states('sensor.sm_g965f_next_alarm')) so I converted the string to timestamp with as_timestamp(states('sensor.sm_g965f_next_alarm')) and tried to get a real datetime variable by parsing it again with as_datetime().
Here’s what I tried :

  • {{ as_datetime(as_timestamp(states('sensor.sm_g965f_next_alarm'))) }} Card ends up empty (is a float)
  • {{ as_datetime(as_timestamp(states('sensor.sm_g965f_next_alarm')) | int) }} Card ends up empty (forced int)
  • {{ as_datetime(as_timestamp(states('sensor.sm_g965f_next_alarm')) | int | string) }} Displays “None”
  • {{ as_datetime(as_timestamp(states('sensor.sm_g965f_next_alarm')) | string) }} Displays “None”

What’s wrong with this variable translation ?
I just want a variable from which I can get the weekday(), day, month, hours and minutes…

Thanks for your help

strptime is what you use to format a datetime, normally.

And there are some pointers on using it in this topic The EPIC Time Conversion and Manipulation Thread!

1 Like

this works fine for me (aside from the hour being in UTC)

{{as_datetime(states('sensor.sm_g965f_next_alarm')).hour}}

{{as_datetime(states('sensor.sm_g965f_next_alarm')).weekday()}}

{{as_datetime(states('sensor.sm_g965f_next_alarm')).minute}}

{{as_datetime(states('sensor.sm_g965f_next_alarm')).year}}

{{as_datetime(states('sensor.sm_g965f_next_alarm')).month}}

EDIT:

for local time as suggested below:

{{as_local(as_datetime(states('sensor.sm_g965f_next_alarm'))).hour}}

{{as_local(as_datetime(states('sensor.sm_g965f_next_alarm'))).weekday()}}

{{as_local(as_datetime(states('sensor.sm_g965f_next_alarm'))).minute}}

{{as_local(as_datetime(states('sensor.sm_g965f_next_alarm'))).year}}

{{as_local(as_datetime(states('sensor.sm_g965f_next_alarm'))).month}}

The time being UTC is a pain. How can we get the “Local time” attribute ?

EDIT : {{as_local(as_datetime(states('sensor.sm_g965f_next_alarm')))}} does the trick

1 Like