Extract value from http response  [Solved]

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

Moderator: leecollings

Post Reply
Neuvidor2307
Posts: 6
Joined: Wednesday 07 March 2018 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Extract value from http response

Post by Neuvidor2307 »

Dear all,

In order to follow my internet access, I want to get somes values from my internet box "SFR NB6" API and store these data into Domoticz.

The API request is :

Code: Select all

http://192.168.1.1/api/1.0/?method=dsl.getInfo
The API answer is :

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" version="1.0">
<dsl status="up" linemode="ADSL2+" uptime="137177" counter="1" crc="0" noise_down="6.4" noise_up="9.2" attenuation_down="22.0" attenuation_up="12.0" rate_down="23619" rate_up="1023" line_status="No Defect" training="Showtime" />
</rsp>
The data I want to extract :
- rate_down
- rate_up
- attenuation_down
- attenuation_up
- noise_down
- noise_up
- crc
- Counter

Could you please help me to write a dzVents script ?

My expectation is to use something similar as my below script used for Json data :

Code: Select all

local idxCloudCover = 112      -- (Integer) Device ID of device holding cloudcoverage
local idxBarometer  = 113      -- (Integer) Device ID of device barometric presusure
local idxPrecipIntensity  = 131      -- (Integer) Device ID of device barometric presusure

local ApiKey = '79947d33c8097e153533ce959hsjeldi'  -- (string) API key for Darksky
local GPSCoordinates = '47.454062,8.846521'         -- (string) City GPS coordinates (lat, long)

local Debug = true


return {
   on = {
      timer = { 'every 4 minutes' },
      httpResponses = { 'result' }
   },
   execute = function(domoticz, item)
      if (item.isTimer) then
         domoticz.openURL({
            --url = "https://api.openweathermap.org/data/2.5/weather?id=".. CityID .."&appid=" .. ApiKey,
            url = "https://api.darksky.net/forecast/" .. ApiKey .. "/" .. GPSCoordinates,
            method = 'GET',
            callback = 'result'
         })
      elseif (item.isJSON) then
         if (item.ok) then -- statusCode == 2xx
            -- Read Pressure
            --local pressure = item.json.main.pressure
            local pressure = item.json.currently.pressure
            domoticz.devices(idxBarometer).updateBarometer(domoticz.utils.round(pressure,0))

            -- Read Clouds
            --local CloudCover = item.json.clouds.all
            local CloudCover = item.json.currently.cloudCover
            CloudCover = CloudCover * 100
            domoticz.devices(idxCloudCover).updatePercentage(domoticz.utils.round(CloudCover,0))
            
            -- Read precipIntensity
            local PrecipIntensity = item.json.currently.precipIntensity
            PrecipIntensity = PrecipIntensity*100
            domoticz.devices(idxPrecipIntensity).updateRain(domoticz.utils.round(PrecipIntensity,0))
            
            
            --Debug
            if (Debug == true) then
                domoticz.log("pressure=" .. pressure .. "hPa")
                domoticz.log("CloudCover=" .. CloudCover .. "%")
                domoticz.log("precipIntensity=" .. PrecipIntensity .. "mm/h * 100")
            end
            
         end
      end
   end
}
Thanks in advance to all of you.
Last edited by Neuvidor2307 on Sunday 01 March 2020 0:41, edited 1 time in total.
Version: 2020.2 (last stable)
Platform: Raspberry PI 3 B+
Plugin/Hardware: RFXCOM + GCE IPX800v4 & 2x X-Dimmer & 1x X8R + DOORBIRD D101S + LEGRAND EcoCompteur + Teleinfo + ONKYO TX-NR626
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract value from http response

Post by waaren »

Neuvidor2307 wrote: Friday 28 February 2020 23:19 In order to follow my internet access, I want to get somes values from my internet box "SFR NB6" API and store these data into Domoticz.
You could try this one

Code: Select all

local scriptVar = 'getDSLInfo'

return
{
    on =
    {
        devices =
        {
            'xmlTrigger' , -- just for test
        },

        timer =
        {
            'every 4 minutes',
        },

        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if item.isTimer or item.isDevice then
            dz.openURL(
            {
                url = "http://192.168.1.1/api/1.0/?method=dsl.getInfo",
                method = 'GET',
                callback = scriptVar,
            })
        elseif item.ok then -- statusCode == 2xx
            -- convert xml to Table
            local rt =  dz.utils.fromXML(xml).rsp
            local dsl = rt.dsl._attr

            -- get one value
            dz.log('line-status: ' .. dsl.line_status,dz.LOG_DEBUG)

            -- get all values
            for key, value in pairs(dsl) do
                dz.log(key .. ': '.. value,dz.LOG_DEBUG)
            end
        else
            dz.log('problem retrieving data from modem',dz.LOG_ERROR)
            dz.log(item.data,dz.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
Neuvidor2307
Posts: 6
Joined: Wednesday 07 March 2018 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Extract value from http response

Post by Neuvidor2307 »

Thanks for your help.

I tried your script proposal but unfortunately it seems not working, nevertheless I'm progressing.
Indeed, I can log the complete answer of my HTTP request as a string but I can't get each value independantly even is domoticz seems recognize the answer as XML. If I'm trying to filter some value with the xml functions, I'm getting some table but they seems to be empty.

Please help me :)

My current script :

Code: Select all

return {
	on = {
		timer = {
			'every minute' -- just an example to trigger the request
		},
		httpResponses = {
			'trigger' -- must match with the callback passed to the openURL command
		}
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.1.1/api/1.0/?method=dsl.getInfo',
				method = 'GET',
				callback = 'trigger', -- see httpResponses above.
			})
		end

		if (item.isHTTPResponse) then

			if (item.statusCode == 200) then
				if (item.isXML) then
				    domoticz.log('The answer is XML')
				    
				    domoticz.log("HTTP answer1=" .. tostring(item))
				    domoticz.log("HTTP answer2=" .. tostring(item.data))
				    
				    local DATA1 = domoticz.utils.fromXML(item.data)
				    local DATA2 = domoticz.utils.fromXML(item.data).rsp
				    local DATA3 = domoticz.utils.fromXML(item.data).rsp.dsl
				    local DATA4 = item.xml.rsp
				    local DATA5 = item.xml.rsp.dsl

				    domoticz.log("DATA1=" .. tostring(DATA1))
				    domoticz.log("DATA2=" .. tostring(DATA2))
				    domoticz.log("DATA3=" .. tostring(DATA3))
				    domoticz.log("DATA4=" .. tostring(DATA4))
				    domoticz.log("DATA5=" .. tostring(DATA5))
				    
				    domoticz.log("DATA1b=" .. tostring(DATA1.data))
				    domoticz.log("DATA2b=" .. tostring(DATA2.data))
				    domoticz.log("DATA3b=" .. tostring(DATA3.data))
				    domoticz.log("DATA4b=" .. tostring(DATA4.data))
				    domoticz.log("DATA5b=" .. tostring(DATA5.data))
				else
				    domoticz.log('THE ANSWER IS NOT JSON')
				end
			else
				domoticz.log('ERROR')
			end

		end

	end
}
The corresponding log :

Code: Select all

2020-03-01 00:30:00.233 Status: dzVents: Info: ------ Start internal script: Lecture HTTP:, trigger: every minute
2020-03-01 00:30:00.233 Status: dzVents: Info: ------ Finished Lecture HTTP
2020-03-01 00:30:00.234 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-03-01 00:30:00.352 Status: dzVents: Info: Handling httpResponse-events for: "trigger"
2020-03-01 00:30:00.352 Status: dzVents: Info: ------ Start internal script: Lecture HTTP: HTTPResponse: "trigger"
2020-03-01 00:30:00.356 Status: dzVents: Info: The answer is XML
2020-03-01 00:30:00.356 Status: dzVents: Info: HTTP answer1=table: 0x6ec2cc10
2020-03-01 00:30:00.356 Status: dzVents: Info: HTTP answer2=<?xml version="1.0" encoding="UTF-8"?>
2020-03-01 00:30:00.356 <rsp stat="ok" version="1.0">
2020-03-01 00:30:00.356 <dsl status="up" linemode="ADSL2+" uptime="139817" counter="1" crc="0" noise_down="6.4" noise_up="9.3" attenuation_down="22.0" attenuation_up="12.0" rate_down="23619" rate_up="1023" line_status="No Defect" training="Showtime" />
2020-03-01 00:30:00.356
2020-03-01 00:30:00.356
2020-03-01 00:30:00.356 </rsp>
2020-03-01 00:30:00.356
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA1=table: 0x6ec0bf18
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA2=table: 0x6ec15210
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA3=table: 0x6ec158a8
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA4=table: 0x6ec15210
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA5=table: 0x6ec158a8
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA1b=nil
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA2b=nil
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA3b=nil
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA4b=nil
2020-03-01 00:30:00.359 Status: dzVents: Info: DATA5b=nil
2020-03-01 00:30:00.359 Status: dzVents: Info: ------ Finished Lecture HTTP
Version: 2020.2 (last stable)
Platform: Raspberry PI 3 B+
Plugin/Hardware: RFXCOM + GCE IPX800v4 & 2x X-Dimmer & 1x X8R + DOORBIRD D101S + LEGRAND EcoCompteur + Teleinfo + ONKYO TX-NR626
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract value from http response

Post by waaren »

Neuvidor2307 wrote: Sunday 01 March 2020 0:40 I tried your script proposal but unfortunately it seems not working, nevertheless I'm progressing.
I tested my script with the exact string you gave in your initial post and it worked without a problem.

Can you please try the script I posted and show your loglines ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Neuvidor2307
Posts: 6
Joined: Wednesday 07 March 2018 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Extract value from http response

Post by Neuvidor2307 »

Dear waaren,

Of course I can do it and I really appreciate your help.

Below your script with few modifications :
- I have deleted the both "]]" line 51 because the script was not accepted
- I have replace the timer "every 4 minutes" by "every minute"

Below the script I'm using :

Code: Select all

local scriptVar = 'getDSLInfo'

return
{
    on =
    {
        devices =
        {
            'xmlTrigger' , -- just for test
        },

        timer =
        {
            'every minute',
        },

        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if item.isTimer or item.isDevice then
            dz.openURL(
            {
                url = "http://192.168.1.1/api/1.0/?method=dsl.getInfo",
                method = 'GET',
                callback = scriptVar,
            })
        elseif item.ok then -- statusCode == 2xx
            -- convert xml to Table
            local rt =  dz.utils.fromXML(xml).rsp
            local dsl = rt.dsl._attr

            -- get one value
            dz.log('line-status: ' .. dsl.line_status,dz.LOG_DEBUG)

            -- get all values
            for key, value in pairs(dsl) do
                dz.log(key .. ': '.. value,dz.LOG_DEBUG)
            end
        else
            dz.log('problem retrieving data from modem',dz.LOG_ERROR)
            dz.log(item.data,dz.LOG_ERROR) --
        end
    end
}
Below the log :

Code: Select all

2020-03-01 09:30:00.347 Status: dzVents: Info: getDSLInfo: ------ Start internal script: Lecture HTTP:, trigger: every minute
2020-03-01 09:30:00.347 Status: dzVents: Debug: getDSLInfo: OpenURL: url = http://192.168.1.1/api/1.0/?method=dsl.getInfo
2020-03-01 09:30:00.347 Status: dzVents: Debug: getDSLInfo: OpenURL: method = GET
2020-03-01 09:30:00.347 Status: dzVents: Debug: getDSLInfo: OpenURL: post data = nil
2020-03-01 09:30:00.347 Status: dzVents: Debug: getDSLInfo: OpenURL: headers = nil
2020-03-01 09:30:00.347 Status: dzVents: Debug: getDSLInfo: OpenURL: callback = getDSLInfo
2020-03-01 09:30:00.347 Status: dzVents: Info: getDSLInfo: ------ Finished Lecture HTTP
2020-03-01 09:30:00.348 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-03-01 09:30:00.492 Status: dzVents: Info: Handling httpResponse-events for: "getDSLInfo"
2020-03-01 09:30:00.492 Status: dzVents: Info: getDSLInfo: ------ Start internal script: Lecture HTTP: HTTPResponse: "getDSLInfo"
2020-03-01 09:30:00.496 Status: dzVents: Info: getDSLInfo: ------ Finished Lecture HTTP
2020-03-01 09:30:00.496 Error: dzVents: Error: (2.5.7) getDSLInfo: An error occurred when calling event handler Lecture HTTP
2020-03-01 09:30:00.496 Error: dzVents: Error: (2.5.7) getDSLInfo: ...oticz/scripts/dzVents/generated_scripts/Lecture HTTP.lua:39: attempt to index a nil value
Version: 2020.2 (last stable)
Platform: Raspberry PI 3 B+
Plugin/Hardware: RFXCOM + GCE IPX800v4 & 2x X-Dimmer & 1x X8R + DOORBIRD D101S + LEGRAND EcoCompteur + Teleinfo + ONKYO TX-NR626
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract value from http response

Post by waaren »

Neuvidor2307 wrote: Sunday 01 March 2020 9:31 some bugs in the script...
Can you try this one ?

Code: Select all

local scriptVar = 'getDSLInfo'

return
{
    on =
    {
        devices =
        {
            'xmlTrigger' , -- just for test
        },

        timer =
        {
            'every minute',
        },

        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if item.isTimer or item.isDevice then
            dz.openURL(
            {
                url = "http://192.168.1.1/api/1.0/?method=dsl.getInfo",
                method = 'GET',
                callback = scriptVar,
            })
        elseif item.ok then -- statusCode == 2xx
            -- convert xml to Table
            local rt =  dz.utils.fromXML(item.data)

            dz.utils.dumpTable(rt)
            local dsl = rt.rsp.dsl._attr

            -- get one value
            dz.log('line-status: ' .. dsl.line_status,dz.LOG_DEBUG)

            -- get all values
            for key, value in pairs(dsl) do
                dz.log(key .. ': '.. value,dz.LOG_DEBUG)
            end
        else
            dz.log('problem retrieving data from modem',dz.LOG_ERROR)
            dz.log(item.data,dz.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
Neuvidor2307
Posts: 6
Joined: Wednesday 07 March 2018 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Extract value from http response

Post by Neuvidor2307 »

Dear Waaren,

Thanks a lot for your new script, it seems working well !!! :D
I need now to try to update a customize sensor with values. I will try to do it this week.

Running code :

Code: Select all

local scriptVar = 'getDSLInfo'

return
{
    on =
    {
        devices =
        {
            'xmlTrigger' , -- just for test
        },

        timer =
        {
            'every minute',
        },

        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if item.isTimer or item.isDevice then
            dz.openURL(
            {
                url = "http://192.168.1.1/api/1.0/?method=dsl.getInfo",
                method = 'GET',
                callback = scriptVar,
            })
        elseif item.ok then -- statusCode == 2xx
            -- convert xml to Table
            local rt =  dz.utils.fromXML(item.data)

            dz.utils.dumpTable(rt)
            local dsl = rt.rsp.dsl._attr

            -- get one value
            dz.log('line-status: ' .. dsl.line_status,dz.LOG_DEBUG)

            -- get all values
            for key, value in pairs(dsl) do
                dz.log(key .. ': '.. value,dz.LOG_DEBUG)
            end
        else
            dz.log('problem retrieving data from modem',dz.LOG_ERROR)
            dz.log(item.data,dz.LOG_ERROR) --
        end
    end
}
log :

Code: Select all

2020-03-01 23:25:00.143 Status: dzVents: Info: getDSLInfo: ------ Start internal script: Lecture HTTP:, trigger: every minute
2020-03-01 23:25:00.143 Status: dzVents: Debug: getDSLInfo: OpenURL: url = http://192.168.1.1/api/1.0/?method=dsl.getInfo
2020-03-01 23:25:00.143 Status: dzVents: Debug: getDSLInfo: OpenURL: method = GET
2020-03-01 23:25:00.143 Status: dzVents: Debug: getDSLInfo: OpenURL: post data = nil
2020-03-01 23:25:00.143 Status: dzVents: Debug: getDSLInfo: OpenURL: headers = nil
2020-03-01 23:25:00.143 Status: dzVents: Debug: getDSLInfo: OpenURL: callback = getDSLInfo
2020-03-01 23:25:00.143 Status: dzVents: Info: getDSLInfo: ------ Finished Lecture HTTP
2020-03-01 23:25:00.144 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-03-01 23:25:00.262 Status: dzVents: Info: Handling httpResponse-events for: "getDSLInfo"
2020-03-01 23:25:00.263 Status: dzVents: Info: getDSLInfo: ------ Start internal script: Lecture HTTP: HTTPResponse: "getDSLInfo"
2020-03-01 23:25:00.268 Status: dzVents: > rsp:
2020-03-01 23:25:00.268 Status: dzVents: > 1:
2020-03-01 23:25:00.268 Status: dzVents: > dsl:
2020-03-01 23:25:00.268 Status: dzVents: > _attr:
2020-03-01 23:25:00.268 Status: dzVents: > training: Showtime
2020-03-01 23:25:00.268 Status: dzVents: > crc: 0
2020-03-01 23:25:00.268 Status: dzVents: > uptime: 222317
2020-03-01 23:25:00.268 Status: dzVents: > line_status: No Defect
2020-03-01 23:25:00.268 Status: dzVents: > counter: 1
2020-03-01 23:25:00.268 Status: dzVents: > rate_up: 1023
2020-03-01 23:25:00.268 Status: dzVents: > status: up
2020-03-01 23:25:00.268 Status: dzVents: > noise_up: 9.3
2020-03-01 23:25:00.268 Status: dzVents: > noise_down: 6.1
2020-03-01 23:25:00.268 Status: dzVents: > linemode: ADSL2+
2020-03-01 23:25:00.268 Status: dzVents: > attenuation_down: 22.0
2020-03-01 23:25:00.268 Status: dzVents: > rate_down: 23619
2020-03-01 23:25:00.268 Status: dzVents: > attenuation_up: 12.0
2020-03-01 23:25:00.268 Status: dzVents: > _attr:
2020-03-01 23:25:00.268 Status: dzVents: > version: 1.0
2020-03-01 23:25:00.268 Status: dzVents: > stat: ok
2020-03-01 23:25:00.268 Status: dzVents: > dsl:
2020-03-01 23:25:00.268 Status: dzVents: > _attr:
2020-03-01 23:25:00.268 Status: dzVents: > training: Showtime
2020-03-01 23:25:00.268 Status: dzVents: > crc: 0
2020-03-01 23:25:00.268 Status: dzVents: > uptime: 222317
2020-03-01 23:25:00.268 Status: dzVents: > line_status: No Defect
2020-03-01 23:25:00.268 Status: dzVents: > counter: 1
2020-03-01 23:25:00.268 Status: dzVents: > rate_up: 1023
2020-03-01 23:25:00.269 Status: dzVents: > status: up
2020-03-01 23:25:00.269 Status: dzVents: > noise_up: 9.3
2020-03-01 23:25:00.269 Status: dzVents: > noise_down: 6.1
2020-03-01 23:25:00.269 Status: dzVents: > linemode: ADSL2+
2020-03-01 23:25:00.269 Status: dzVents: > attenuation_down: 22.0
2020-03-01 23:25:00.269 Status: dzVents: > rate_down: 23619
2020-03-01 23:25:00.269 Status: dzVents: > attenuation_up: 12.0
2020-03-01 23:25:00.269 Status: dzVents: > _attr:
2020-03-01 23:25:00.269 Status: dzVents: > version: 1.0
2020-03-01 23:25:00.269 Status: dzVents: > stat: ok
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: line-status: No Defect
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: training: Showtime
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: crc: 0
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: uptime: 222317
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: line_status: No Defect
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: counter: 1
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: rate_up: 1023
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: status: up
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: noise_up: 9.3
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: noise_down: 6.1
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: linemode: ADSL2+
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: attenuation_down: 22.0
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: rate_down: 23619
2020-03-01 23:25:00.269 Status: dzVents: Debug: getDSLInfo: attenuation_up: 12.0
2020-03-01 23:25:00.269 Status: dzVents: Info: getDSLInfo: ------ Finished Lecture HTTP
Version: 2020.2 (last stable)
Platform: Raspberry PI 3 B+
Plugin/Hardware: RFXCOM + GCE IPX800v4 & 2x X-Dimmer & 1x X8R + DOORBIRD D101S + LEGRAND EcoCompteur + Teleinfo + ONKYO TX-NR626
Neuvidor2307
Posts: 6
Joined: Wednesday 07 March 2018 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Extract value from http response  [Solved]

Post by Neuvidor2307 »

Finaly I did it this evening... :)
Thanks to Waaren for the support.

My code :

Code: Select all

local scriptVar = 'getDSLInfo'
local idxADSLdown = 31      -- (Integer) Device ID
local idxADSLup = 32      -- (Integer) Device ID
local idxADSLatt_down = 33      -- (Integer) Device ID
local idxADSLatt_up = 34      -- (Integer) Device ID
local idxADSLnoise_down = 35      -- (Integer) Device ID
local idxADSLnoise_up = 36      -- (Integer) Device ID



return
{
    on =
    {
        devices =
        {
            'xmlTrigger' , -- just for test
        },

        timer =
        {
            'every minute',
        },

        httpResponses = 
        {
            scriptVar,
        },
    },

    --logging = 
    --{
    --    level = domoticz.LOG_DEBUG,
    --    marker = scriptVar,
    --},

    execute = function(dz, item)
        if item.isTimer or item.isDevice then
            dz.openURL(
            {
                url = "http://192.168.1.1/api/1.0/?method=dsl.getInfo",
                method = 'GET',
                callback = scriptVar,
            })
        elseif item.ok then -- statusCode == 2xx
            -- convert xml to Table
            local rt =  dz.utils.fromXML(item.data)

            --dz.utils.dumpTable(rt)
            local dsl = rt.rsp.dsl._attr

            -- get one value
            --dz.log('line-status: ' .. dsl.line_status,dz.LOG_DEBUG)
            dz.log("#############")
            dz.log("Débit ADSL download => " .. dsl.rate_down .. " Kbps")
            dz.log("Débit ADSL upload => " .. dsl.rate_up .. " Kbps")
            dz.log("#############")
            
            dz.devices(idxADSLdown).updateCustomSensor(dsl.rate_down)
            dz.devices(idxADSLup).updateCustomSensor(dsl.rate_up)
            dz.devices(idxADSLatt_down).updateSoundLevel(dsl.attenuation_down)
            dz.devices(idxADSLatt_up).updateSoundLevel(dsl.attenuation_up)
            dz.devices(idxADSLnoise_down).updateSoundLevel(dsl.noise_down)
            dz.devices(idxADSLnoise_up).updateSoundLevel(dsl.noise_up)
            
    
            -- get all values
            --for key, value in pairs(dsl) do
            --    dz.log(key .. ': '.. value,dz.LOG_DEBUG)
            --end
        else
            dz.log('problem retrieving data from modem',dz.LOG_ERROR)
            dz.log(item.data,dz.LOG_ERROR) --
        end
    end
}
The result :
Visual.JPG
Visual.JPG (157.25 KiB) Viewed 4031 times
Version: 2020.2 (last stable)
Platform: Raspberry PI 3 B+
Plugin/Hardware: RFXCOM + GCE IPX800v4 & 2x X-Dimmer & 1x X8R + DOORBIRD D101S + LEGRAND EcoCompteur + Teleinfo + ONKYO TX-NR626
Piacco
Posts: 69
Joined: Friday 14 November 2014 9:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Extract value from http response

Post by Piacco »

I want to read the data off my ventilation unit, can someone help me with a dzvents script to read the dat from a webAPI.

The API request is :

Code: Select all

http://192.168.1.65/api.html?get=ithostatus
The API result is :

Code: Select all

{"requested-fanspeed_perc":-1,"balance_perc":99.7,"supply-fan_rpm":933,"supply-fan-actual_rpm":925,"exhaust-fan_rpm":936,"exhaust-fan-actual_rpm":933,"supply-temp_c":16.88,"exhaust-temp_c":21.16,"status":0,"room-temp_c":16.88,"outdoor-temp_c":21.16,"valve-position":0,"bypass-position":0,"summercounter":300,"summerday_kmin":1,"frost-timer":0,"boiler-timer":177,"frost-block":120,"current-position":0,"vkkswitch":0,"gheswitch":0,"airfilter-counter":1942,"global-fault-code":0,"actual-mode":24,"pir-fan-speed-level":-1,"highest-received-co2-value_ppm":726,"highest-received-rh-value_rh":58,"air-quality_perc":100,"remaining-override-timer_sec":0,"fallback-speed-timer_sec":0,"label-out-of-bound-error":0}
any ideas :)
willemd
Posts: 631
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Extract value from http response

Post by willemd »

Have a look here: https://www.domoticz.com/wiki/DzVents:_ ... d_handling

That will give you the API result as a response, which then needs to be processed as a json. The json processing in my experience is a lot of trial and error and not very well documented. You can use domoticz.utils.dumpTable to see the structure and take it from there. Examples with json processing can be found in the forum.
willemd
Posts: 631
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Extract value from http response

Post by willemd »

or, probably easier, you can set up a http poller to get the data.
Have a look here
https://www.domoticz.com/wiki/HTTP/HTTPS_poller
Piacco
Posts: 69
Joined: Friday 14 November 2014 9:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Extract value from http response

Post by Piacco »

I tried the HTTP poller, but no luck.

Error

Code: Select all

2023-02-18 23:53:45.983 Error: CLuaHandler: /home/pi/domoticz/scripts/lua_parsers/itho.lua:4: attempt to index a number value
The device is updating, but the value is still 0, it should be 99.8

lua parser script:

Code: Select all

local idx = 1748
local value = domoticz_applyJsonPath(request['content'], '.balance_perc')
domoticz_updateDevice(idx, '' , value)
Result WebAPI:

Code: Select all

{"requested-fanspeed_perc":-1,"balance_perc":99.8,"supply-fan_rpm":1757,"supply-fan-actual_rpm":1750,"exhaust-fan_rpm":1761,"exhaust-fan-actual_rpm":1772,"supply-temp_c":17.99,"exhaust-temp_c":22.36,"status":0,"room-temp_c":17.99,"outdoor-temp_c":22.36,"valve-position":0,"bypass-position":0,"summercounter":300,"summerday_kmin":1,"frost-timer":0,"boiler-timer":177,"frost-block":120,"current-position":0,"vkkswitch":0,"gheswitch":0,"airfilter-counter":1951,"global-fault-code":0,"actual-mode":24,"pir-fan-speed-level":-1,"highest-received-co2-value_ppm":1082,"highest-received-rh-value_rh":56,"air-quality_perc":100,"remaining-override-timer_sec":0,"fallback-speed-timer_sec":0,"label-out-of-bound-error":0}
willemd
Posts: 631
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Extract value from http response

Post by willemd »

I can't see anything obviously wrong. What is the type of device that you are updating? Build in some debug print statements?
Piacco
Posts: 69
Joined: Friday 14 November 2014 9:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Extract value from http response

Post by Piacco »

I'am still stuck, when i try the variable ithostatus.balance_perc i get no error, and see the requested value in the domoticz log, but when i try another value by example the highest-received-co2-value_ppm varable i get an error.

Code: Select all

2023-02-19 21:26:06.115 Error: dzVents: Error: (3.1.8) getIthoStatus: ...oticz/scripts/dzVents/generated_scripts/1_IthoStatus.lua:41: attempt to perform arithmetic on a nil value (field 'highest')

Code: Select all

local scriptVar = 'getIthoStatus'

return
{
    on =
    {
        devices =
        {'Test' , -- just for test
        },
        timer =
        {
            'every minute',
        },
        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if item.isTimer or item.isDevice then
            dz.openURL(
            {
                url = "http://192.168.1.65/api.html?get=ithostatus",
                method = 'GET',
                callback = scriptVar,
            })
       
         elseif (item.isHTTPResponse)  and item.ok then -- statusCode == 2xx
 
            local ithostatus =  dz.utils.fromJSON(item.data)
            dz.utils.dumpTable(ithostatus)
            test1 = ithostatus.balance_perc -- this value is working
            test2 = ithostatus.highest-received-co2-value_ppm -- on this value i get an error
            local test = ithostatus.balance_perc
            dz.log('Balans: ' .. test1,dz.LOG_DEBUG)
            dz.log('Actuele Co2 waarde: ' .. test2,dz.LOG_DEBUG)

        else
            dz.log('problem retrieving data from modem',dz.LOG_ERROR)
            dz.log(item.data,dz.LOG_ERROR) --
        end
    end
}
plugge

Re: Extract value from http response

Post by plugge »

You declared test1 and test2 as globals. Anything could happen then.

Change it in:

Code: Select all

            local test1 = ithostatus.balance_perc -- this value is working
            local test2 = ithostatus.highest-received-co2-value_ppm -- on this value i get an error
And try again.
(And check if the dumped table contains your key:value pair.)
Piacco
Posts: 69
Joined: Friday 14 November 2014 9:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Extract value from http response

Post by Piacco »

The issue is solved with help of topic https://domoticz.com/forum/viewtopic.php?t=26021.

I have to declare the local co2 on this way, I'don't know exactly why, but it's working now :D

Code: Select all

local co2 = (ithostatus["highest-received-co2-value_ppm"])
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Extract value from http response

Post by boum »

Because `-` is not authorized in identifier names in lua (so neither in dzVents). The error comes from the parser reads the line as ithostatus.highest - received - ..., not as one table key. So you must use the other syntax for accessing table elements ithostatus["keyname"].
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest