The Bindicator - Old person addition for nspanel lovelace-ui

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.

I seem to be having trouble displaying this in a lovelace entity card, I’d like to create one card that shows the important info (next bin day, icon based upon which bin it is). I have all of the info in there that’s required to do the task but I can’t for the life of me change the icon based upon which bin it is.

Here’s my lovelace card so far, it’s very simple…

type: entity
entity: sensor.bindicator_notification
name: Next Bin Day

And here’s the yaml that’s running the show as it stands (I realise that there’s probably lots to remove as I keep experimenting)…

rest:
  - resource: https://info.ambervalley.gov.uk/WebServices/AVBCFeeds/WasteCollectionJSON.asmx/GetCollectionDetailsByUPRN?uprn=100030039397
    method: GET
#    scan_interval: 3600
    scan_interval: 86400
    binary_sensor:
      - name: bindicator
        unique_id: bindicator
        value_template: >-
          {% set refnd = (as_datetime(value_json['refuseNextDate']) + timedelta(hours=12)) %}
          {% set recnd = (as_datetime(value_json['recyclingNextDate']) + timedelta(hours=12)) %}
          {% if (refnd < recnd) %}
            {% set bintype = "ref" %}on
          {% else %}
            {% set bintype = "rec" %}off
          {% endif %}
        icon: >
          {% if as_datetime(value_json['recyclingNextDate']) < as_datetime(value_json['refuseNextDate']) %} mdi:recycle
          {% else %} mdi:trash-can-outline
          {% endif %}
      - name: bindaysensor
        unique_id: bindaysensor
        value_template: >-
          {% if (value_json['isTodayRecColDay'] == true or value_json['isTodayRefColDay'] == true) %} on
          {% else %} off
          {% endif %}
    sensor:
      - name: "Bindicator: Next Bin Type"
        unique_id: bindicatortype
        value_template: >-
          {% set refnd = (as_datetime(value_json['refuseNextDate']) + timedelta(hours=12)) %}
          {% set recnd = (as_datetime(value_json['recyclingNextDate']) + timedelta(hours=12)) %}
          {% if (as_timestamp(refnd) < as_timestamp(recnd)) %}"ref"
          {% elif (as_timestamp(recnd) < as_timestamp(refnd)) %}"rec"
          {% endif %}
        icon: >-
          {% if (as_timestamp(refnd) > as_timestamp(recnd)) %} mdi:trash-can-outline
          {% else %} mdi:recycle
          {% endif %}
          
      - name: "Bindicator: Next Bin Day"
        unique_id: bindicatorday
        value_template: >-
          {% set refnd = (as_datetime(value_json['refuseNextDate']) + timedelta(hours=12)) %}
          {% set recnd = (as_datetime(value_json['recyclingNextDate']) + timedelta(hours=12)) %}
          {% if (refnd < recnd) %}
            {% print refnd %}
          {% else %}
            {% print recnd %}
          {% endif %}
        icon: >
          {% if (refnd > recnd) %} mdi:trash-can-outline
          {% else %} mdi:recycle
          {% endif %}
      - name: "Bindicator: Next Bin Notification"
        unique_id: bindicatornotification
        value_template: >-
          {% set refnn = (as_datetime(value_json['refuseNextDate']) - timedelta(hours=12)) %}
          {% set recnn = (as_datetime(value_json['recyclingNextDate']) - timedelta(hours=12)) %}
          {% if (refnn < recnn) %}
            {% print refnn %}
          {% else %}
            {% print recnn %}
          {% endif %}
        icon: >
          {% if (refnn < refnn) %} mdi:trash-can-outline
          {% else %} mdi:recycle
          {% endif %}
      - name: "Bindicator: Next Recycle Day"
        unique_id: bindicatornextrecday
        value_template: "{{ as_datetime(value_json['recyclingNextDate']) }}"
      - name: "Bindicator: Next Refuse Day"
        unique_id: bindicatornextrefday
        value_template: "{{ as_datetime(value_json['refuseNextDate']) }}"
      - name: "Bindicator Notification Icon"
        unique_id: bindicatornotificationicon
        value_template: >-
          {% if (as_timestamp( states("sensor.bindicator_next_bin_notification") ) < as_timestamp(now()) < as_timestamp( states("sensor.bindicator_next_bin_day") )) %}tomorrow
          {% elif ((value_json['isTodayRecColDay'] == true or value_json['isTodayRefColDay'] == true) and (as_timestamp(now()) < as_timestamp( today_at("12:00")))) %}today
          {% else %} {{ as_timestamp(states('sensor.bindicator_next_bin_day')) | timestamp_custom('%A',false) }}
          {% endif %}

Unfortunately the restful side of things with home assistant doesn’t allow for icon_template to be used, I’ve tried card mod to no avail. So far my only idea is to create a separate template that uses the values of these sensors, seems like yet more overkill though.

Can anybody offer any advice at all?