JSON Response greater than 255 characters

Hi,

First off, I’m not a programmer so this stuff is copy/paste from other posts. Basically I noticed that someone had a module for Magic Mirror that used my local council’s API to get bin collection dates. I thought I could try get this into my HA setup, so after hours of pouring over his code I found a JSON Post command that I could use to get a JSON response with my bin collection dates BUT I’ve come across the 255 character limit. Looking this up everybody says to use json_attributes to split it up. As I don’t understand this step, I’ve spent hours trying to get it to work but not got anywhere.

My sensor looks like this at the moment:

  - platform: rest
    name: JSON BINS
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321456987934464654" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXXMYUPRN" } }'
    json_attributes:
      - Description
      - Last
      #- Next
      #- State
      #- TaskType
    #value_template: '{{ value_json.value}}'
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor

If I POST in Postman I get the following back, which all seems to work, I just can’t figure out a way to capture it. I basically want the below out of the JSON

“ServiceName”: “Domestic Refuse Collection” —> “Next”: “2019-07-30T00:00:00”
“ServiceName”: “Domestic Food and Garden Waste Collection” —> “Next”: “2019-07-23T00:00:00”

{
    "jsonrpc": "2.0",
    "id": "1321456987934464654",
    "result": {
        "success": true,
        "status": "Success",
        "errors": [],
        "services": [
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "ScheduleDescription": null,
                        "State": null,
                        "TaskType": null
                    }
                },
                "ServiceName": "Bulky Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "Last": "2019-07-16T07:00:00",
                        "Next": "2019-07-30T00:00:00",
                        "ScheduleDescription": "Tuesday every other week",
                        "State": "Completed",
                        "TaskType": "Collect Domestic Bin Refuse"
                    }
                },
                "ServiceName": "Domestic Refuse Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "Last": "2019-07-09T07:00:00",
                        "Next": "2019-07-23T00:00:00",
                        "ScheduleDescription": "Tuesday every other week",
                        "State": "Completed",
                        "TaskType": "Collect Domestic Dry Recycling"
                    }
                },
                "ServiceName": "Domestic Dry Recycling Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": [
                        {
                            "Last": "2019-07-09T07:00:00",
                            "Next": "2019-07-23T00:00:00",
                            "ScheduleDescription": "Tuesday every other week",
                            "State": "Completed",
                            "TaskType": "Collect Domestic Food"
                        },
                        {
                            "Last": "2019-07-09T07:00:00",
                            "Next": "2019-07-23T00:00:00",
                            "ScheduleDescription": "Tuesday every other week",
                            "State": "Completed",
                            "TaskType": "Collect Domestic Garden"
                        }
                    ]
                },
                "ServiceName": "Domestic Food and Garden Waste Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "ScheduleDescription": null,
                        "State": null,
                        "TaskType": null
                    }
                },
                "ServiceName": "Container Maintenance"
            }
        ]
    }
}

Any Help would be greatly appreciated.

Thanks

Simon

To gain a better understanding (by allowing you to experiment) paste all of this into the Template Editor:

{% set value = {
    "jsonrpc": "2.0",
    "id": "1321456987934464654",
    "result": {
        "success": true,
        "status": "Success",
        "errors": [],
        "services": [
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "ScheduleDescription": null,
                        "State": null,
                        "TaskType": null
                    }
                },
                "ServiceName": "Bulky Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "Last": "2019-07-16T07:00:00",
                        "Next": "2019-07-30T00:00:00",
                        "ScheduleDescription": "Tuesday every other week",
                        "State": "Completed",
                        "TaskType": "Collect Domestic Bin Refuse"
                    }
                },
                "ServiceName": "Domestic Refuse Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "Last": "2019-07-09T07:00:00",
                        "Next": "2019-07-23T00:00:00",
                        "ScheduleDescription": "Tuesday every other week",
                        "State": "Completed",
                        "TaskType": "Collect Domestic Dry Recycling"
                    }
                },
                "ServiceName": "Domestic Dry Recycling Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": [
                        {
                            "Last": "2019-07-09T07:00:00",
                            "Next": "2019-07-23T00:00:00",
                            "ScheduleDescription": "Tuesday every other week",
                            "State": "Completed",
                            "TaskType": "Collect Domestic Food"
                        },
                        {
                            "Last": "2019-07-09T07:00:00",
                            "Next": "2019-07-23T00:00:00",
                            "ScheduleDescription": "Tuesday every other week",
                            "State": "Completed",
                            "TaskType": "Collect Domestic Garden"
                        }
                    ]
                },
                "ServiceName": "Domestic Food and Garden Waste Collection"
            },
            {
                "Description": "5 HORSE GUARDS WAY, THATCHAM, RG19 4PE",
                "EventTypes": null,
                "ServiceHeaders": {
                    "ServiceHeader": {
                        "ScheduleDescription": null,
                        "State": null,
                        "TaskType": null
                    }
                },
                "ServiceName": "Container Maintenance"
            }
        ]
    }
}

%}

{{ value.result.services[1].ServiceHeaders.ServiceHeader.Next }}
{{ value.result.services[2].ServiceHeaders.ServiceHeader.Next }}

The two templates at the very bottom extract the dates you want.

Here’s the catch. The data has a complex structure made up of dictionaries and lists. It’s the list portion than may become problematic. This part of the tempalate services[2] is selecting an item from the list. That item represents all the information for ‘Domestic Food and Garden Waste Collection’. As long as it always exists in services[2], the template will be valid. However, if its position in the list is not fixed, but variable, then the template will fail. In other words, if the next time the sensor retrieves data and Food and Garden Waste has changed position in the list to services[0] then the template will fail to retrieve it. Let’s hope they don’t shuffle the positions and always use the same ‘slots’ in the list.

Hi,

Thanks for helping me with this, I didn’t know about template editor, it’s been really good playing around with it. I’ll have to keep my eye the JSON reponse to see if the structure remains constant. I broke it down into two REST calls and ended up with the below, I figured out how to format the date and added a scan interval.

  - platform: rest
    name: Black Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 7200
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321455687934464654" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXXXXXXXX" } }'
    value_template: "{{ as_timestamp(value_json.result.services[1].ServiceHeaders.ServiceHeader.Next) | timestamp_custom('%d/%m/%y') }}"
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor
 
  - platform: rest
    name: Green Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 7260
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321455517694427543" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXXXXXXXX" } }'
    value_template: "{{ as_timestamp(value_json.result.services[2].ServiceHeaders.ServiceHeader.Next) | timestamp_custom('%d/%m/%y') }}"    
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor

Thanks

Simon

2 Likes

To keep this updated - the service code did change - also the template sensor was update - so here is some updated code. Now the sensor says how many days until the collection type is due

  - platform: rest
    name: Black Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 7200
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321455687934464654" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXXXXX" } }'
    value_template: "{{ as_timestamp(value_json.result.services[0].ServiceHeaders.ServiceHeader.Next) | timestamp_custom('%A, %B %d, %Y') }}"
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor
 
  - platform: rest
    name: Green Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 7260
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321455517694427543" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXXXXX" } }'
    value_template: "{{ as_timestamp(value_json.result.services[1].ServiceHeaders.ServiceHeader.Next) | timestamp_custom('%A, %B %d, %Y') }}"    
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor

updated code to deal with 2021.12 changes to dates (council UTC doesnt include the timezone)

  - platform: rest
    name: Black Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 7200
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321455687934464654" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXX" } }'
    value_template: "{{ as_timestamp(value_json.result.services[0].ServiceHeaders.ServiceHeader.Next) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') }}"
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor
 
  - platform: rest
    name: Green Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 7260
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "1321455517694427543" , "method" : "veolia.wasteservices.v1.getServicesByUPRN" , "params" : { "uprn" : "XXXXXX" } }'
    value_template: "{{ as_timestamp(value_json.result.services[1].ServiceHeaders.ServiceHeader.Next) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') }}"    
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor