Page 1 of 1

dzVents parsing JSON sensor community data

Posted: Sunday 22 November 2020 21:18
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?

Re: dzVents parsing JSON sensor community data

Posted: Sunday 22 November 2020 21:48
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 ?)

Re: dzVents parsing JSON sensor community data

Posted: Sunday 22 November 2020 22:11
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

Re: dzVents parsing JSON sensor community data

Posted: Sunday 22 November 2020 22:16
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.

Re: dzVents parsing JSON sensor community data

Posted: Sunday 22 November 2020 22:48
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
}

Re: dzVents parsing JSON sensor community data  [SOLVED]

Posted: Monday 23 November 2020 8:51
by Lammert
Many thanks @waaren works super!!