Page 1 of 1

dzVents script for getting pollen data via Google Pollen API

Posted: Friday 09 February 2024 13:18
by dzdm
Hi,

I've created and attached a simple LUA dzVents script (I've had to rename it from pollen.lua to pollen.txt) which fetches the current pollen data from the Google Pollen API (https://developers.google.com/maps/docu ... n/overview) and updates some (pre-created) custom devices (see deviceMapping). The LUA script must be configured:

API KEY (you get this one from google)
LAT (latitude)
LONG (longitude)
LANG (language, currently not really used)

The LUA script fetches the pollen data once per hour and updates the corresponding devices (see deviceMapping), this devices must be created manually (Create Virtual Sensor and use Custom Sensor as device type). The "TOTAL" device is set to the average UPI of all devices with a UPI > 0.

Feel free (as in free beer) to use this :)

Edit WalterVl

Code: Select all

return {
	active = true,
	on = {
		timer = {
			'every hour'
		},
        httpResponses = {
            'pollenCallback'
        }
	},
	logging = {
		marker = 'pollen'
	},
	execute = function(dz, item, triggerInfo)
        -- Configuration
        local cfg = {
            URL           = 'https://pollen.googleapis.com/v1/forecast:lookup',
            APIKEY        = '<API KEY>',
            LAT           = '<LAT>',
            LONG          = '<LONG>',
            LANG          = 'en',
        }

        -- Device mapping (code to device id)
        local deviceMapping = {
            HAZEL         = 2572,
            ASH           = 2573,
            COTTONWOOD    = 2574,
            OAK           = 2575,
            PINE          = 2576,
            BIRCH         = 2577,
            OLIVE         = 2578,
            GRAMINALES    = 2579,
            RAGWEED       = 2580,
            ALDER         = 2581,
            MUGWORT       = 2582, 
            TOTAL         = 2583
        }

	if (item.isTimer) then
			dz.log('Pollen')
            -- Request pollen data
            local url = cfg.URL .. 
                        '?key=' .. cfg.APIKEY .. 
                        '&location.latitude=' .. cfg.LAT .. 
                        '&location.longitude=' .. cfg.LONG .. 
                        '&languageCode=' .. cfg.LANG ..
                        '&plantsDescription=0' ..
                        '&days=1'

            dz.openURL({
                url = url,
                method = 'GET',
                callback = 'pollenCallback'
            });
        elseif (item.isHTTPResponse) then
            if (item.ok and item.isJSON) then
                local json = dz.utils.fromJSON(item.data)

                if (json and json.dailyInfo) then
                    local plantInfos = json.dailyInfo[1].plantInfo
                    local totalUPI = 0
                    local upiCount = 0;

                    -- Iterate over all plants
                    for i, plant in pairs(plantInfos) do
                        local upi = 0

                        if (plant.inSeason and plant.inSeason == true and plant.indexInfo and plant.indexInfo.value) then
                            upi = plant.indexInfo.value
                        end

                        dz.log('Processing plant: ' .. plant.code .. ' => UPI: ' .. upi)

                        if(upi > 0) then
                            totalUPI = totalUPI + upi
                            upiCount = upiCount + 1
                        end

                        if (deviceMapping and deviceMapping[plant.code]) then
                            -- Get domotics device
                            local dev = dz.devices(deviceMapping[plant.code])

                            if (dev) then
                                dev.updateCustomSensor(upi)
                                dz.log('UPI for plant ' .. plant.code .. ' has been set to: ' .. upi)
                            else
                                dz.log('Error: Device mapping found for plant ' .. plant.code .. ' but device does not exist in domoticz')
                            end
                        else
                            dz.log('Error: No device mapping found for plant ' .. plant.code)
                        end
                    end

                    -- Update total upi
                    local dev = dz.devices(deviceMapping['TOTAL'])
                    if(upiCount > 0) then
                        totalUPI = totalUPI / upiCount
                    end

                    dz.log('Setting total UPI: ' .. totalUPI .. ' (upiCount: ' .. upiCount .. ')')
                    dev.updateCustomSensor(totalUPI)
                else
                    dz.log('Error: Invalid JSON structure')
                end
            else
                dz.log('Errror: ' .. item.statusText .. ' (' .. item.statusCode .. ')')
            end
		end
	end
}

Re: dzVents script for getting pollen data via Google Pollen API

Posted: Friday 09 February 2024 13:38
by waltervl
Thanks. I editted your post and put the output as a script block

Re: dzVents script for getting pollen data via Google Pollen API

Posted: Friday 09 February 2024 13:46
by dzdm
waltervl wrote: Friday 09 February 2024 13:38 Thanks. I editted your post and put the output as a script block
Thank you!