Page 1 of 1

How to read/write a list (array) into a variable?

Posted: Monday 24 July 2017 11:37
by renerene
How to read/write a list (array) into a variable?

I want to read, update and store some sensor values in my LUA script. By storing the whole list in one variable, adding a sensor is easy, without the need for extra variables. Just add another list item.

In dzVents with it's global variables this task would be easier. But until dzVents is supported in a stable Domoticz release, I have to use LUA.

Re: How to read/write a list (array) into a variable?

Posted: Monday 24 July 2017 11:44
by Westcott
If it's a dict-type list, write/read it as JSON.
Otherwise perhaps as a Lua list string "{val1, val2, ...}"

Re: How to read/write a list (array) into a variable?

Posted: Tuesday 25 July 2017 0:10
by renerene
thank you.
With JSON no experience, but with help from this page I've got it working. The joined string goes to the string variable.

Code: Select all

function meld (melding,prio)
    if (not prio) then print ('<font color="purple">test: E R R O R geen prio meegegeven') end
    if debug==1 then print ('<font color="purple">test: '..melding) end
    if prio~=99 then 
       commandArray[index]={['SendNotification']='test#'..melding..'#'..prio}
       index = index + 1
    end
end   

function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
         table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

commandArray = {}

l = {    -- timeout for each  device, in seconde.
        'PIR_woonkamer', 172800, 1,             -- 2 dagen
        'PIR badkamer',172800, 2               -- 2 dagen
    }
debug=1
meld ('starttest',99)
    i=1
    meld ('the original elements:',99)
    meld ('**********************',99)
    while l[i] do
      meld (i..':'..l[i]..'/'..l[i+1]..'/'..l[i+2],99)
      i = i + 3
    end    
    
    s=table.concat(l, ";") 
    meld ('the joined string:',99)
    meld ('**********************',99)
    meld (s,99)
    
    l=split (s,';')
    i=1
    meld ('the join+split elements:',99)
    meld ('**********************',99)
    while l[i] do
      meld (i..':'..l[i]..'/'..l[i+1]..'/'..l[i+2],99)
      i = i + 3
    end    
return commandArray

Result:

2017-07-25 00:07:23.806 LUA: test: starttest
2017-07-25 00:07:23.806 LUA: test: the original elements:
2017-07-25 00:07:23.807 LUA: test: **********************
2017-07-25 00:07:23.807 LUA: test: 1:PIR_woonkamer/172800/1
2017-07-25 00:07:23.807 LUA: test: 4:PIR badkamer/172800/2

2017-07-25 00:07:23.808 LUA: test: the joined string:
2017-07-25 00:07:23.808 LUA: test: **********************
2017-07-25 00:07:23.809 LUA: test: PIR_woonkamer;172800;1;PIR badkamer;172800;2

2017-07-25 00:07:23.810 LUA: test: the join+split elements:
2017-07-25 00:07:23.810 LUA: test: **********************
2017-07-25 00:07:23.810 LUA: test: 1:PIR_woonkamer/172800/1
2017-07-25 00:07:23.810 LUA: test: 4:PIR badkamer/172800/2

Re: How to read/write a list (array) into a variable?

Posted: Tuesday 25 July 2017 7:06
by renerene
Aaarghhh:
Domoticz string variable maximum size is 200 bytes
Source: https://www.domoticz.com/wiki/User_variables

(For now) I don't want to check size and spread over multiple variables.

Can you tell me more about the JSON option?

Re: How to read/write a list (array) into a variable?

Posted: Tuesday 25 July 2017 8:32
by mivo
Hi,

if you are on stable Domoticz version, you can still use dzVents version 1.x, which also has persistent variables.
https://github.com/dannybloe/dzVents

I think it is easier way than create JSON, store to file etc.