Value_template selection criteria

Hi all ninjas.

(Same question from my friend Erik, Help with suggestions, Rest, json, attributes)

Yes, COVID-19 status of regions in Sweden … :confused:

The question at hand: How can I create template sensors based on the “kod” of each region?

And sorry if my lingo is not according to json nor jinja2 standard … :confused:

Data from: https://www.svt.se/special/articledata/2322/sverige.json comes in json sorted based on numbers of infected (antal) per region (kod). The most effected region will come first (data[0]) and so on.

It is easy to pick out for example the region (kod) with most effected (antal) using:
value_template: '{{ value_json.data[0].antal }}'

How can I assure that the region “Stockholm” always is the first one in the data array? The only thing that can be used for selection is “kod” and that is only an attribute not a topic.

I want to create a value template that searches for “kod” and picks the “antal” as the value to insert into the sensor with the name accordingly.

First the rest sensor, takes all “data” and puts that into the attributes of the rest sensor.

- platform: rest
  resource: https://www.svt.se/special/articledata/2322/sverige.json
  name: COVID-19-svt
  scan_interval: 15
  json_attributes:
    - total
    - data
  value_template: '{{ value_json.data_updated }}'

Then to the template sensor (that I have not yet cracked)

- platform: template
  sensors:
    covid_19_sverige_regionstockholm: 
      value_template: '{{ states.sensor.covid_19_svt.attributes["data"][**?????**] }}'
      unit_of_measurement: 'Personer'
    covid_19_sverige_regionvarmland: 
      and so on....
      ......
{
  "data": [
    {
      "antal": 267,
      "dead": 1,
      "kod": "01",
      "namn": "Stockholm"
    },
    {
      "antal": 116,
      "dead": 0,
      "kod": "14",
      "namn": "Västra Götaland"
    }
  ],
  "data_updated": "13 mars 2020 09.19",
  "total": 688
}

Full info here: https://pastebin.com/G95cVa5m

Just change the name for each one.

    covid_19_sverige_regionstockholm: 
      value_template: >
        {% set name = 'Stockholm' %}
        {% set value = state_attr('sensor.covid_19_svt', 'data') | selectattr('namn','eq',name) | list | first %}
        {{ value.kod }}

If you want other data, just change value.kod to whatever attribute you want… value.antal, value.dead

2 Likes

Guess what! It works like A CHARM!

Thanks a million for your support and knowledge! <3

@Naesstrom … Here is the solution!

Awsome! I’m trying it out right now!
This might be a really stupid question but is it possible to get more then one value into the same sensor?
For example one sensor that have both antal and dead

as attributes or in the state?

As attributes to the template sensor.

Here is the complete sensor package BTW. :slight_smile:

Those wont update realtime, only at startup, need to add entity_id: sensor.whatever_your_rest_sensor_is to all of them.

Thanks for your great support @petro

1 Like

Is this data source down? At me the same number is repeated every day. Any one know how to get the latest numbers that I can see here https://www.svt.se/datajournalistik/har-sprider-sig-coronaviruset/

Yep, the JSON source is no longer.
It has been replaced by one in a totally other format. I havnt found the time to tackle the new structure.

https://www.svt.se/special/articledata/2322/folkhalsomyndigheten.json

Here is the updated package gist based on the current SVT´s data sources.