Need help searching for specific info in JSON output

Moderator: leecollings

Post Reply
wlevels
Posts: 16
Joined: Friday 16 September 2016 13:54
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: The Netherlands
Contact:

Need help searching for specific info in JSON output

Post by wlevels »

Hi all,

I am trying to monitor my Master Node through Domoticz. There is a website where you can check the status of each Master Node, however the JSON API is pretty basic so I have to find my Master Node in the complete list to monitor the status.

The URL is http://api-rupx.mn.zone/masternodes

A snippet of example output is added to the bottom of this post.

I already managed to look up the complete list, but now I have to filter out only my own Master Node but I'm missing scripting knowledge/experience how to do that.

I want to be able to find my node based on IP or Address and then retrieve the Status

I don't know if something like a for loop is needed to look through all nodes, or if I can somehow directly search on IP or Address and I can just output the Status.

Thanks for your help!

Wesley

Code: Select all

{
  "nodes": [
    {
      "ip": "1.1.1.1",
      "port": 9020,
      "address": "123456",
      "protocol": "70002",
      "wallet": "4.0.0",
      "status": "ENABLED",
      "active_time": 218054,
      "last_seen": 1523211508,
      "last_paid": 1523206405,
      "paid_block": 0,
      "listen": 1,
      "voting": 0,
      "blocks": 80022
    },
    {
      "ip": "2.2.2.2",
      "port": 9020,
      "address": "234567",
      "protocol": "70002",
      "wallet": "4.0.0",
      "status": "ENABLED",
      "active_time": 17512,
      "last_seen": 1523211413,
      "last_paid": 0,
      "paid_block": 0,
      "listen": 1,
      "voting": 0,
      "blocks": 60408
    },
    {
      "ip": "3.3.3.3",
      "port": 9020,
      "address": "345678",
      "protocol": "70002",
      "wallet": "4.0.0",
      "status": "ENABLED",
      "active_time": 415347,
      "last_seen": 1523211492,
      "last_paid": 1523207099,
      "paid_block": 0,
      "listen": 1,
      "voting": 0,
      "blocks": 73137
    }
  ],
  "statuses": [
    {
      "status": "ENABLED",
      "count": 752
    }
  ],
  "total_nodes": 752,
  "blocks": 0,
  "error": "",
  "tech": "pivx",
  "api_ver": "1.5.5",
  "time": 1523211739
}
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Need help searching for specific info in JSON output

Post by jvdz »

Wesley,
I've dumped your JSON in file testjson4.txt and wrote this LUA code to show all IP - Status records to get you started.

Code: Select all

json = (loadfile "/usr/local/share/lua/5.2/JSON.lua") ()
-- example 1
file = io.open("testjson4.txt", "r")
datainp=file:read("*a")
file:close()
--~ print(datainp)
input = json:decode(datainp)
result_table = input['nodes']
tc = #result_table
for i = 1, tc do
	print(result_table[i].ip)
	print(result_table[i].status)
end
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need help searching for specific info in JSON output

Post by waaren »

in dzVents 2.4.3

Code: Select all

--[[ 
   masternodes test (dzVents >= 2.4.0
   ]] --

return {
	on = { 
		timer = { 'every minute' },             -- timer or device to trigger the script. Can be both or one of them           
        httpResponses = { 'masternodesResponse' }	    -- When json call is answered it will trigger this
    },
	
    execute = function(dz, trigger)
		if trigger.isHTTPResponse then
			local mymasternodeIP = "207.246.85.225"	
			local statusTextDevice = dz.devices("masternodeStatus")
			
			if (trigger.ok) then -- statusCode == 2xx
                		local result_table = trigger.json.nodes
				local tc = #result_table
				
				for i = 1, tc do
					if result_table[i].ip == mymasternodeIP then
						if statusTextDevice.text ~= result_table[i].status then
							print("Status of: " .. result_table[i].ip .. " changed to: " ..  result_table[i].status )
							statusTextDevice.updateText(result_table[i].status)
						else
							print("Status of: " .. result_table[i].ip .. " still: " ..  result_table[i].status )
						end
					end
				end
            		end
		else
			local masternodesURL = "http://api-rupx.mn.zone/masternodes"
            		dz.openURL({
                	url = masternodesURL,
                	method = "GET",
                	callback = "masternodesResponse"    })
        	end
    end
}
in PHP:

Code: Select all

<?php
$masternodes = file_get_contents('http://api-rupx.mn.zone/masternodes');
$masternodes = json_decode($masternodes,true);
$masternodes = $masternodes['nodes'];
$mymasternodesIP = "45.77.56.191"; // your masternode IP number
$Domoticz_URL = 'http://nnn.nnn.nnn.nnn:nnnn/json.htm?';
$Domoticzmasternodes_IDX = nnn;   // text device in domoticz

foreach($masternodes as $ip => $value)
{
	if ( $value['ip'] == $mymasternodesIP ) {
		$domoticzrespons=$value['status'];
		$respons="Status of " . $value['ip']  . " ===>>> " . $value['status'] .  "\r\n"	;
	}
}
		
echo $respons ;		
$Domoticz_putURL = $Domoticz_URL.'type=command&param=udevice&idx='.$Domoticzmasternodes_IDX.'&nvalue=0&svalue=' . $domoticzrespons ;
file_get_contents($Domoticz_putURL);
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
wlevels
Posts: 16
Joined: Friday 16 September 2016 13:54
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: The Netherlands
Contact:

Re: Need help searching for specific info in JSON output

Post by wlevels »

Thanks @jvdz @waaren for the input! I'll have a go at it :D
wlevels
Posts: 16
Joined: Friday 16 September 2016 13:54
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: The Netherlands
Contact:

Re: Need help searching for specific info in JSON output

Post by wlevels »

I got it mostly running as I want, thanks for your help!

I can ofcourse send anybody the script if they also want to monitor their Master Node but probably there is a lot of finetuning still possible in my newbie script :)

I am monitoring two master nodes. The second master node list is over 1000 lines, about 133X lines. It throws me an error:

2018-04-11 20:26:10.989 Error: EventSystem: in time-shekel-online-1.0: ./scripts/lua/JSON.lua:1009: ./scripts/lua/JSON.lua:834: Lua script execution exceeds maximum number of lines

How can I circumvent this? Something like splitting the result file?
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Need help searching for specific info in JSON output

Post by jvdz »

This error is coming from the domoticz LUA event module.
This shouldn't be related to the number of lines in the data, but rather the number of lines in the script and all its includes (at least that's the way it sounds).
Is your script really that big or is is really related to the size of the JSON data?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
wlevels
Posts: 16
Joined: Friday 16 September 2016 13:54
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: The Netherlands
Contact:

Re: Need help searching for specific info in JSON output

Post by wlevels »

Hi Jos,

Running the exact same script (only replaced all the variables) for two different master nodes.

The master node script which is working retrieves 790 entries in a json output file which it searches succesfully for my Master Node.
The other master node script retrieves 1340 entries in a json output file and gives me the error when trying to open that output file

I think it's related to the numer of lines in the output file reaching above 1000
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest