Value_template: Sort JSON array

So a little context: i found an API that returns the Garbage Collection for my city in a JSON array, NICE! One problem though it returns the dates in some weird order, so i’m having some trouble determining which collection is next… (REST sensor)

Any help on sorting the output or an idea how i get the right entry?

example output:

[
   { 
      "allDay":true,
      "color":"brown",
      "start":"2019-08-08T00:00:00+02:00",
      "title":"P-K"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-09T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-16T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"blue",
      "start":"2019-08-21T00:00:00+02:00",
      "title":"PMD"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-23T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-30T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-01-04T00:00:00+01:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"lightgreen",
      "start":"2019-01-04T00:00:00+01:00",
      "title":"Kerstboom"
   },
   { 
      "allDay":true,
      "color":"blue",
      "start":"2019-01-09T00:00:00+01:00",
      "title":"PMD"
   }
]

If the array is called value this will sort its items (ascending mode) using start as the key.

{{ value | sort(attribute='start') }}

Paste this into the Template Editor and experiment with it:

{% set value = [
   { 
      "allDay":true,
      "color":"brown",
      "start":"2019-08-08T00:00:00+02:00",
      "title":"P-K"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-09T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-16T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"blue",
      "start":"2019-08-21T00:00:00+02:00",
      "title":"PMD"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-23T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-08-30T00:00:00+02:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"grey",
      "start":"2019-01-04T00:00:00+01:00",
      "title":"Restafval"
   },
   { 
      "allDay":true,
      "color":"lightgreen",
      "start":"2019-01-04T00:00:00+01:00",
      "title":"Kerstboom"
   },
   { 
      "allDay":true,
      "color":"blue",
      "start":"2019-01-09T00:00:00+01:00",
      "title":"PMD"
   }
] %}

{{ value | sort(attribute='start') }}

1 Like

Thnx man i had no idea there was a template editor, let alone a link to the full list of expression! I didnt find the perfect solution yet but this helped me a lot!

1 Like

Just a small follow up question, how do you stop a loop? it looks like break is not supported in HA and you cant set values inside a loop… How do i stop this loop after the first output?

{% for item in value | sort(attribute='start') %}
  {% if as_timestamp(now()) <= as_timestamp(item.start) %}
    {{ item.title }}
  {% endif %}
{%- endfor %}
{% for item in value | sort(attribute='start') %}
  {% if as_timestamp(now()) <= as_timestamp(item.start) and loop.first %}
    {{ item.title }}
  {% endif %}
{%- endfor %}