How to get hours / minutes out of a time stamp

My Android alarm delivers me for

{{ states.sensor.sabbelknecht_nachster_wecker.attributes['Local Time'] }}

the following time stamp:

Sat Jan 16 10:30:00 GMT+01:00 2021

Now I want to create an TTS notification that basically says: “Good night, the alarm is set to 10 30”. How can I access the hours and minutes to process them further?

Thanks for help!

1 Like

See if you can work it out by reading this:

The EPIC Time Conversion and Manipulation Thread!

Thanks, I am on it

If not I’ll have some time tomorrow to give you a hand.

This isn’t particularly elegant but it might do the trick.

{{ state_attr('sensor.sabbelknecht_nachster_wecker', 'Local Time')[11:16].replace(':', ' ') }}

It simply slices the time portion (10:30) out of the string then replaces the colon with a space.

1 Like

the issue I see right now is that “GMT” part in there. Everything else can be factored in using strptime.

Is there some reason that the time string is sent using that (very non-standard) format? It’s kind of all over the place. Can you change that to be more standardized?

It think it will as long as the format is always the same. The only issue I see is if the date is a zero padded number or not. if not then it will throw off the count. I think…

Well, I would also have the linux time stamp, e.g.:

Time in Milliseconds: 1610787600000

I am getting these from the HA Android app. And those are the two time stamps I am getting.

Definitely easier with the timestamp but if you want to use the other attribute it can be done like this:

{% set t = strptime(state_attr('sensor.sabbelknecht_nachster_wecker', 'Local Time'), '%a %b %d %H:%M:%S %Z%z %Y') %}

{{ t.hour }} {{ t.minute }}
2 Likes

Thanks, that works. I dont really care which time stamp to use - just thought initially that it would be easier with the human readable one. But likely that was a wrong assumption :wink:

If you want to use the timestamp you’ll need to divide it by 1000 and then feed it to the timestamp_custom filter.

Hmm…I could have sworn that I used the %Z to try to convert the GMT but I guess I didn’t since I just stuck it in there again and it worked.

Follow up question: How do I integrate this in order to be able to execute an action that does an TTS announcement like:

“the alarm is set to {{ t.hour }} hours and {{ t.minute }} minutes.”

Can I set t as a variable in the automation (how?) and reference {{ t.hour }} in the TTS call? Will I need a script to make this work?

Sorry, I have tried to find examples, but was not successful :frowning: Again thanks a lot for your help!

Do you want the automation to trigger at the time specified by the attribute Local Time?

I have moved this to another thread, so things are easier to search for, closed this one.
Thanks again for all great help!

Had a time stamp quest, did a forum search to find this post to then get linked to the recommended post, and yet, the recommended post doesn’t show in my initial search :wink:

Oh well, that’s why we have searches I guess