[SOLVED] Air pollution data from luftdaten.info

Hi,

yes it seems to be accessible with JSON and HTTP:
http://luftdaten.info/faq/#toggle-id-7

You should be able to access the values of a specific sensor by, as the FAQ say:
http://api.luftdaten.info/v1/sensor/sensorid/

Cheers

Thanks for all replies.
I did install the HTTP binding. But I’m struggeling with configuration and use.

What did I try:
Using the Paper UI I tried to add anew thing based on the HTTP binding. All I get is an empty screen with no chance to insert anything.
I tried to configure a new thing using the config file /etc/openhab2/services/http.cfg
There I added two lines

luftdaten_3361.url=“http://api.luftdaten.info/v1/sensor/3361/
luftdaten_3361.updateInterval=60000
But I can’t find a thing in the configuration interface of the paper UI.
What do I do in the wrong way ?

HTTP binding is a 1.x version binding. You will need to create Items with a binding configuration as documented in the binding’s readme. I don’t think you can create Items for 1.x bindings in PaperUI.

1.x bindings do not support Things and if it did, you would create the Thing in a .things file in the things folder. The services/http.cfg is, as documented for the binding, a place to configure a way for the binding to make one request to the HTTP service the result of which can be used to populate multiple different Items.

http://docs.openhab.org/addons/bindings/http1/readme.html

Thanks for that solution proposal. I tried it and it is working fine.
But here is my next question.
With that rule I just create an entry in the logfile giving me the value.
How can I get value in a thing or item (I still confused how to distinguish them)?

I also tried to write a script which captures the api-result in a file and cat that file in a thing using a json transformation

JSONPATH( “$[0].sensordatavalues[0].value”, json)

But this approach just fires an error

[ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while transforming ‘[{“id”:240651585,“sampling_rate”:null,“timestamp”:“2017-08-05 09:39:26”,“location”:{“id”:16,“latitude”:“xxxxx”,“longitude”:“xxxx”,“country”:“DE”},“sensor”:{“id”:3361,“pin”:“7”,“sensor_type”:{“id”:9,“name”:“DHT22”,“manufacturer”:“various”}},“sensordatavalues”:[{“id”:554239711,“value”:“24.70”,“value_type”:“temperature”},{“id”:554239712,“value”:“61.30”,“value_type”:“humidity”}]},{“id”:240655278,“sampling_rate”:null,“timestamp”:“2017-08-05 09:42:10”,“location”:{“id”:1693,“latitude”:“xxx”,“longitude”:“xxx”,“country”:“DE”},“sensor”:{“id”:3361,“pin”:“7”,“sensor_type”:{“id”:9,“name”:“DHT22”,“manufacturer”:“various”}},“sensordatavalues”:[{“id”:554247472,“value”:“24.90”,“value_type”:“temperature”},{“id”:554247473,“value”:“61.30”,“value_type”:“humidity”}]}]’ with ‘JSONPATH( “$[0].sensordatavalues[0].value”, json)’ : ‘An error occurred while transforming JSON expression.’

After searching for documentation and lot of try&error I found a solution which returns me the desired data.
I added an item

String StrTest1 “Sensor 3361: [%s]”

Then I extened the rule by an postUpdate. I extracted this idea from How to display the min & max value of an item.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

val String Station_ID=“3361”
// Using JSONPath

// Set the name of the rule
rule “Reading AirPollution”

// When cron job execution
when Time cron “0 */2 * * * ?” //every 5 Minutes at Second 0
then
var String URL= “http://api.luftdaten.info/v1/sensor/” + Station_ID + “/”
var String json = sendHttpGetRequest(URL)
var String P10
var String P2
var String Unit = " (Unit)"
var String returnvalue = “Wert Rückgabe”

// logInfo(“Reading AirPollution”," P1 first Reading: {}“,transform(“JSONPATH”, “$[0].sensordatavalues[0].value”, json))
// logInfo(“Reading AirPollution”,” P2 first Reading: {}“,transform(“JSONPATH”, “$[0].sensordatavalues[1].value”, json))
// logInfo(“Reading AirPollution”,” timestamp: {}“,transform(“JSONPATH”, “$[0].sensordatavalues[1].timestamp”, json))
// logInfo(“Reading AirPollution”,” P1 second Reading: {}“,transform(“JSONPATH”, “$[1].sensordatavalues[0].value”, json))
// logInfo(“Reading AirPollution”,” P2 second Reading: {}",transform(“JSONPATH”, “$[1].sensordatavalues[1].value”, json))

P10=transform(“JSONPATH”, “$[0].sensordatavalues[0].value”, json)
P2=transform(“JSONPATH”, “$[0].sensordatavalues[1].value”, json)

// calculate the return value (string)
returnvalue = "P10: " + P10 + Unit + “” + " - P2,5: " + P2 + Unit

// Update the item for that value
postUpdate(StrTest1 , returnvalue)
logInfo(“Reading AirPollution”," return value: {}", returnvalue)

Not sure whether this is a good and/or nice solution.

My posted solution was for the reply with a single sensor. If there are more sensors, you have to separate the needed one Can’t help atm because I’m on the road, sorry.
You have found a solution, that is good. You do handle the returns as strings, if you would convert them to numbers you could start persisting them for a chart display.

Yes, I’m aware of that. My idea was to start with a string to avoid any trouble when converting to a number. And I was quite happy when I the string was finally displayed in a panel.
Persistance is a topic that I will start later on as it is not easy to understand the world of OH2: the documentation is quite sparse for a newbie like me and partly outdated (for OH1 only).

Hi @openhabiger:

Have you been able to set everything using numbers?

Could you please share your items, rules and other requires stuff?
Any accelaration for my progress is greatly appreciated.
Thanks in advance.

Never mind.
I almost did it. :slight_smile:

The only issue I have is that the values of the sensor are stored the “german way” with a ","instead of a “.” as the decimal point.

How can I replace the “,” with a “.” before converting the string into a number?

@NCO
I defined a number item PM10, PM2 and an additional string StrTest1 and a rule is assigning the value to the number

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

val String Station_ID="3361"
// Using  JSONPath

// Set the name of the rule
rule "Reading AirPollution"

// When cron job execution
when Time cron "0 */2 * * * ?" //every 5 Minutes at Second 0
then
var String URL= "http://api.luftdaten.info/v1/sensor/" + Station_ID + "/"
var String json = sendHttpGetRequest(URL)
var String P10
var String P2
var String Unit = " (Unit)"
var String returnvalue = "Wert Rückgabe"


P10=transform("JSONPATH", "$[0].sensordatavalues[0].value", json)
P2=transform("JSONPATH", "$[0].sensordatavalues[1].value", json)

// calculate the return value (string)
returnvalue =  "P10: " + P10 + Unit + "" + " - P2,5: " + P2 + Unit

// Update the item for that value
postUpdate(PM10 , P10)
postUpdate(PM25 , P2)
postUpdate(StrTest1 , returnvalue)
logInfo("Reading AirPollution"," return value: {}", returnvalue)

end

I just checked my data.
It also displays with a dot.
I fear that OH2 is using the “international” representation with the dot.
Maybe somebody more experienced can answer that question how to adapt this to the German representation.

If you are in a rule you can use String methods to replace the , with .

You can do the same with a JS transform, but you can’t use to transforms at the same time so you will need to extract the value from the JSON and then replace the , with .

Hi @openhabiger, @rlkoshak,

thanks a lot for your help. I have it up an running :slight_smile:
I investigated a little more and found out that the persistence (in the beginning playing around with formats) stored PM10 as a string (VARCHAR) and PM2 as a number (DOUBLE) item in my sql DB.
Both have been shown in the logs with “.” - so there was no obvious difference.
I just recognized the “,” used in Germany was shown depending on the sql DB tool to display the data (this was the case with heidiSQL).
Accessing the DB with the iphone APP QueryDB, everything is shown with “.”

However, the problem I had, was that it took me a while until I recognized the different format in my SQL DB (I was not able to show the data of the string in charts of course).

The issue was, once initialized (by persistence) as string in my DB, the values have not been converted to number even after changing to the parseFloat format in my rule.
I first needed to delete the whole item entry in my DB to overcome this issue.
Now both are stored as DOUBLE and can be used for charts.

Again, thanks for your help.

Unfortunately there was obviously a change in JSONPATH in OH 2.2 stable (?)

I get:
Error executing the transformation 'JSONPATH': Invalid path '$[0].sensordatavalues[1]

Anyone who solved this already?

EDIT:
After a reboot the issue was solved.
In general my first impression about 2.2. is that it’s more sensitive to updates (rules / items etc) while running.

Hi All,

I read few times the thread as I want to use also data from luftdaten.info, but seems my level is to low. When configuring it as a Thing what should be the type_ID? Can you please help with how it should look like the Thing, Item and Rule files (and if required somewhere else - http.cfg?) adds to make it work?
HTTP binding installed and JSONPath Transformation too.

Thank you!

I paperUI enable legacy bindings and install the http binding 1.x version
Configure the http.cfg in your service folder as above
Then follow post #9

Thanks a lot! Followed your recommendations.
To http.cfg is added now:

luftdaten_6568.url="http://api.luftdaten.info/v1/sensor/6568/"
luftdaten_6568.updateInterval=60000

Items is:

String StrTest1 "Sensor 6568: [%s]"

Rules is as posted above, only Station ID is changed.

Sorry if asking something too obvious to others but not to me - what should be added as a text for Thing, and where to add this from above:
JSONPATH( “$[0].sensordatavalues[0].value”, json)

Thanks for your patience!

In the rule as post #9

Hi,

I stumbled across this threat and first of all thanks to the contributors of it above and the hint to luftdaten.info!
A nice addon to my weather data :slight_smile:

I just wanted to share my way of implementing it. I didn’t adopt the rule though, as I didn’t see the benefit of it for my implementation. But, I’m using your http.cfg for the below Items-file config:

Number Luftdaten_P1 "PM10: [%s µg/m³]" { http="<[luftdaten_6568:60000:JSONPATH($[0].sensordatavalues[0].value)]"}
Number Luftdaten_P2 "PM2,5: [%s µg/m³]" { http="<[luftdaten_6568:60000:JSONPATH($[0].sensordatavalues[1].value)]"}

Kurt

Thank you so much both of you for your help. I followed Kurt’s way and it’s working like charming.

Just connected my “feinstaub” sensor and used this items with http binding:

/* **************************
 * Feinstaub data
 * ************************** */
Number N_FS_SDS_PM10 "Partikelgröße 10µm [%.2f µg/m³]" { http="<[http://feinstaubsensor-14255834/data.json:80:JSONPATH($.sensordatavalues[0].value)]" }
Number N_FS_SDS_PM25 "Partikelgröße 2.5µm [%.2f µg/m³]" { http="<[http://feinstaubsensor-14255834/data.json:80:JSONPATH($.sensordatavalues[1].value)]" }
/* BME280 data*/
Number N_FS_Temperatur "Temperatur [%.2f °C]" <temperature> { http="<[http://feinstaubsensor-14255834/data.json:80:JSONPATH($.sensordatavalues[2].value)]" }
Number N_FS_Pressure "Luftdruck [%.2f hPa]" <pressure> { http="<[http://feinstaubsensor-14255834/data.json:80:JS(airpressure.js)]" }
Number N_FS_Humidity "Luftfeuchte [%.2f %%]" <water> { http="<[http://feinstaubsensor-14255834/data.json:80:JSONPATH($.sensordatavalues[4].value)]" }

I have a BME2080 sensor connected. The Humidity must be diveded by 100 to show hPa. I am doing this with a java transformation file airpressure.js:

(function(x) {
    var json = JSON.parse(x);
    return json.sensordatavalues[3].value/100;
})(input)

I hope its helpful for others…

1 Like