Home Assistant Community

Convert String to time

#1

Hi,
I want to setup a trigger to compare sensor value to current time.
The sensor value (last_capture) is a string (I think), extracted as a monitored_condition from Arlo. My entity id for this sensor value is sensor.last_tv_room

While using template tool

{{states.sensor.last_tv_room.state}}

Result: 12-30-2017 00:05:49

{{as_timestamp(states.sensor.last_tv_room.state)}}

Result: None

{{ states.sensor.last_tv_room.state.split(’ ')[1] }}

Result: 00:05:49

{{ as_timestamp(states.sensor.last_tv_room.state.split(’ ')[1]) }}

Result: None

Shouldn’t i expect to see a unix timestamp instead of “None”?
And so, how can i convert a string (formatted as “12-30-2017 00:05:49”) to unix stamp

Appreciate the help.

0 Likes

#2

Have a look at the template docs - you want strptime. Probably something like:

{{ strptime(states('sensor.last_tv_room'), "%d-%m-%Y %H:%M:%S") }}
0 Likes

#3

hi,
Tried strptime already.

{{ strptime(states(‘sensor.last_tv_room’), “%d-%m-%Y %H:%M:%S”) }}
Result: 12-30-2017 04:21:45

How can i convert this to a unix time?

Converting above result using as_timestamp:

{{ as_timestamp(strptime(states(‘sensor.last_tv_room’), “%d-%m-%Y %H:%M:%S”)) }}
Result: None

0 Likes

#4

Does this work?

{{ strptime(states(‘sensor.last_tv_room’), “%d-%m-%Y %H:%M:%S”).timestamp() }}

(NB: Not in a position to test at the moment & I’ve not checked for timezone correctness)

0 Likes

#5

That gives out an error:

0 Likes

#6

I’m have the exact same problem with my code:

{{states.zwave.schlage_deadbolt.attributes.receivedTS}}
2018-03-27 19:48:44:341 

{{as_timestamp(states.zwave.schlage_deadbolt.attributes.receivedTS)}}
None

{{now()}}
2018-03-27 20:27:06.955177-04:00

{{as_timestamp(now())}}
1522196826.955699

Has anyone figured out a solution??

0 Likes

#7

I had the same problem this is how I solved it.

{{ as_timestamp(states.zwave.master_bedroom_multisensor.attributes.receivedTS) }}
None

{{ as_timestamp(states.zwave.master_bedroom_multisensor.attributes.receivedTS | regex_replace(":[0-9][0-9][0-9].*$","")) }}
1527546876.0
0 Likes

Bin / Waste Collection
#8

Fantastic, that worked! Thank you!

0 Likes

#9

Not working for me:

{{ as_timestamp(states.script.bikenotify.attributes.last_triggered) }}
None

{{ as_timestamp(states.script.bikenotify.attributes.last_triggered | regex_replace(":[0-9][0-9][0-9].*$","")) }}
None

Can you advise?

0 Likes