Json to domoticz

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Json to domoticz

Post 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.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Json to domoticz

Post 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?
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Json to domoticz

Post by ben53252642 »

This might be what you are looking for?

https://github.com/SevenW/Plugwise-2-py ... r/domoticz
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
zak45
Posts: 954
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Json to domoticz

Post 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...
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: Json to domoticz

Post 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
zak45
Posts: 954
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Json to domoticz

Post 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.
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: Json to domoticz

Post by edwin1234 »

Thank you for your help,
I will try it an let you know.
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: Json to domoticz

Post by edwin1234 »

Its not working
Error atempt to get a nil value
What is wrong?
zak45
Posts: 954
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Json to domoticz

Post by zak45 »

Need more information.. put some 'print' for debugging.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest