I like to think I am 80% there, but I need someone with the knowhow to finalize the code.
The URL/API gets a Json dataset, table. And for every row in the dataset, I want to create a sensor, and update the state and attributes at the selected time interval.
So as a N00B, trying to do something way over my skill level, - which is still on the “Hello world” level, did set up Windows 10, with Visual Studio and Python. Managed to get the data set trough the URL/API. Python code for getting dataset:
import requests
import json
url = 'https://api.nilu.no/aq/utd.json?areas=Lillehammer;Oslo&stations=Bankplassen;Manglerud'
myData = json.loads(requests.get(url).text)
for i in range(len(myData)):
zone = myData[i]['zone']
municipality = myData[i]['municipality']
station = myData[i]['station']
component = myData[i]['component'].replace(".","")
fromTime = myData[i]['fromTime']
toTime = myData[i]['toTime']
value = myData[i]['value']
unit = myData[i]['unit']
index = myData[i]['index']
color = myData[i]['color']
latitude = myData[i]['latitude']
longitude = myData[i]['longitude']
sensor_name = ("sensor.nilu_" + municipality + "_" + station + "_" + component).lower()
print(sensor_name,":", value)
print("ATTRIBUTES:")
print("zone:",zone)
print("municipality:",municipality)
print("station:",station)
print("unit:",unit)
print("index:",index)
print("color:",color)
print("latitude:",latitude)
print("longitude:",longitude)
print()
Json dataset result:
[{
"zone": "Stor-Oslo",
"municipality": "Oslo",
"area": "Oslo",
"station": "Manglerud",
"eoi": "NO0071A",
"component": "PM10",
"fromTime": "2018-09-18T13:00:00+01:00",
"toTime": "2018-09-18T14:00:00+01:00",
"value": 28.38,
"unit": "µg/m³",
"index": 1,
"color": "6ee86e",
"latitude": 59.898690,
"longitude": 10.814950
}, {
"zone": "Øst og Sørlandet",
"municipality": "Lillehammer",
"area": "Lillehammer",
"station": "Bankplassen",
"eoi": "NO0074A",
"component": "PM10",
"fromTime": "2018-09-18T13:00:00+01:00",
"toTime": "2018-09-18T14:00:00+01:00",
"value": 21.34,
"unit": "µg/m³",
"index": 1,
"color": "6ee86e",
"latitude": 61.112880,
"longitude": 10.464970
},etc.
Running the code, the print out, is what I am trying to do in HA. There is no manipulation of the dataset, so getting this to work, will make a great template for others, who want to develop sensors for HA.
The result:
The data set is air quality measurements, from measuring stations, scattered around Norway. If I can get this sensor working, I´ll try to make sensors for other countries too.
sensor:
- platform: nilu
locations:
- Lillehammer,Bankplassen
- Oslo, Manglerud
scan_interval:
minutes: 30
If someone out there has the time to finalize my 80% code, I would be a happy camper.
80% NILU air quality code:
Cheers.