Adding a sensor of a remote host to Domoticz is easy, just not very well documented.
I have a DS1820 one-wire sensor plugged on a Linux machine that polls the temperature every minute then stores it in a CSV. Since I recently installed Domoticz on a Raspberry PI, I wanted to show the values there as well. I’ll show what you need to do this.
Open the Setup menu and choose the Hardware option. Enter a name you like and select the HTTP/HTTPS poller type, you should see the following:
Data Timeout: leave it Disabled
ContentType: the type of received data, application/json
URL: the full URL including scheme, host name and path where you will have the JSON file with the latest data.
Command: this is a filename (with extension) for a script that will handle the downloaded JSON, which Domoticz will search at …/domoticz/scripts/lua_parsers/<command>
Tehre’s a basic example called example_json.lua, for a this setup it will be fine.
Refresh: refresh interval in seconds.
After adding you should see it immediately in the list:
Click on Create Virtual Sensors button to add a sensor, give it a custom name, select the type of data you want to log and select OK. It should appear in your devices list:
Note the Idx of the sensor (not the ID nor Unit, the first number) which is 15 in my case. You will need to put this in the JSON file on remoteserver in a field named id – yes, this is inconsistent.
The JSON file you need to provide on remoteserver can be as simple as this:
{ "id": 15, "temperature": 12.3 }
Tip: you can output this with any script, you can consider it a string, just replace the value with the latest measurement. You also need to update it periodically.
As soon as you added the poller, Domoticz starts to poll the URL you supplied and passes the result to the script you supplied.
If in the next few minutes you start to see the data in Domoticz, congratulations, you have it working. If not, check the Troubleshooting section below.
Note 1: Domoticz executes the script one time for every HTTP/HTTPS poller “hardware” and you can have multiple sensors configured for one poller. In this case you need to supply multiple id/idx values and multiple values for measurements and need a custom script updating each sensor.
Note 2: In the example script the id is read from the JSON and blindly supplied to Domoticz which will update the sensor having that as idx. This may overwrite the data of any other sensor if configured improperly.
Unfortunately Domoticz is only supplying the hardware idx as hwdId variable but not the idx of the sensor(s) on that poller, so if you want to prevent it you need to hardcode the idx of the sensor(s) in your script, e.g. you can create remote_1_json.lua with the content:
local idx = 15 local temperature = domoticz_applyJsonPath(request['content'], '.temperature') domoticz_updateDevice(idx, '' , temperature)
Then you can entirely skip the id field in JSON:
{ "temperature": 12.3 }
Just make sure you update the script’s name on the web interface.
Note 3: you might need to add the following header on the remote host to your response:
Content-Type: application/json
Troubleshooting
Error: CLuaHandler (updateDevice from LUA) : Incorrect parameters type
One of the field received in JSON is invalid. The id should be an integer, temperature should be a float.
Error: CLuaHandler: cannot open /home/.../domoticz/scripts/lua_parsers/example_json: No such file or directory
You need to provide a full file name with extension in the Command field of the Setup > Hardware page. Also, make sure the script exists.
CLuaHandler (updateDevice from LUA) : idx=15 nvalue= svalue=12.3 invalue=0 signallevel=12 batterylevel=255
This is normal, the script parsed the JSON and issued an update for the sensor. If you can’t see the update on the interface, double check the idx or id parameter you have in the JSON file.
View Comments (4)
Hi
Can you give me some help please ?
I need to retrieve a data in json response and I could not figure how even after I read you post.
Hello
I havé an invalid parameter type with this json link
https://pi0d2ded-macbenoit-guest.t1pal.com/api/v1/entries.json?count=1
My code is
local idx = 796
local sgv = domoticz_applyJsonPath(request['content'], '.svg')
domoticz_updateDevice(idx, '' , sgv)
Please help me i am trying to understand sinces days
Of course i Did not copy properlly this is thé code but it wont work stil invalid parameter type
local idx = 796
local sgv = domoticz_applyJsonPath(request['content'], '.sgv')
domoticz_updateDevice(idx, '' , sgv)
Hey Benoit,
I believe your code should be like this as your JSON reply is in an array:
local sgv = domoticz_applyJsonPath(request['content'], '.[0].sgv')
domoticz_updateDevice(idx, '', sgv)
Hope this helps,
Gabor