Domoticz HTTP/HTTPS poller and JSON

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:

HTTP/HTTPS poller to handle JSON in Domoticz
HTTP/HTTPS poller to handle JSON in Domoticz

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:

HTTP/HTTPS poller added in Domoticz
HTTP/HTTPS poller added in Domoticz

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:

A temperature sensor using Domoticz's HTTP/HTTPS poller
A temperature sensor using Domoticz’s HTTP/HTTPS poller

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.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.