Sorting REST sensors by date

Not sure how/if this would be possible. I’ve got four sensors reading JSON rest output providing me with recycling collection dates.

waste

This is informed by:
from groups.yaml

waste: 
  name: Waste Schedule
  entities:
    - sensor.testbin_d
    - sensor.nextcolltype
    - sensor.testbin_e
    - sensor.nextcolltype2

and from configuration.yaml

sensor:
  - platform: rest
    resource: https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747
    value_template: '{{ value_json.0.NextCollection}}'
    name: nextcoll
  - platform: rest
    resource: https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747
    value_template: '{{ value_json.0.WasteType}}'
    name: nextcolltype
  - platform: rest
    resource: https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747
    value_template: '{{ value_json.1.NextCollection}}'
    name: nextcoll2
  - platform: rest
    resource: https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747
    value_template: '{{ value_json.1.WasteType}}'
    name: nextcolltype2    
	  - platform: template
sensors:
       testbin_d:
    value_template: '{{ states.sensor.nextcoll.state | replace("/Date(","") | replace(")","") | replace("/","")  | multiply(0.001) | round(0) | timestamp_custom("%d/%m/%y") }}'
    friendly_name: 'Next Collection'
  testbin_e:
    value_template: '{{ states.sensor.nextcoll2.state | replace("/Date(","") | replace(")","") | replace("/","")  | multiply(0.001) | round(0) | timestamp_custom("%d/%m/%y") }}'
    friendly_name: 'Next Collection'  

but could I sort the rest sensors by date so that the next collection by date appears first in the list?

Thanks!

1 Like

I would do this using a command_line sensor that uses curl & jq. Something along the lines of:

sensor:
  - platform: command_line
    name: testbin_d
    command: curl -s https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747 | jq 'sort_by(.NextCollection)|.[0].NextCollection'
    value_template: >
      {{ value | replace("/Date(","") | replace(")","") | replace("/","")  | multiply(0.001) | round(0) | timestamp_custom("%d/%m/%y") }}
  - platform: command_line
    name: nextcolltype
    command: curl -s https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747 | jq 'sort_by(.NextCollection)|.[0].WasteType'
  - platform: command_line
    name: testbin_e
    command: curl -s https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747 | jq 'sort_by(.NextCollection)|.[1].NextCollection'
    value_template: >
      {{ value | replace("/Date(","") | replace(")","") | replace("/","")  | multiply(0.001) | round(0) | timestamp_custom("%d/%m/%y") }}
  - platform: command_line
    name: nextcolltype2
    command: curl -s https://doitonline.york.gov.uk/BinsApi/EXOR/getWasteCollectionDatabyUprn?uprn=10034770747 | jq 'sort_by(.NextCollection)|.[1].WasteType'

Thanks!!I I changed the first character to avoid having duplicate sensor names while I’m testing. So - that gives values of:

1550016000000” and “1549411200000” in the front end - and I guess I’d need to pop them in a group to enable sorting?

258

EDIT: Looks like I’ll have to see if I can install JQ onto Hass as well?

Sorry, I’m not following you. The sorting is done by jq by processing the output from the curl command. That’s why one set of the sensors uses [0] and the other uses [1].

And jq is a command that needs to be installed in the OS in which HA is running. It’s not a HA component or Python package that HA uses.

Thanks! :slight_smile:

In the end Reddit user naito- came up with this:

{% set binbags = "states.sensor.testbin_d.state" %}
{% set recycle = "states.sensor.testbin_e.state" %} 
{% if strptime(binbags, "%d/%m/%y") > strptime(recycle, "%d/%m/%y") %} 
Bin bags on {{ states.sensor.testbin_d.state }}
{% else %} 
Recycling on {{ states.sensor.testbin_e.state }}
{% endif %}  

which I went with because it allows me to keep my existing rest sensors.

Thanks :slight_smile: :slight_smile:

1 Like