I’m hoping someone can help me map a REST API response into a set of sensors. The API returns the following;
{
"data": [{
"regionid": 9,
"dnoregion": "UKPN East",
"shortname": "East England",
"postcode": "CM6",
"data": [{
"from": "2021-04-20T08:00Z",
"to": "2021-04-20T08:30Z",
"intensity": {
"forecast": 308,
"index": "high"
},
"generationmix": [{
"fuel": "biomass",
"perc": 3
},
{
"fuel": "coal",
"perc": 6.3
},
{
"fuel": "imports",
"perc": 0
},
{
"fuel": "gas",
"perc": 62.1
},
{
"fuel": "nuclear",
"perc": 13.4
},
{
"fuel": "other",
"perc": 0
},
{
"fuel": "hydro",
"perc": 0
},
{
"fuel": "solar",
"perc": 14.4
},
{
"fuel": "wind",
"perc": 0.7
}
]
}]
}]
}
So far, I’ve managed to map the following;
- platform: rest
resource: https://api.carbonintensity.org.uk/regional/postcode/CM6
scan_interval: 3600
name: Carbon
value_template: '{{ value_json.data[0].data[0].intensity.index }}'
json_attributes_path: '$.data[0].data[0].intensity'
json_attributes:
forecast
What I’d like to do though, is jump up a level and grab the from and to values into attributes, but if I change the json_attributes_path to ‘$.data[0].data[0]’ and then put intensity before forecase it stops working.
Additionally, I’d like to map all of the values in ‘generationmix’ into corresponding attributes. The problem here is that they are in an array and as such don’t map nicely.
Any guidance would be greatly appreciated.