HTTP/HTTPS poller

From Domoticz
(Redirected from Lua parsers)
Jump to navigation Jump to search

The HTTP/HTTPS poller can be used to get data from a remote server (serving a webserver over http/https) and put the data into a Domoticz device.

It make uses from a lua script to parse the data and store it in a Devices. To have the values of the remote device being stored in Domoticz you have to create a Virtual Device first.

Parser script examples can be found in domoticz/scripts/lua_parsers for CSV, JSON and XML data.

To create a HTTP/HTTPS poller goto Menu Setup - Hardware and select from the Type pull down list: HTTP/HTTPS poller.



Fields to enter:
Method GET or POST (for explanation see https://www.w3schools.com/tags/ref_httpmethods.asp)
Content type examples: application/json application/xml, text
Headers optional
URL The URL where the remote data is coming from. Can also be a measuring device on your local network.
Command The lua parser script. The script has to be placed in domoticz/scripts/lua_parsers. In the script the commands for updating the devices.
Refresh refresh (wait) time in seconds. Do not use 0 (zero) seconds as it will crash Domoticz. An advised refresh time is > 60 seconds.
Username optional
Password optional

Create Virtual Device

From the Hardware list click on the Create Virtual Sensor button and create a device. See also page create a Virtual Device.


Create Lua Parser script

Make a copy of one of the examples in domoticz/scripts/lua_parsers and modify it to your needs. See also the domoticz github repository.

Be aware that an incorrect idx of the virtual device could overwrite another sensor!


Special Lua parser functions for Domoticz:

domoticz_updateDevice(deviceId (integer), svalue (string), {nvalue (string) | nvalue (integer)}, [rssi(integer)], [battery(integer)])

Be aware that device svalue and nvalue parameters should be supplied according Domoticz API/Json calls definitions.

domoticz_applyJsonPath(request, path)

domoticz_applyXPath(request, path)


Note: 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.

Troubleshooting

Error: CLuaHandler (updateDevice from LUA) : Incorrect parameters type 

One of the field received is invalid. The id should be an integer, temperature should be a float etc.

To check the value add a print statement in the parser and it will be listed in Domoticz Log file example: print('value read from poller: ' .. PValue)

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.


Example script

Example http poller json parser script, more examples for other datatypes to be found in the domoticz github repository.

-- Example of JSON parser handling data with the following structure
--{
--  "id": 13,
--  "name": "outside",
--  "temperature": 12.50,
--  "tags": ["France", "winter"]
--}

-- A test with curl would be : curl -X POST -d "@test.json" 'http://192.168.1.17:8080/json.htm?type=command&param=udevices&script=example_json.lua'

-- Retrieve the request content
s = request['content'];

-- Update some devices (index are here for this example)
local id = domoticz_applyJsonPath(s,'.id')
local s = domoticz_applyJsonPath(s,'.temperature')
domoticz_updateDevice(id,'',s)