Inserting raw lua parsers

From Domoticz
Jump to: navigation, search

Let's describe a new way to insert data using lua scripts

Example: You have a nice hardware like the Yocto-Meteo [1] from Yoctopuce [2] and you want to insert the sensors data into Domoticz

The idea is to use Yoctopuce's VirtualHub (a process exposing the sensors) to push the data to an external HTTP server, that's call the Outgoing callbacks.

The overall architecture is quiet simple: Yoctopuce's VirtualHub is pushing the data with HTTP to a new Domoticz's entry point, once received the data is parsed by a lua script and the parsed data can to inserted into any devices

Step 1 : Configuring VirtualHub

Connect to port 4444 and configure the Outgoing callbacks as followed :

Inserting raw lua parsers.png

  • HTTP Method : GET
  • Callback : http://<domoticz ip>:<domoticz port>/json.htm?type=command&param='''udevices'''&script='''yoctopuce_meteo.lua'''&

Some explanations :

  • Notice the ending '&', it is mandatory because VirtualHub will append its data to the url
  • "udevices" is the new command
  • yoctopuce_meteo.lua is the name of the lua script to be used to parse incoming data
  • The HTTP method is 'GET' and the HTTP query made to Domoticz will look like : http://<domoticz ip>:<domoticz port>/json.htm?type=command&param=udevices&script=yoctopuce_meteo.lua&timestamp=1446371516&network=&meteo%23dataLogger=OFF&meteo%23temperature=5.72&meteo%23pressure=889&meteo%23humidity=81.1

Step 2 : Create a dummy device

Create a dummy device and note the device index, it will be useful just below

Step 3 : Parse the data

Domoticz is designed to execute the lua script defined in the url (defined in Step 1) and parse the HTTP data (query + 'POST' data if defined). The script has to be added to <domoticz path>/scripts/lua_parsers Here is the script use to parse the VirtualHub's data:


local temperature = uri['meteo#temperature'];
local humidity = uri['meteo#humidity']; local pressure = uri['meteo#pressure'];

domoticz_updateDevice(15,0,temperature .. ";" .. humidity .. ";" .. "0" .. ";" .. pressure .. ";" .. "0");


Some explanations:

  • 2 internals global lua variables are defined :
    • request["content"] : Contains the query data (when using 'POST' HTTP method)
    • uri : It's an associative array with all HTTP query parameters. From the above query, the array will contain several entries like uri['timestamp'], uri['meteo#temperature'], uri['meteo#humidity'], uri['meteo#pressure'] etc...
  • a lua function called domoticz_updateDevice can be used to assign a (or several) values to a device: The parameters are <device id>, <nvalue>, <svalue>. See [3] for more details