Lua, Read data from device

Moderator: leecollings

thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Lua, Read data from device

Post by thaui »

Maybe a stupid question but I don't get the temperature value read to use the number in a lue script.
Unbenannt.JPG
Unbenannt.JPG (17.91 KiB) Viewed 1047 times
if the command = tonumber(otherdevices['Lightsensor_Ost']) >= 107
the protocoll says "attempt to compare number with nil"


Edit Waltervl: Moved from mqttmapper topic as this is a Lua question.
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

Would it be possible to get the full script (or at least lines around the command)?

As written, syntax seems to be strange...
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

For better explanation. The LUA script was used with a fibaro smart implant device. I got the values from 3 DS18B20 sensors. The system with Fibaro was not stable enough and lost from time to time the sensors. Luckily there is MqttMapper and the Shelly Plus with Addon is providing the data. After adding the device via MqttMapper to Domoticz I only changed the device name to the same names the Fibaro device had.

Here is line 29 of the script. "Lightsensor_Ost" is exactly the same name from Zwave and MqttMapper

if tonumber(otherdevices['Lightsensor_Ost']) >= 107 and tonumber(otherdevices['Wetter Clouds %']) < 41 and uservariables['Shutter_Loop_Ost'] == 0 and otherdevices['Solar_Timer_Ost'] == 'Off' and otherdevices['Sonnensensor_Ost An/Aus'] == 'On' then
commandArray['Solar_Timer_Ost'] = 'On'
commandArray['Variable:Shutter_Loop_Ost'] = '1'
print('Solar_Timer_Ost = AN')

protocoll error
Error: EventSystem: in script_shutter_sun.lua: [string "-- script_shutter_sun.lua ..."]:29: attempt to compare number with nil
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

Could you insert this code just before line 29, in order to see which variable is nil?

Code: Select all

print(otherdevices['Lightsensor_Ost'])
print(otherdevices['Wetter Clouds %'])
print(uservariables['Shutter_Loop_Ost'])
print(otherdevices['Solar_Timer_Ost'])
print(otherdevices['Sonnensensor_Ost An/Aus'])
Getting new error line, you'll be able to tell which is the first valiable set to nil.
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

here we have the result

2024-08-13 17:20:00.278 Status: LUA: 0.00
2024-08-13 17:20:00.278 Status: LUA: 0
2024-08-13 17:20:00.278 Status: LUA: Off
2024-08-13 17:20:00.278 Status: LUA: On

seems to be the Lightsensor_Ost which generate the problem. No idea why
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

here we have the device list from MqttMapper
Unbenannt.JPG
Unbenannt.JPG (34.24 KiB) Viewed 967 times
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

Strange...

What's Domoticz version? MqttMapper version? OS version?

Is it possible to get (part of) MqttMapper json configuration file, I'd like making test here.
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

And a stupid question: why making a light sensor (normally lux) as a temp one?
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

There are no stupid questions. The Sensor Module I am using is a tiny PCB Board with a lightsensor. The Output is simulating a Ds18B20. So the equivalent Lux value is a temperture. Lux value is calculated Ev=e(temp/10). But this is not the problem. Fibaro smart implant also could only handle DS18B20 temperature Sensors. There is no Lux Sensor on the market which messures up to 100000 lux

I am on Domoticz 2024.4 on a RPI 3 with Bullseye.

part of the Json file
"Lightsensors Ost":{
"topic": "Lightsensors/status/temperature:100",
"type": "80", "subtype":"5",
"mapping":{"item":"tC"}

Mqtt Explorer topic : Lightsensors/status/temperature:100

status = {"id": 100,"tC":85.1, "tF":185.1}
tC is the value in Celsius and tF in Fahrenheit
Last edited by thaui on Wednesday 14 August 2024 10:46, edited 1 time in total.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Lua, Read data from device

Post by waltervl »

Yes but in Domoticz you can just define it as a Lux sensor with MQTT mapper and send the correct value. So then it looks like a lux sensor in Domoticz instead of a temperature sensor which it is not.
Device Type: 246 Subtype: 1 Lux Illumination (sValue: "float")
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

@waltervl Temperature is fine for me. If I have 93°C I know thats 10200lx .
ost temp.JPG
ost temp.JPG (13.71 KiB) Viewed 902 times
Ost lux.JPG
Ost lux.JPG (14.44 KiB) Viewed 902 times
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Lua, Read data from device

Post by waltervl »

I dont see a reason why Lua would not be able to read the "Lightsensor_Ost" value.
Are there multiple devices with this name "Lightsensor_Ost" ?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

thaui wrote: Wednesday 14 August 2024 10:23 I am on Domoticz 2024.4 on a RPI 3 with Bullseye.

part of the Json file
"Lightsensors Ost":{
"topic": "Lightsensors/status/temperature:100",
"type": "80", "subtype":"5",
"mapping":{"item":"tC"}

Mqtt Explorer topic : Lightsensors/status/temperature:100

status = {"id": 100,"tC":85.1, "tF":185.1}
tC is the value in Celsius and tF in Fahrenheit
Are you sure about topic content?

Code: Select all

status = {"id": 100,"tC":85.1, "tF":185.1}
... is not a valid json format, and is interpreted as text by MqttMapper.

To be seen as json, topic should be:

Code: Select all

{"status": {"id": 100,"tC":85.1, "tF":185.1}}
If you can change payload, then do it. If not, we should parse it manually, to extract data with a script. I can provide it if needed.

Unless payload content is only

Code: Select all

{"id": 100,"tC":85.1, "tF":185.1}
In this case, with a 2024.7, on RPi 4 with Buster and MqttMapper 1.0.42, sending payload "{"id": 100,"tC":85.1, "tF":185.1}" to "Lightsensors/status/temperature:100" properly loads data into Domoticz.
LightsensorOst.png
LightsensorOst.png (11.13 KiB) Viewed 880 times
LightsensorOst2.png
LightsensorOst2.png (8.13 KiB) Viewed 880 times
Using the following LUA script

Code: Select all

commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName=='Test MQTT - Lightsensors Ost') then
        print ("Device event fired on '"..deviceName.."', value '"..tostring(deviceValue).."', otherDevices '"..tostring(otherdevices['Test MQTT - Lightsensors Ost']).."'")
    end
end

return commandArray
... I got :
LightsensorOst3.png
LightsensorOst3.png (2.87 KiB) Viewed 880 times
So, if payload is just {"id": 100,"tC":85.1, "tF":185.1}, we have to found what are the differences between your configuration. If otherdevices is correctly spelled, check for exact device name match (including leading/trailing spaces).
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

Many thanks for your continues work to find the problem. Sorry I was not clear on the topic content. Maybe the picture gives you the correct response.
mqtt explorer.JPG
mqtt explorer.JPG (28.13 KiB) Viewed 873 times
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

I was imagining that, crossing with other parts of answer, but just wanted to be sure.

So, part to be taken in account is:
FlyingDomotic wrote: Wednesday 14 August 2024 11:38 Unless payload content is only

Code: Select all

{"id": 100,"tC":85.1, "tF":185.1}
In this case, with a 2024.7, on RPi 4 with Buster and MqttMapper 1.0.42, sending payload "{"id": 100,"tC":85.1, "tF":185.1}" to "Lightsensors/status/temperature:100" properly loads data into Domoticz.
LightsensorOst.png
LightsensorOst2.png
Using the following LUA script

Code: Select all

commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName=='Test MQTT - Lightsensors Ost') then
        print ("Device event fired on '"..deviceName.."', value '"..tostring(deviceValue).."', otherDevices '"..tostring(otherdevices['Test MQTT - Lightsensors Ost']).."'")
    end
end

return commandArray
... I got :
LightsensorOst3.png
So, if payload is just {"id": 100,"tC":85.1, "tF":185.1}, we have to found what are the differences between your configuration. If otherdevices is correctly spelled, check for exact device name match (including leading/trailing spaces).
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

your test script will generate the follwing error. : EventSystem: in Test.lua: [string "commandArray = {} ..."]:4: bad argument #1 to 'pairs' (table expected, got nil)
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

Could you confirm you're using "simple" LUA, not dzVents? And if "simple" LUA, with "device" type?

I just cut and pasted code properly running in Domoticz to forum.
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

I updated Domoticz to version 2024.7 and updated MQttMapper to the latest version like in your system. It's running well now. No idea what the reason was. Thanks for your help
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Lua, Read data from device

Post by FlyingDomotic »

I made few changes last days and weeks, it was probably MqttMapper the faulty part ;-)
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: Lua, Read data from device

Post by thaui »

Anyhow, great support from your side and a very helpfull tool. Without it it's difficult to get Shelly Mqtt data to Domoticz.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest