Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95919

Value_template selection criteria

$
0
0

@tubalainen wrote:

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

Posts: 8

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 95919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>