Help getting JSON into Restful / Template Sensor

Hi guys,

Trying to get JSON data from here (don’t worry, not my real house) into a restful sensor.

The output is longer than 255 chars so Restful won’t store it; I believe I can break it down using json_attributes but I just can’t make the nesting work no matter which way I try it (I think due to the import at the start).

Ideally I would have three template sensors which just hold the dates mentioned so I can make a custom card.

Can you post the example outside the link. Link isn’t working.

Sorry; here you go. It comes all in one line in the web browser also which makes it more difficult :slight_smile:

import({"Results":{"bin_collections_combined":[{"__":100010877913,"_":"<div class=\"clearfix 2\"><img class=\"isharefeed\" src=\"https://maps.bolton.gov.uk/residents/custom/mybolton_files/beigebin.jpg\"/><p>Your next beige bin collection will be on </p><p><strong>Wednesday 11 September 2019</strong></p></div>"},{"__":100010877913,"_":"<div class=\"clearfix 2\"><img class=\"isharefeed\" src=\"https://maps.bolton.gov.uk/residents/custom/mybolton_files/burgundybin.jpg\"/><p>Your next burgundy bin collection will be on </p><p><strong>Wednesday 11 September 2019</strong></p></div>"},{"__":100010877913,"_":"<div class=\"clearfix 2\"><img class=\"isharefeed\" src=\"https://maps.bolton.gov.uk/residents/custom/mybolton_files/greenbin.jpg\"/><p>Your next green bin collection will be on </p><p><strong>Wednesday 11 September 2019</strong></p></div>"},{"__":100010877913,"_":"<div class=\"clearfix 2\"><img class=\"isharefeed\" src=\"https://maps.bolton.gov.uk/residents/custom/mybolton_files/greybin.jpg\"/><p>Your next grey bin collection will be on </p><p><strong>Wednesday 04 September 2019</strong></p></div>"}]}});

that looks like html not json. That would be the scrape sensor. Actually it looks like a mix of html and json.

Either way, if the rest sensor reads it in…

What info are you trying to get? Just the dates?

Yeah, ideally I would have three sensors with the three different dates. The rest sensor was reading the whole string in when it was shorter but I just couldn’t format it into a template.

Sorry for the delay, is the import seen in your rest sensor? If yes, I don’t think you can treat it as json.

Yeah it does show, unfortunatley.

OK, understood; now to try and figure out the scrape sensor instead, but it also doesn’t seem to be valid html so not sure if this will work

Yeah, it looks like it’s json with html values. Super shitty tbh. Anyways, you can do a regex. Hopefully the order of the bins doesn’t change.

index bin
0 beige
1 burgundy
2 green
3 grey

value_template possibilities

Whole sentance ( Your next beige bin collection will be on Wednesday 11 September 2019 )

{%- set find = '[\w\s]+<\/p><p><strong>[\w\s]+<\/strong>' %}
{%- set index = 0 %}
{%- set result = value | regex_findall_index(find, index ) %}
{{ result.replace('</p><p><strong>','').replace('</strong>','') }}

Just the date ( Wednesday 11 September 2019 ):

{%- set find = '<\/p><p><strong>[\w\s]+<\/strong>' %}
{%- set index = 0 %}
{%- set result = value | regex_findall_index(find, index ) %}
{{ result.replace('</p><p><strong>','').replace('</strong>','') }}

Color: date ( beige: Wednesday 11 September 2019 )

{%- set find = '[\w\s]+<\/p><p><strong>[\w\s]+<\/strong>' %}
{%- set index = 0 %}
{%- set result = value | regex_findall_index(find, index ) %}
{{ result.replace('Your next ','').replace(' bin collection will be on ',': ').replace('</p><p><strong>','').replace('</strong>','') }}
2 Likes

petro, thank you for doing this; you are amazing!

I can mostly follow it though but what should go in the scrape select sensor as its coming back unknown no matter what I put in.

scrape will only work if it’s html, so it won’t work. You gotta use the rest sensor and just brute force regex the result.

Petro, got it thanks! Had to create 4 instances of Rest as it isn’t JSON so couldn’t get json_attributes to work but its getting the data, I’ve just set scan_interval to 24hrs to avoid repeated calls.

Thanks so much for your assistance; I really appreciate it - spent hours trying to get this work! Cheers.

2 Likes

glad to help!

1 Like

Hi Petro, sorry to try and appoint you as my personal assistant but is there a way to convert this to a true date format that HA can understand for automations, I’m having trouble trying to convert just a string to a useable date format following a few posts on here …