Restful sensor string to date

Hi all,

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

{{ as_timestamp(states('sensor.grey_bin')) | timestamp_custom('%A %d %B %Y') }}
1 Like

Use strptime to convert the string to an actual datetime format.

https://docs.python.org/3.6/library/datetime.html#strftime-strptime-behavior

Looking at the docs, the format is: “%A %d %B %Y”

%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

as_timestamp(states(‘sensor.rest_garbage_sensor’))

Note, this will change how it looks in lovelace or wherever else you were using that string before. It will now be “2020-02-05 00:00:00”.

You can either convert it yet again in places you want to display it…i.e.:

       {{ as_timestamp(states('sensor.rest_garbage_sensor')) | timestamp_custom("%A %d %B %Y") }}

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

Thanks for this

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.

Are you just copying me?

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>","") }}

And use this in templates:

as_timestamp(strptime(states('sensor.grey_bin'), '%A %d %B %Y'))

You’ll have the best of both worlds with 1 sensor.

I was writing all that out before you replied lol. After submitting I noticed someone else replied.

On two separate topics? Seems suspicious…

Got it - I think. But I actually end up having two entities then;
sensor.grey_bin (the date value)

and sensor.grey_bin_ll (driven by this in the templating):

as_timestamp(strptime(states('sensor.grey_bin'), '%A %d %B %Y'))

This is fine, just wondered if there was a way to make lovelace display that way without having to use two entities.

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.

Thanks Petro! Makes sense; this will work for me.

If you get to implementing it let me know - you definitely seem more than skilled to do it :slight_smile: