Page 1 of 1

Last value is not written to variable

Posted: Wednesday 12 October 2016 19:50
by Rolo
If have this setup in Domoticz :
A MySensors node that measures light, it works fine, the values are shown as lux in domoticz.
A lua script that writes the lux value to a variable, so other scripts can read this. This also works, but...
When it gets night the lux sensor goes to zero, it will not transmit the same value's so it's it's quiet until the next morning. The variable then reads 1, this is strange, I expect a zero here because that's the last value that came from the sensor. The time stamps are also different, looks like the variable is the previous value. Any explanation for this ? It's no big deal at night but a day time I would like the variable to be the latest value.

Image

Image

Code: Select all

-- Script script_device_Light Level.lua
-- When the light value chnges it writes the new value to the user Variable : Light
-- This Variable must be created first in Domoticz, type is integer.

lightsensor = "Light Level_Utility"

commandArray = {}

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

return commandArray


Re: Last value is not written to variable

Posted: Wednesday 12 October 2016 20:40
by Flopp
This is what I found in Events as help when you create a new LUA script

Code: Select all

-- device changed contains state and svalues for the device that changed.
--   devicechanged['yourdevicename'] = state 
--   devicechanged['svalues'] = svalues string 
--
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices: 
--   otherdevices['yourotherdevicename'] = "On"
--   otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
--   otherdevices_svalues['yourotherthermometer'] = string of svalues
Can it be that you get the State from the Node?

Have you tried otherdevices_svalues?

Re: Last value is not written to variable

Posted: Friday 14 October 2016 6:07
by Rolo
Thanks for the tips. It has do to be something with "devicechanged", i will do more testing with this.

Re: Last value is not written to variable

Posted: Sunday 13 November 2016 7:45
by Rolo
Ok, did some more testing and it turns out that it's the zero that causes the problem. When the lux value goes to zero this is not written to the variable. Looks like the script handles a zero different. As work arroud the sensor is now not sending the zero value, the minium value transmitted is 1, that works and not having the zero is no big deal. But sure would like to know how to get a zero value written into the variable in a LUA script.
Any tips ?

Re: Last value is not written to variable

Posted: Sunday 13 November 2016 8:49
by gizmocuz
Well it is not a domoticz issue, tested with blockly and this lua

Code: Select all

commandArray = {}
commandArray['Variable:Light']=tostring(0)
return commandArray

Re: Last value is not written to variable

Posted: Sunday 13 November 2016 9:14
by Rolo
Thanks, but this is writing a fixed value (zero) to the variable, I would like to use the sensor value :

Code: Select all

commandArray['Variable:Light']=tostring(devicechanged[lightsensor])
Should I use an IF section first to check if it's zero and then write the fixed (0), if not then write the devicechanged[lightsensor] ?

Re: Last value is not written to variable

Posted: Monday 28 November 2016 19:51
by Rolo
I skipped working with a variable and now query the lightsensor direct in the outdoorlights control script. This works fine, zero lux is also handled well now.

This is the line I use in the control script to read the current lux value :

Code: Select all

lightlevel = tonumber(otherdevices_svalues[lightsensor])   -- Read light level and convert to integer