how to read Device values and write in Text file

Moderator: leecollings

Post Reply
Rezah
Posts: 10
Joined: Wednesday 31 May 2017 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

how to read Device values and write in Text file

Post 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
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

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

Post 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
Rezah
Posts: 10
Joined: Wednesday 31 May 2017 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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."
Rezah
Posts: 10
Joined: Wednesday 31 May 2017 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

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

Post by freijn »

And?

You got it working now?
Rezah
Posts: 10
Joined: Wednesday 31 May 2017 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by Rezah »

yes,its work correctly
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests