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

Moderator: leecollings

Post Reply
renerene
Posts: 351
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

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

Post 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.
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post by Westcott »

If it's a dict-type list, write/read it as JSON.
Otherwise perhaps as a Lua list string "{val1, val2, ...}"
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
renerene
Posts: 351
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

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

Post 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
renerene
Posts: 351
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

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

Post 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?
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

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

Post 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.
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest