Page 1 of 1

how to read Device values and write in Text file

Posted: Wednesday 05 July 2017 13:22
by Rezah
hi there
i want to read all devices value and write them in a sensor.txt file.
i write a script for read from one device and write to text file and its work, but when i want read more than one device i get several error.

First code : read from one device

Code: Select all

commandArray = {}
luxsensor = "ESP2_Lux"

if (devicechanged[luxsensor]) then
   print( " ####lux debug: "..devicechanged[luxsensor])
   
   file = io.open("sensor.txt", "w+") 
    file:write("Sensors Status \n")
    file:write(luxsensor.." : "..devicechanged[luxsensor].."\n")
    file:close()
   end

return commandArray

second code: read from more than one device

Code: Select all

commandArray = {}
luxsensor = "ESP2_Lux"
smokesensor = "Smoke_Sensor"

if (devicechanged[luxsensor]) then
   print( " ####lux debug: "..devicechanged[luxsensor])
   file = io.open("sensorl.txt", "w+") 
    file:write("Sensors Status \n")
    file:write(luxsensor.." : "..devicechanged[luxsensor].."\n")
    file:write(smokesensor.." : "..devicechanged[smokesensor].."\n")
    file:close()
   end

return commandArray

i get this:
2017-07-01 15:26:29.442 Error: EventSystem: in Lua LUX: [string "commandArray = {} ..."]:13: attempt to concatenate field '?' (a nil value)


please help me
tnx

Re: how to read Device values and write in Text file

Posted: Wednesday 05 July 2017 21:42
by freijn
You do the first action on basis that the luxsensor value is changed.

In the second script you do the same only for the smokesensor there is nothing changed so I guess no info so no content which is a nil value.

Not sure home many sensors you are planning to log but why not make the same ( single sensor script ) for the smokesensor?

See below..

or......
Store all devicechanged values of sensors in variables, then have a separate script writing all variables in the file.
You could trigger the write file script by 1 of the deviced changed scripts.

Hope my story is not too smokey :-)


commandArray = {}
smokesensor = "Smoke_Sensor"

if (devicechanged[smokesensor]) then
print( " ####lux debug: "..devicechanged[smokesensor])

file = io.open("sensor.txt", "w+")
file:write("Sensors Status \n")
file:write(lsmokesensor.." : "..devicechanged[smokesensor].."\n")
file:close()
end

return commandArray

Re: how to read Device values and write in Text file

Posted: Thursday 06 July 2017 10:46
by Rezah
dear frejin

i tried to write same script for other devices, but i get errors.also i need to write all states of my devices to one file at same time.
lux is a parameter which changed every time and its good condition for me to read all device continuously.

but

i have try your second solution.
before that,can domoticz store values in userVariable automatically?

"Store all devicechanged values of sensors in variables, then have a separate script writing all variables in the file.
You could trigger the write file script by 1 of the deviced changed scripts."

Re: how to read Device values and write in Text file

Posted: Thursday 06 July 2017 14:29
by Rezah
i try frejin solution

Step 1:
write Script for all devices which i need them.
if device changed store the value in userVariables.

Code: Select all

commandArray = {}
luxsensor = "ESP2_Lux"

if (devicechanged[luxsensor]) then
   commandArray['Variable:varLux']=devicechanged[luxsensor]
end

return commandArray

Code: Select all

commandArray = {}
smokesensor = "Smoke_Sensor"

if (devicechanged[smokesensor]) then
   commandArray['Variable:varSmoke']=devicechanged[smokesensor]
end

return commandArray
Step 2:
write a script for aggregation userVariables and write them in text File.

Code: Select all

commandArray = {}
luxsensor = "ESP2_Lux"
smokesensor = "Smoke_Sensor"

year 	= tonumber(os.date("%Y"));
month 	= tonumber(os.date("%m"));
day 	= tonumber(os.date("%d"));
hour 	= tonumber(os.date("%H"));
min 	= tonumber(os.date("%M"));


if (2==2) then
   file = io.open("sensorl.txt", "w+") 
   -- Opens a file named sensor.txt(stored under Domoticz folder) 
   -- in append mode
    --write to opened file
    file:write(year .. "-" .. month .. "-"..day.."   "..hour..":"..min.."\n")
    file:write(luxsensor.." : "..uservariables['varLux'].."\n")
    file:write(smokesensor.." : "..uservariables['varSmoke'].."\n")
    file:close() --closes the open file
   end

return commandArray

Re: how to read Device values and write in Text file

Posted: Thursday 06 July 2017 20:23
by freijn
And?

You got it working now?

Re: how to read Device values and write in Text file

Posted: Saturday 08 July 2017 11:49
by Rezah
yes,its work correctly