I’ve got a restful sensor pulling a date string from a website, but I want to replace it to a true date format so I can use in automation, current its only useful in lovelace to see.
Currently sensor.grey_bin returns Wednesday 05 February 2020
Using the following code to grab it:
- platform: rest
resource: https://maps.bolton.gov.uk/residents/getdata.aspx?requesttype=LocalInfo&ms=Bolton/MyHouse&group=My%20house%20data|bin_collections_combined&format=jsonp&uid=100010877913
name: Grey Bin
scan_interval: 86400
value_template: '{%- set find = "<\/p><p><strong>[\w\s]+<\/strong>" %}
{%- set index = 3 %}
{%- set result = value | regex_findall_index(find, index ) %}
{{ result.replace("</p><p><strong>","").replace("</strong>","") }}'
Had loads of help to get it this far but I just can’t strip it properly at all
This will convert it to a timestamp that HA likes to use. Then just modify the device_class to be timestamp so it shows up nicely in the UI. This may not work well if it’s in the future. Which it probably will. But you can always make a template sensor out of it to display the info you originally had.
- platform: rest
resource: https://maps.bolton.gov.uk/residents/getdata.aspx?requesttype=LocalInfo&ms=Bolton/MyHouse&group=My%20house%20data|bin_collections_combined&format=jsonp&uid=100010877913
name: Grey Bin
scan_interval: 86400
value_template: >
{%- set find = "<\/p><p><strong>[\w\s]+<\/strong>" %}
{%- set index = 3 %}
{%- set result = value | regex_findall_index(find, index ) %}
{%- set result = result.replace("</p><p><strong>","").replace("</strong>","") %}
{{ as_timestamp(strptime(result, '%A %d %B %Y')) | timestamp_local }}
To use it:
{{ as_timestamp(states('sensor.grey_bin')) }}
to get back original timestamp in a template sensor
%A is Weekday as full name
%d is Day of the month as zero padded decimal number
%B is Month as full name
%Y is year with century as decimal number
We need to pass those into strptime to it can convert the string into a date time following the format we give it.
value_template: '{%- set find = "<\/p><p><strong>[\w\s]+<\/strong>" %}
{%- set index = 3 %}
{%- set result = value | regex_findall_index(find, index ) %}
{%- set date_str = result.replace("</p><p><strong>","").replace("</strong>","") %}
{{ strptime(date_str, "%A %d %B %Y") }}'
Now the state of this sensor is a real datetime format so you should be able to call things like
Or create an input_datetime that you set when you update the sensor…or a template sensor to hold the converted value…or you can do the conversion (strptime) everytime you want to use it in an automation…but this one sounds annoying and not recommended. Or, convert the time and store is as a state attribute which is just as useful. There are hundreds of ways you can handle it.
The important part is the “strptime()” command to convert a string to a datetime
Can this be used directly in lovelace or must I load it into a value_template sensor (which seems wasteful). Ideally I would just use it to display this way only on one entity card in lovelace.
You won’t be able to have a ‘nice date format’ and have a useable sensor for automations.
If you keep what you have for lovelace:
- platform: rest
resource: https://maps.bolton.gov.uk/residents/getdata.aspx?requesttype=LocalInfo&ms=Bolton/MyHouse&group=My%20house%20data|bin_collections_combined&format=jsonp&uid=100010877913
name: Grey Bin
scan_interval: 86400
value_template: >
{%- set find = "<\/p><p><strong>[\w\s]+<\/strong>" %}
{%- set index = 3 %}
{%- set result = value | regex_findall_index(find, index ) %}
{{ result.replace("</p><p><strong>","").replace("</strong>","") }}
So, I’ve been saying this for what feels like months now, MQTT sensors have this ability to template various attributes on a sensor. The rest sensor does not. This really needs to be added to rest. This would essentially give you what you’re after, but it doesn’t exist. Maybe i’ll give it a whirl at implementing it.