Page 2 of 2

Re: How to use Data from a Dust Sensor

Posted: Saturday 13 November 2021 21:27
by jp1980
assenzuid wrote: Tuesday 03 July 2018 21:34 I have created a dummy device and some virtual sensors and use a LUA script.

capture_hardware.JPG

capture_devices.JPG

Code: Select all

local FQDN = 'IP DOMOTICZ'

return {
        active = true,
        on = {
                timer = { 'every minute' },
                httpResponses = { 'luftdatenRetrieved' } -- matches callback string below
        },
        execute = function(domoticz, item)

                if (item.isTimer) then
                        domoticz.openURL({
                                url = 'http://' .. FQDN .. '/data.json',
                                method = 'GET',
                                callback = 'luftdatenRetrieved'
                        })

                elseif (item.isHTTPResponse) then
                        if (item.ok and item.isJSON) then -- statusCode == 2xx
                                if tonumber(item.json.age) < 60 then
-- 1: SDS_P1 PM10, 2: SDS_P2 PM2.5, 3: DHT22 temp, 4: DHT22 hum, 5: BME280 temp, 6: BME280 hum, 7: BME280 baro
                                        domoticz.devices('Luftdaten PM10').updateCustomSensor(item.json.sensordatavalues[1].value)
                                        domoticz.devices('Luftdaten PM2.5').updateCustomSensor(item.json.sensordatavalues[2].value)
                                        domoticz.devices('Luftdaten DHT22').updateTempHum(item.json.sensordatavalues[3].value,item.json.sensordatavalues[4].value,0)
                                        domoticz.devices('Luftdaten BME280').updateTempHumBaro(item.json.sensordatavalues[5].value,item.json.sensordatavalues[6].value,0,(item.json.sensordatavalues[7].value/100),0)
                                end
                        else
                                -- oops
                                domoticz.log('Error fetching Luftdaten data', domoticz.LOG_ERROR)
                                domoticz.log(item.data, domoticz.LOG_ERROR)
                        end
                end
        end
}
I'm also publishing the valuses at my site.
http://emmenzuidwest.nl/weather/index.p ... #data-area
Thanks, that helped me a lot! I'm a developer although new to dzVents and LUA. Yesterday my dust sensor died although it was still providing humidity and temperature readings. What happened is that the temperature ended up in the PM2.5 sensor. So I decided to make the script more robuust and also check the value_type. Also earlier I noticed the dust sensor produces a lot of 99.9 values which were always odly out of bounds so I started to ignore these values as well.

It was a bit challenging to understand how to create functions inside functions but every language has its perks :D

Here is my code hope it proves handy for other people using dust sensor, it also is a bit easier to adapt if you have other kind or additional sensors.

Remember to change your device names and IP adres.

Now I can continue to investigate what is wrong with the dust sensor.

Code: Select all

return {
    active = true,
    on = {
            timer = { 'every minute' },
            httpResponses = { 'luftdatenRetrieved' } -- matches callback string below
    },

    logging =   {   level     =   domoticz.LOG_ERROR,
                    marker    =   "luftdaten"    },   


    execute = function(dz, item)
        local function FindValueFromArray(ValueArray, ValueName)
            local emptyvar
            for ArrayValueID = 1, #ValueArray do
                if ValueArray[ArrayValueID].value_type == ValueName then
                    return ValueArray[ArrayValueID].value
                end
            end 
            return emptyvar
        end
        
        local function IsValidPMMeasurement(InputValue)
            if (InputValue == nil) then
                return false
            end
            if (InputValue == 99.9) then
                return false
            end
            if (InputValue == 0) then
                return false
            end
            return true
        end

        if (item.isTimer) then
            dz.openURL({
                url = 'http://airrohr-14319147/data.json',
                    method = 'GET',
                    callback = 'luftdatenRetrieved'
                })
        elseif (item.isHTTPResponse) then
            if (item.ok and item.isJSON) then -- statusCode == 2xx
                if tonumber(item.json.age) < 60 then
                    local temperature = tonumber(FindValueFromArray(item.json.sensordatavalues, 'temperature'))
                    local humidity = tonumber(FindValueFromArray(item.json.sensordatavalues, 'humidity'))
                    local PM100 = tonumber(FindValueFromArray(item.json.sensordatavalues, 'SDS_P1'))
                    local PM25 = tonumber(FindValueFromArray(item.json.sensordatavalues, 'SDS_P2'))

                    if IsValidPMMeasurement(PM100) then
                        dz.devices('Fijnstof PM10').updateCustomSensor(PM100)
                    end
                    if IsValidPMMeasurement(PM25) then
                        dz.devices('Fijnstof PM2.5').updateCustomSensor(PM25)
                    end
                    if (temperature < 100) then
                        dz.devices('Weerstation').updateTempHum(temperature, humidity, 0)
                    end
                end
            else
                -- oops
                dz.log('Error fetching Luftdaten data', dz.LOG_ERROR)
                dz.log(item.data, dz.LOG_ERROR)
            end
        end
    end

}