Page 1 of 1

Json to domoticz

Posted: Sunday 22 January 2017 13:19
by edwin1234
I m on windows and want the results from a json request from a server in to domoticz.
This is the request :

Code: Select all

http://ip:port/jsonapi.asp?action=getdevice&id=$31
And get this result :

Code: Select all

{"id": "$31", "name": "CV-ketel", "room": "Zolder", "floor": "", "dimmable": onwaar, "status": 2, "value": 8, "since": "22-1-2017 13:10:58", "type": "Plugwise: CV-ketel", "status_support": onwaar, "misc": "65536"}
How do i get this output in a virtual device in domoticz?
For example "value": 8

I have no scripting experience
So if someone could help me out i would be very grateful.

Re: Json to domoticz

Posted: Sunday 22 January 2017 14:30
by ben53252642
This can be done a few ways that I know of:

1) Lua time script in Domoticz, runs every minute and collects the data

2) PHP script, can be set to run however frequently you like (say every second) and update the data if the state in Domoticz doesn't match the data it fetches.

3) With a Python script (I don't use this method for JSON), I prefer PHP.

Each way would take approx 1hr of coding / testing for me to implement, it's not a simple request.

Which method would work best?

Re: Json to domoticz

Posted: Sunday 22 January 2017 14:59
by ben53252642
This might be what you are looking for?

https://github.com/SevenW/Plugwise-2-py ... r/domoticz

Re: Json to domoticz

Posted: Sunday 22 January 2017 15:17
by zak45
you can also use the HTTP/HTTPS poller

see topics :
viewtopic.php?t=13406
http://www.domoticz.com/forum/viewtopic.php?t=14440

this is small code use to retreive data from Json output!
Spoiler: show
-- Retrieve the request content
s = request['content'];
--
-- charge lib JSON
json = (loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\json.lua")()
-- on decode le contenu recuperé par l'URL
local jsonLocation = json:decode(s)
--
-- concaténation des entrées de la table et formatage
local PluieText = ''
if jsonLocation.niveauPluieText ~= nil then
for Index, Value in pairs( jsonLocation.niveauPluieText ) do
if Index == 1 then
PluieText = PluieText..jsonLocation.niveauPluieText[Index]
elseif Index > 1 then
PluieText = PluieText..' puis ' ..jsonLocation.niveauPluieText[Index]
end
end
end
--
--
-- niveau 1 vert, pas de pluie
local niveau = 1
-- on cherche a savoir si il va pleuvoir
local valeur_pluie = string.find(PluieText,": Pr")
-- si trouvé alors risque de pluie
if valeur_pluie ~=nil then niveau = 4 end
-- maj du device alert
domoticz_updateDevice(42,niveau,PluieText)
But will need in anyway small script to run it...

Re: Json to domoticz

Posted: Monday 23 January 2017 13:04
by edwin1234
Wirh the http poller i get an output in the domoticz log, but the virtual device doesnt get an update?
This is in the log:
CLuaHandler (updateDevice from LUA) : idx=226 nvalue= svalue= "value": 9 invalue=0 signallevel=12 batterylevel=255
2017-01-23 13:02:36.766 CLuaHandler (updateDevice from LUA) : idx=227 nvalue=
{"id": "$31" svalue= "value": 9; "since": "23-1-2017 13:01:00" invalue=0 signallevel=12 batterylevel=255

I want the value :9 virtual device is idx 226

Re: Json to domoticz

Posted: Monday 23 January 2017 21:20
by zak45
So after you have created your HTTP Poller, this is the script part:

Code: Select all

-- Retrieve the request content
s = request['content'];
-- 
-- load JSON lib need to be adapted in case not x64
json = (loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\json.lua")()
-- call Json decode function
local jsonData = json:decode(s)
--
ValueIWant= jsonData.value
--
domoticz_updateDevice(226,0,ValueIWant)
this script need to be put under :
C:\Program Files (x86)\Domoticz\scripts\lua_parsers

give it name you want, this name need to be the same as in your HTTP Poller device setup.

Hope this help.

Re: Json to domoticz

Posted: Tuesday 24 January 2017 6:07
by edwin1234
Thank you for your help,
I will try it an let you know.

Re: Json to domoticz

Posted: Saturday 28 January 2017 11:13
by edwin1234
Its not working
Error atempt to get a nil value
What is wrong?

Re: Json to domoticz

Posted: Sunday 29 January 2017 11:29
by zak45
Need more information.. put some 'print' for debugging.