Extract value from json response

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

Moderator: leecollings

Post Reply
Supertramp4
Posts: 6
Joined: Saturday 26 March 2022 10:19
Target OS: Windows
Domoticz version:
Contact:

Extract value from json response

Post by Supertramp4 »

Hi, I am a newbie to domoticz, and have been experimenting with lua/dzvents scripting, and need a little help
My script calls dz.openurl with a callback

Code: Select all

/json.htm?type=devices&rid=2, which successfully returns values, 
ie 
"Data" : "309 Watt",
"LastUpdate" : "2022-03-25 19:05:43",
My question is after the

Code: Select all

if item.isHTTPResponse and item.isJSON then
  local results=item.json.result
  for i, node in pairs(results) do
    [b]local power = node.Data -- just holding the numeric part without units[/b]
    local lastUpdate = Time(node.LastUpdate)
  ...
  end
end
how do I extract the numeric value from the "node.Data" field so that power will contain a value of 309, and does not have the units of "watt" so that I can use this to update another custom sensor
Many Thanks
mgugu
Posts: 218
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: Extract value from json response

Post by mgugu »

You have to split the string with space separator. Unfortunately there is no built in split function un LUA. You have to make it yourself.
Check at https://www.tutorialspoint.com/how-to-s ... rogramming
Doler
Posts: 147
Joined: Friday 31 July 2015 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Sint-Oedenrode, Netherlands
Contact:

Re: Extract value from json response

Post by Doler »

Here you go

Code: Select all

		
-- Split a string separated by delimiter and return result in array
stringSplit = function(s, delimiter)   
	local result = {}
	for strmatch in (s..delimiter):gmatch('(.-)'..delimiter) do
		table.insert(result, strmatch)
	end
	return result
end
Mark: Domoticz Beta on Raspberry Pi 4 running Debian Bookworm - Z-Stick 7 - RFXCom - P1 - MySensors - SolarEdge - Dahua - Philips Hue - Docker - Zigbee2mqtt (plugin) - Zwave-js-ui - dzVents - Nodered
User avatar
waltervl
Posts: 5855
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Extract value from json response

Post by waltervl »

It is fine that you experiment with JSON and OpenURL but normally this is not necessary with dzvents.
For example the lastupdate time can be read with statement
lastupdateTime = domoticz.devices(2).lastUpdate
or
lastupdateTime = device.lastUpdate
when the device 2 is the triggered device
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5855
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Extract value from json response

Post by waltervl »

I made an example script for getting the trend value out of the domoticz json respond.
Most important line:

Code: Select all

local DevTrend = item.json.result[1].trend 
no need for splitting etc.

viewtopic.php?p=288478#p288478
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Supertramp4
Posts: 6
Joined: Saturday 26 March 2022 10:19
Target OS: Windows
Domoticz version:
Contact:

Re: Extract value from json response

Post by Supertramp4 »

Hi all, thanks for your input.
@doler, the code worked, but after @waltervl input, I played a bit more, and discovered that was the way to go, and now have something working.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest