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.
Tinkerer
(aka DubhAd on GitHub)
December 30, 2017, 9:22am
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") }}
2 Likes
amritgary
(Gary)
December 30, 2017, 10:28am
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
gwynevans
(Gwyn Evans)
December 30, 2017, 12:07pm
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)
Kip
March 28, 2018, 12:29am
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??
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
Kip
May 29, 2018, 7:47pm
8
Fantastic, that worked! Thank you!
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?
1 Like
BurnedSoup
(BurnedSoup)
October 21, 2019, 7:26pm
10
Add this to your config, it adds a sensor with the id: sensor.date_time
platform: time_date
display_options:
Then as others have said, add this line to the line with the issue not updating.
entity_id: sensor.date_time
Here my working sensor using Google Calender sensor:
platform: template
sensors:
time_until_next_habs_game:
friendly_name: ‘Time Until Next Habs Game’
entity_id: sensor.date_time
unit_of_measurement: ‘minutes’
value_template: >-
{% if states.calendar.montreal_canadiens.attributes.start_time %}
{{((as_timestamp(states.calendar.montreal_canadiens.attributes.start_time) - as_timestamp(now())) / 60) | int }}
{%- else -%}
0
{%- endif %}
rolandow
(Rolandow)
March 1, 2021, 10:14am
11
I wanted to use a SQL sensor to compare with the current datetime. This didn’t work well either, because somehow the date time from the SQL query was assumed to be in local time by home assistant, while it was in UTC already.
I found when formatting the datetime according to ISO 8601, the as_timestamp()
function works correctly out of the box.
So I have my sensor like this now:
- platform: sql
db_url: !secret mysql_db_url
queries:
- name: Badkamer laatst normaal
query: "SELECT DATE_FORMAT(last_changed, '%Y-%m-%dT%TZ') AS last_changed FROM states WHERE entity_id = 'sensor.0x00158d0006796e6e_humidity' AND state < 60 AND state > 0 ORDER BY created DESC LIMIT 1"
column: "last_changed"
This keeps it human readable, but still it converts really easy.
So my script has a template like this:
{% if (states("sensor.0x00158d0006796e6e_humidity")|float) < 70 %}
Idle
{% else %}
{% set warm_time = (as_timestamp(now())|int - as_timestamp(states("sensor.badkamer_laatst_normaal"))|default(0)|int) %}
{% if warm_time > 600 %}
Bath
{% else %}
Shower
{% endif %}
{% endif %}
My use case was to detect if someone was showering or taking a bath, so I could set the extractor to the right mode.
Just wanna provide another solution. If the output already looks similar to an iso8601 format, you can use as_timestamp
. This is a valid iso8601 time.
2023-07-13T14:20:03.215000+06:00
In my case the raw output was 2023-07-13 14:20:03.215000+06:00
, so I just replace the space with a T and feed that to as_timestamp
.
{{ as_timestamp(state_attr("sensor.something", "timestamp") | replace(" ", "T")) }}