Hi, I’ve been working on integrating my local council’s refuse collection rest api with my sonoff nspanel running the custom lovelace ui (GitHub - joBr99/nspanel-lovelace-ui: Custom Firmware for NsPanel with the design of HomeAssistant's lovelace UI in mind, works with Tasmota.). In order to do this I require the JSON to be transformed into a switch, I fear I’ve stepped further into the rabbit hole than originally planned, however it’s working well and I’m proud of what I’ve acheived.
Having inspected the api calls from the web page on my local council’s website, I am able to produce a sensor from the rest api which is doing exactly as I wish in regard to bringing the data into home assistant with the following code which resides in my configuration.yaml file…
# Bindicator
rest:
- resource: https://info.ambervalley.gov.uk/WebServices/AVBCFeeds/WasteCollectionJSON.asmx/GetCollectionDetailsByUPRN?uprn=100030039397
method: GET
scan_interval: 3600
sensor:
- name: bincollection
icon: "mdi:trash-can-outline"
unique_id: bincollection
value_template: "OK"
json_attributes:
- recyclingNextDate
- isTodayRecColDay
- refuseNextDate
- isTodayRefColDay
- name: binsensor
unique_id: bincolour
value_template: >-
{% if as_datetime(value_json['recyclingNextDate']) < as_datetime(value_json['refuseNextDate']) %} black
{% else %} green
{% endif %}
As you can probably tell from this, my recycling bin is black, my refuse bin is green. The sensor state reflects this depending upon which date comes first out of the two. For clarity, here’s the JSON that comes from the api…
{"communalWasteId":0,"UPRN":"100030039397","collectionZone":"I","clinical":"False","refuseRound":"4","recyclingRound":"4","gardenZone":"","greenRound":0,"gardenZone2020":" ","greenRound2020":0,"communalWasteLocation":"","recyclingNextDate":"2022-09-29T00:00:00","refuseNextDate":"2022-10-06T00:00:00","calendarUrl":"https://info.ambervalley.gov.uk/docarc/docviewer.aspx?docguid=2a683b45138d4d51ab2b22e9862900b1","gardenCalendarUrl":"","greenNextDate":"0001-01-01T00:00:00","communalRefNextDate":"1900-01-01T00:00:00","communalRycNextDate":"1900-01-01T00:00:00","bags":false,"communalRefuse":false,"communalRecycle":false,"communalRefWeekly":false,"communalRycWeekly":false,"weeklyCollection":false,"recycling":true,"hasService":true,"isTodayRefColDay":false,"isTodayRecColDay":false,"isTodayGrdColDay":false}
In order to make my switch I produced the following code in my switches.yaml file…
- platform: template
switches:
bindicator:
unique_id: binswitch
friendly_name: Bindicator
value_template: "{{ is_state_attr('sensor.binsensor', 'sensor_state', 'green') }}"
icon_template: >-
{% if is_state('sensor.binsensor', 'green') %} mdi:trash-can-outline
{% else %} mdi:recycle
{% endif %}
turn_on:
service: switch.turn_on
target:
entity_id: switch.null
turn_off:
service: switch.turn_off
target:
entity_id: switch.null
I’ve also added the following to the screensaver section of the nspanel config file (/config/appdaemon/apps/apps.yaml)…
statusIcon2:
entity: switch.bindicator
name: bins
icon:
"off": mdi:recycle
"on": mdi:trash-can-outline
color:
"off": [255,255,255]
"on": [0,255,0]
Not sure if I’ve gone about this backwards or if I could’ve done it with a load less code but it works none the less and I did it all by myself with a little help from the docs and this forum.
Hope you like it and thanks to those who helped along the way who I am totally unable to name. Naturally a big thanks to the author of the nspanel firmware that I used.