Reading data by lua from (remote) virtual devices

Moderator: leecollings

Post Reply
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Reading data by lua from (remote) virtual devices

Post by Toulon7559 »

Application of data of virtual devices requires extraction of such data.
Not always easy & consistent as discussed here.
More complicated when the virtual device is resident in another/remote Domoticz-instance:
Simple solution is duplication of the device in the 'local' Domoticz-instance, but is an extra step & dependency in the chain, to be avoided.
Testing at CLI should give same results as during normal runtime, unlike constructions using 'otherdevices_svalues'.

The following setup is a derivative from the read-out by lua-script of DarkSky-info, giving combined, practical solution for the 3 aspects mentioned above.

The setup described in this message has been triggered by the need to get hold of data from an Ecowitt GW1000 which collects data from various sensors.
The setup consists of 2 steps.
The 1st step is reading & decoding of the json-file coming out of the GW1000:
that decoding results in a number of virtual devices according to the API/JSON for Domoticz.
Not further described.
The 2nd step is individually reading those virtual devices and extracting data.
The script below in the spoiler is a feasability demonstration of that 2nd step as a lua-script.
Spoiler: show
------------------------------------------------------------------------------
-- Version GW1000_00_RPI3A2, revision 221114
-- This script tests an alternative approach to read data from virtual devices
-- Virtual device for GW1000 = type 'THB' as defined under API/JSON_Domoticz
-- Virtual device for WH31A = type 'T&H' as defined under API/JSON_Domoticz
-- Virtual device for WH40 = type 'rain' as defined under API/JSON_Domoticz
-- Virtual device for WH51 = type 'humidity' as defined under API/JSON_Domoticz
-- Realisation = reading of the json-contents of a 'remote' virtual device and then decoding of the json-file.
------------------------------------------------------------------------------
-- Collected & Compiled from various scripts by Toulon7559 (c)2020~2022
------------------------------------------------------------------------------
-- Line 012 = Start of commandArray
commandArray = {}
print ('Start of GW1000test_script 00')

-- Line 016 = Local Functions
-- Line 017 = Read url and extract contents
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end

-- Line 029 = Read-out value from remote Domoticz device for GW1000_THB
-- Line 030 = Querying remote virtual device
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
result=os.capture('curl -s "http://192.168.0.166:8080/json.htm?type=devices&rid=103"')
-- print(result)
-- Line 034 = Reading & decoding values from json
local jsonValue = json:decode(result)
val_GW1000temp =jsonValue.result[1].Temp
print ('GW1000_Temp = '.. val_GW1000temp)
val_GW1000hum =jsonValue.result[1].Humidity
print ('GW1000_Humidity = '.. val_GW1000hum)
val_GW1000baro =jsonValue.result[1].Barometer
print ('GW1000_Baro_Rel = '.. val_GW1000baro)

-- Line 043 = Read-out value from remote Domoticz device for GW1000_WH31A
-- Line 044 = Querying remote virtual device
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
result=os.capture('curl -s "http://192.168.0.166:8080/json.htm?type=devices&rid=12"')
-- print(result)
-- Line 048 = Reading & decoding values from json
local jsonValue = json:decode(result)
val_WH31Atemp =jsonValue.result[1].Temp
print ('WH31A_Temp = '.. val_WH31Atemp)
val_WH31Ahum =jsonValue.result[1].Humidity
print ('WH31A_Humidity = '.. val_WH31Ahum)

-- Line 055 = Read-out value from remote Domoticz device for GW1000_WH40
-- Line 056 = Querying remote virtual device
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
result=os.capture('curl -s "http://192.168.0.166:8080/json.htm?type=devices&rid=104"')
-- print(result)
-- Line 060 = Reading & decoding values from json
local jsonValue = json:decode(result)
val_RainToday =jsonValue.result[1].Rain
print ('WH40_RainToday = '.. val_RainToday)
val_RainRate =jsonValue.result[1].RainRate
print ('WH40_RainRate = '.. val_RainRate)

-- Line 067 = Read-out value from remote Domoticz device for GW1000_WH51
-- Line 068 = Querying remote virtual device
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
result=os.capture('curl -s "http://192.168.0.166:8080/json.htm?type=devices&rid=105"')
-- print(result)
-- Line 072 = Reading & decoding values from json
local jsonValue = json:decode(result)
val_soilmoist =jsonValue.result[1].Humidity
print ('WH51_SoilMoist = '.. val_soilmoist)

print ('End of GW1000test_script 00')

return commandArray
Obviously, for your applications,
- IPs of the weblinks have to be tuned [by manipulation of the IP probably also a local virtual device can be read]
- RIDs need to be aligned for the configuration
- on purpose the script-block per sensor-type is identical in layout, but for a combination of devices some redundant scriptlines might be removed/disabled
- the use of the output-data is your choice .....
Note
In practise the 1st step of the setup is very similar to the 2nd step, but with realisation through a Python-script.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest