Page 1 of 1

Reading data from device

Posted: Friday 03 April 2020 12:05
by PatrickGoosen
Hi everybody,

I'm having problems reading the data from my weatherstation, I can see the device and with a json command from the browser it gives my all the data I need but getting it from lua is not working.

Can someone please help me?

Kind regards,
Patrick

The error I'm getting is: EventSystem: in Tuindata: [string "local Tuin_Temperatuur_idx = 251 ..."]:14: attempt to index a nil value (field 'Tuindata')

The device is: 236 RFXLink 2301 0 Tuindata Temp + Humidity WTGR800 10.3 C, 68 %

The json command is: http://192.168.178.5:8080/json.htm?type=devices&rid=236
and returns:
Spoiler: show
{
"ActTime" : 1585907850,
"AstrTwilightEnd" : "22:22",
"AstrTwilightStart" : "05:05",
"CivTwilightEnd" : "20:53",
"CivTwilightStart" : "06:33",
"DayLength" : "13:10",
"NautTwilightEnd" : "21:36",
"NautTwilightStart" : "05:51",
"ServerTime" : "2020-04-03 11:57:30",
"SunAtSouth" : "13:43",
"Sunrise" : "07:08",
"Sunset" : "20:18",
"app_version" : "2020.1 (build 11836)",
"result" :
[
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 100,
"CustomImage" : 0,
"Data" : "10.3 C, 69 %",
"Description" : "",
"DewPoint" : "4.86",
"Favorite" : 0,
"HardwareID" : 16,
"HardwareName" : "RFXLink",
"HardwareType" : "RFLink Gateway USB",
"HardwareTypeVal" : 46,
"HaveTimeout" : false,
"Humidity" : 69,
"HumidityStatus" : "Wet",
"ID" : "2301",
"LastUpdate" : "2020-04-03 11:57:22",
"Name" : "Tuindata",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" :
[
0
],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "WTGR800",
"Temp" : 10.300000000000001,
"Timers" : "false",
"Type" : "Temp + Humidity",
"TypeImg" : "temperature",
"Unit" : 0,
"Used" : 0,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "236",
"trend" : 2
}
],
"status" : "OK",
"title" : "Devices"
}
This is the script:
Spoiler: show
local Tuin_Temperatuur_idx = 251
local Tuin_Vochtigheid_idx = 252

commandArray = {}
time = os.date("*t")
if((time.min % 1)==0)then

function round(num, idp) -- Credits go to Martin Rourke, I used his function.
local mult = 10^(idp or 1)
return math.floor(num * mult + 0.5) / mult
end

-- Split data
sWeatherTemp, sWeatherHumidity = otherdevices_svalues['Tuindata']:match("([^;]+);([^;]+)")

sWeatherTemp = tonumber(sWeatherTemp)
commandArray[1]={['UpdateDevice'] = Tuin_Temperatuur_idx .. '|0|' .. tostring(sWeatherTemp)}

sWeatherHumidity = tonumber(sWeatherHumidity)
commandArray[2]={['UpdateDevice'] = Tuin_Vochtigheid_idx .. '|' .. (sWeatherHumidity) .. '|0'}

end

return commandArray

Re: Reading data from device

Posted: Monday 06 April 2020 14:09
by bewo
Hi Patrick,

your weatherstation sends the data to an "real" domoticz device over RFXcom and your aim is to split the data and write it in two dummy devices... Is this right?

If yes, can you please post the output of:

Code: Select all

commandArray = {}

print(otherdevices_svalues['Tuindata'])

return commandArray
If no, please clarify... :)

Re: Reading data from device

Posted: Tuesday 14 April 2020 14:38
by PatrickGoosen
This is what I got.

lua: test.lua:3: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
test.lua:3: in main chunk
[C]: ?

And it's rfxlink by the way.

Device: 236 RFXLink 2301 0 Tuindata Temp + Humidity WTGR800 11.8 C, 50 %

Re: Reading data from device

Posted: Tuesday 14 April 2020 16:37
by bewo
So that's the same error as you wrote in your first post. That's spooky....

Can you post an screenshot form the device in state-table? (Under events, first index tab)

Re: Reading data from device

Posted: Thursday 16 April 2020 11:49
by PatrickGoosen
This is the data:
Image

And the message above comes with every variable I try.

Re: Reading data from device

Posted: Thursday 16 April 2020 13:40
by bewo
Sorry for the stupid question: Are you trying this in the domoticz event editor?

I've tried in all ways. The runtime error you posted above isn't shown there. If i try over CLI i'll get the error. Imho Domoticz doesn't offer the variables to the native lua instance...

Re: Reading data from device

Posted: Thursday 16 April 2020 13:57
by PatrickGoosen
I created test.lua in /home/pi/domoticz/scripts/lua and ran it with sudo lua test.lua.
Can we move to whatsapp or something, that goes quicker.

Re: Reading data from device

Posted: Thursday 16 April 2020 13:59
by bewo
Ok - there's the problem. Give me just an second. I'll post an script for you.

Re: Reading data from device

Posted: Thursday 16 April 2020 14:36
by bewo
Sorry for the delay - had to do some other work. :)

Here's an script for you. In the domoticz event editor just make an new lua script with device as type of trigger, insert my lines and i guess it works. :D

Code: Select all

-- Script for seperating temperature and humidity in several dummy-devices

-- Devices:
WheaterStation = 'Tuindata'
Dummy_Temperature = 'Tuin Temperatuur'
Dummy_Humidity = 'Tuin Vochtigheid'

-- Nothing to change after this line... :-)
commandArray = {}

if devicechanged[WheaterStation] then

    -- Split data
    Temperature, Humidity = otherdevices_svalues[WheaterStation]:match("([^;]+);([^;]+)")
    
    -- Update Temperature-Device
    if Temperature ~= otherdevices_svalues[Dummy_Temperature] then
        commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[Dummy_Temperature]..'|0|'..Temperature}
    end
    
    -- Update Humidity-Device
    if Humidity ~= otherdevices[Dummy_Humidity] then
        commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[Dummy_Humidity]..'|'..tonumber(Humidity)..'|0'}
    end


end

return commandArray 


Re: Reading data from device

Posted: Thursday 16 April 2020 14:51
by PatrickGoosen
I Will try thanks so far.

Re: Reading data from device

Posted: Thursday 16 April 2020 15:26
by PatrickGoosen
IT WORKS! Thank you very much.

Re: Reading data from device

Posted: Thursday 16 April 2020 15:31
by bewo
No problem, you're welcome. :D

Re: Reading data from device

Posted: Friday 17 April 2020 12:06
by PatrickGoosen
Cheering too soon...
It updated but not anymore.

252 2020-04-17 03:23:47 Tuin - Vochtigheid 57 57/0
251 2020-04-17 03:52:17 Tuin - Temperatuur 9.30 0/9.30
236 2020-04-17 12:05:19 Tuindata 13.2;54;1 0/13.2;54;1

Any tips?

Re: Reading data from device

Posted: Friday 17 April 2020 12:39
by bewo
Any errors or outputs in the log?

Re: Reading data from device

Posted: Friday 17 April 2020 12:57
by PatrickGoosen
Sorry my fault. It still works. Typo.

Re: Reading data from device

Posted: Tuesday 28 April 2020 16:58
by PatrickGoosen
It stopped again, no idea why? No logentries nothing. Any ideas?