dzVents parsing JSON sensor community data  [SOLVED]

Moderator: leecollings

Post Reply
Lammert
Posts: 7
Joined: Sunday 22 November 2020 20:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

dzVents parsing JSON sensor community data

Post by Lammert »

I’m new on this forum and could do with some help to implement json decoding for 2 virtual sensors.
The data is read using the following link:

http://data.sensor.community/airrohr/v1/sensor/52186/

Right now I read the entire string and cut it in parts using basic string commands in dzVents. Since the data is not fixed in place it was somewhat cumbersome to decode. Although my script runs I get the occasional error like: 020-11-22 18:30:30.342 Error: dzVents: Error: (3.0.2) ------ Finished Fijnstof #1 after >16 seconds. (using 1.525 seconds CPU time !)

I’m sure there is a better way and I highly would appreciate a solution using direct dzVents decoding in JSON.
Since I cannot get my head arround the JSON solution I hope someone in the forum can help me with the script?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents parsing JSON sensor community data

Post by waaren »

Lammert wrote: Sunday 22 November 2020 21:18 I’m new on this forum and could do with some help to implement json decoding for 2 virtual sensors.
Happy to help and it seems straight forward JSON. What do you want to do with the data ? (what data to what sensors ?)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Lammert
Posts: 7
Joined: Sunday 22 November 2020 20:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents parsing JSON sensor community data

Post by Lammert »

thanks Waaren
The P1 value is stored in a vitrual sensor called PM10 and the P2 value is stored in virtual sensor with the name PM2.5
regards,
Lammert
Lammert
Posts: 7
Joined: Sunday 22 November 2020 20:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents parsing JSON sensor community data

Post by Lammert »

The sensor is installed in my garden as part of an air pollution monotoring initiave and ultimately I would like to assess the relative performaince against other nearby sensors. Once I know how to use JSON decoding effectively I should be able to do that in Domotiz.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents parsing JSON sensor community data

Post by waaren »

Lammert wrote: Sunday 22 November 2020 22:11 The P1 value is stored in a virtual sensor called PM10 and the P2 value is stored in virtual sensor with the name PM2.5
Can you check this one?

Code: Select all

local scriptVar = 'Air quality'

return
{
    on =
    {
        timer =
        {
           'every 5 minutes', -- adjust to your needs
        },
        devices =
        {
            -- 'JSONTrigger', -- only used for test / development/ Can be ignored
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        local pm10 = dz.devices('PM10') -- define as air quality
        local pm25 = dz.devices('PM2.5') -- define as air quality

        if item.isTimer or item.isDevice then
            dz.openURL({
                url = 'http://data.sensor.community/airrohr/v1/sensor/52186/',
                callback = scriptVar,
            })
            return
        end

        if item.isHTTPResponse and item.isJSON then
            -- dz.utils.dumpTable(item.json) -- Only for debug purposes
            local latestAirQuality = item.json[1].sensordatavalues

            pm10.updateAirQuality(latestAirQuality[1].value)
            pm25.updateAirQuality(latestAirQuality[2].value)

        else
            domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
            domoticz.log(item, domoticz.LOG_ERROR)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Lammert
Posts: 7
Joined: Sunday 22 November 2020 20:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents parsing JSON sensor community data  [SOLVED]

Post by Lammert »

Many thanks @waaren works super!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest