dzvents .json to device

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
gammalikker
Posts: 2
Joined: Saturday 02 October 2021 17:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

dzvents .json to device

Post by gammalikker »

Hello, im trying to make a script that reads a json file from http (example at below) and send those values to the appropriate devices in this case a pm2.5, pm10, humidity and a temperature sensor. i tried using this script https://domoticz.com/forum/viewtopic.php?t=26021script that i found in another thread but i can't get it to work. i have been searching for an answer for hours now but i can't find anything im a total newbie at coding but this didn't sound too difficult to do but apparently i was wrong :oops:.

Domoticz log:

Code: Select all

 
 2021-10-02 17:33:00.509 Status: dzVents: Info: Air quality: ------ Start internal script: airrohr:, trigger: "every 1 minutes"
2021-10-02 17:33:00.536 Status: dzVents: Debug: Air quality: Processing device-adapter for airrohr PM10: Custom sensor device adapter
2021-10-02 17:33:00.537 Status: dzVents: Debug: Air quality: Processing device-adapter for airrohr PM2.5: Custom sensor device adapter
2021-10-02 17:33:00.537 Status: dzVents: Debug: Air quality: OpenURL: url = http://192.168.1.152/data.json
2021-10-02 17:33:00.537 Status: dzVents: Debug: Air quality: OpenURL: method = GET
2021-10-02 17:33:00.537 Status: dzVents: Debug: Air quality: OpenURL: post data = nil
2021-10-02 17:33:00.537 Status: dzVents: Debug: Air quality: OpenURL: headers = nil
2021-10-02 17:33:00.537 Status: dzVents: Debug: Air quality: OpenURL: callback = Air quality
2021-10-02 17:33:00.537 Status: dzVents: Info: Air quality: ------ Finished airrohr
2021-10-02 17:33:00.537 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-10-02 17:33:00.666 Status: dzVents: Info: Handling httpResponse-events for: "Air quality"
2021-10-02 17:33:00.667 Status: dzVents: Info: Air quality: ------ Start internal script: airrohr: HTTPResponse: "Air quality"
2021-10-02 17:33:00.680 Status: dzVents: Debug: Air quality: Processing device-adapter for airrohr PM10: Custom sensor device adapter
2021-10-02 17:33:00.681 Status: dzVents: Debug: Air quality: Processing device-adapter for airrohr PM2.5: Custom sensor device adapter
2021-10-02 17:33:00.681 Status: dzVents: Info: Air quality: ------ Finished airrohr
2021-10-02 17:33:00.681 Error: dzVents: Error: (3.0.2) Air quality: An error occurred when calling event handler airrohr
2021-10-02 17:33:00.681 Error: dzVents: Error: (3.0.2) Air quality: ...i/domoticz/scripts/dzVents/generated_scripts/airrohr.lua:41: attempt to index a nil value (field '?') 
This is the code i got so far:

Code: Select all

local scriptVar = 'Air quality'

return
{
    on =
    {
        timer =
        {
           'every 1 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('airrohr PM10') -- define as air quality
        local pm25 = dz.devices('airrohr PM2.5') -- define as air quality

        if item.isTimer or item.isDevice then
            dz.openURL({
                url = 'http://192.168.1.152/data.json',
                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[0].value)
            pm25.updateAirQuality(latestAirQuality[1].value)
        else
            domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
            domoticz.log(item, domoticz.LOG_ERROR)
        end
    end
    
JSON im trying to read

Code: Select all

	software_version	"NRZ-2020-133"
	age	"30"
sensordatavalues	
	0	
		value_type	"SDS_P1"
		value	"9.95"
	1	
		value_type	"SDS_P2"
		value	"3.60"
	2	
		value_type	"BME280_temperature"
		value	"23.11"
	3	
		value_type	"BME280_pressure"
		value	"100378.88"
	4	
		value_type	"BME280_humidity"
		value	"56.71"
	5	
		value_type	"samples"
		value	"5058621"
	6	
		value_type	"min_micro"
		value	"28"
	7	
		value_type	"max_micro"
		value	"20037"
	8	
		value_type	"interval"
		value	"145000"
	9	
		value_type	"signal"
		value	"-42"
gammalikker
Posts: 2
Joined: Saturday 02 October 2021 17:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents .json to device

Post by gammalikker »

As usual i found the answer to my question as soon as i asked it... i found this script and it worked instantly :oops:
viewtopic.php?t=23406&start=20
Pjedr
Posts: 38
Joined: Friday 27 December 2013 3:13
Target OS: Linux
Domoticz version: Beta
Location: Friesland
Contact:

Re: dzvents .json to device

Post by Pjedr »

I use "jq" for .json data, for example:

battery_plugged=$(cat $sTempDir/wired_battery.json | jq -r '.data.attributes.plugStatus')
if [ "$battery_plugged" == "1" ]; then battery_plugged='On'; else battery_plugged='Off'; fi

sudo apt-get install jq
Test it works! Try this to see it pretty print some example json
echo '{ "name":"John", "age":31, "city":"New York" }' | jq
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest