Last value is not written to variable

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:

Last value is not written to variable

Post 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

Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: Last value is not written to variable

Post 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?
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Last value is not written to variable

Post by Rolo »

Thanks for the tips. It has do to be something with "devicechanged", i will do more testing with this.
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Last value is not written to variable

Post 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 ?
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Last value is not written to variable

Post 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
Quality outlives Quantity!
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Last value is not written to variable

Post 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] ?
Rolo
Posts: 52
Joined: Sunday 27 December 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Last value is not written to variable

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest