How can I read the sensor value in LUA

Moderator: leecollings

Post Reply
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

How can I read the sensor value in LUA

Post by Rolo »

Working on a Lux meter to connect to domoticz, the hardware works, I have the sensor available in Domoticz and it shows changing lux values. But now I want to interact with this lux value to make smart dusk sensors and light switches. This is were the LUA scripts come in, I have read the wiki, I understand the basics but can't get the Lux value written in to a user variable. Can someone point me in the right direction ?
This is what I have now :

Code: Select all

--------------------------------
------ Variables to edit ------
--------------------------------
devicename = "Lux meter" --Name of the device
debug = true
--------------------------------
-- End of variables to edit --
--------------------------------

commandArray = {}

if (devicechanged[devicename]) then

	
	if (debug) then
	    print("Following values are coming in from device: ")
		for i, v in pairs(devicechanged) do print(i, v) end
	end
	 
	-- how can I read the lux value	from the sensor ?
	commandArray['Variable:LUX']='1234'  -- fixed value for now, this should be the current lux
end
	
return commandArray
This is the output in the log

Code: Select all

2016-08-20 10:49:15.004 LUA: Following values are coming in from device:
2016-08-20 10:49:15.004 LUA: Lux meter_Utility
2016-08-20 10:49:15.004 LUA: 508
2016-08-20 10:49:15.004 LUA: Lux meter
2016-08-20 10:49:15.004 LUA:
2016-08-20 10:49:15.006 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Lux meter.lua
So the lux value is there, at line 3, next thing is to write it to the user variable. \

Thanks !
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How can I read the sensor value in LUA

Post by jvdz »

This should work:

Code: Select all

commandArray = {}
luxsensor = "Lux meter_Utility"

if (devicechanged[luxsensor]) then
   print( " ####lux debug: "..devicechanged[luxsensor]
   commandArray['Variable:LUX']=devicechanged[luxsensor]
end

return commandArray
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: How can I read the sensor value in LUA

Post by Rolo »

Thanks, it works (half..)

Debug shows correct value :

Code: Select all

2016-08-20 12:30:36.862 LUA: ####lux debug: 361
But then an error :

Code: Select all

2016-08-20 12:22:28.791 Error: EventSystem: commandArray in script /home/pi/domoticz/scripts/lua/script_device_Lux meter.lua should only return ['string']='actionstring' or [integer]={['string']='actionstring'}
Looks like it's not the correct type.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How can I read the sensor value in LUA

Post by jvdz »

ah..ok, that means you need to convert it to string like:

Code: Select all

commandArray['Variable:LUX']=tostring(devicechanged[luxsensor])
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: How can I read the sensor value in LUA

Post by Rolo »

BIngo, works !

Thanks, I do some coding in C but this LUA takes some getting used to. Now next thing is making a fancy dusk switch.

This is the completed script :

Code: Select all

commandArray = {}
luxsensor = "Lux meter_Utility"

if (devicechanged[luxsensor]) then
   print( " ####lux debug: "..devicechanged[luxsensor])
   commandArray['Variable:LUX']=tostring(devicechanged[luxsensor])
   end

return commandArray

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest