Accessing table elements

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Janco
Posts: 17
Joined: Monday 25 April 2016 10:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Accessing table elements

Post by Janco »

I'm not very familiar with dzVents nor with LUA, but the recent problems with timer forced me to have a look at it.

However, I've got a problem which I do not really understand. I created a table representing groups of thermostats
in global_data.lua. I call the helper to set the setpoint of all thermostats in a group by calling for the helper function
switchHeatingGroup, e.g. switchHeatingGroups(domoticz, 'a', 17.0). This works, but I would like to use more meaningful
group names like for instance "Heating Living" or "All radiators". When I change the key names 'a', 'b', etc. by "Heating Living', 'All' etc.
it doesn't work anymore and I get an error message telling me that I'm trying to index a nil value.

Why can I use single characters as key names, but nog strings?
Af far as I found out in the forum LUA would accept this, so why doesn't dzVents?

Code: Select all

return {
    helpers = {
         switchHeatingGroup = function(domoticz, thermostatGroupName, setPoint) 
            local thermostat
            local thermostats = domoticz.globalData.thermostatGroups[thermostatGroupName]
            for i, thermostatName in pairs(thermostats) do
                thermostat = domoticz.devices(thermostatName)
                thermostat.updateSetPoint(setPoint)
            end
        end
    },
    data = {
        groupLiving = {
            initial = {'Heating Living N', 'Heating Living S', 'Heating Living W'}
        },
        thermostatGroups = {
            initial = {
                ['a'] = {'Heating Living N', 'Heating Living S', 'Heating Living W'},
                ['b'] = {'Heating Kitchen'},
                ['c'] = {'Heating Victor'},
                ['d'] = {'Heating Tomas'},
                ['e'] = {'Heating Bedroom'},
                ['f'] = {'Heating Office'},
                ['g'] = {'Heating Bathroom Kids'},
                ['h'] = {'Heating Living N', 'Heating Living S', 'Heating Living W',
                        'Heating Dining Room', 'Heating Kitchen'},
                ['i'] = {'Heating Living N', 'Heating Living S', 'Heating Living W',
                        'Heating Kitchen'},
                ['j'] = {'Heating Living N', 'Heating Living S', 'Heating Living W',
                        'Heating Dining Room', 'Heating Kitchen',
                        'Heating Tomas', 'Heating T', 'Heating Bedroom',
                        'Heating Office', 'Heating Bathroom Kids'},
                ['k'] = {'Heating Living N', 'Heating Living S', 'Heating Living W',
                        'Heating Dining Room', 'Heating Kitchen',
                        'Heating V', 'Heating Bedroom',
                        'Heating Office', 'Heating Bathroom Kids'}
                }
        }
    }
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Accessing table elements

Post by dannybloe »

First of all, dzVents is just Lua with a domoticz data model and api. Other than the model it's just plain old Lua.

However, what I think happened is that you changed the initial table after the variable was created and got its value (and got persisted). The initial table is only applied if the variable doesn't exist is or nil. So, if you first try it with 'a' and then change the initial table to 'some name' and in the next event run you try to access 'some name' then of course that entry doesn't exist because thermostatGroups isn't nil anymore and the table doesn't have a 'some name' entry.

So, there are a couple of ways to fix this. Easiest is to remove the __global_data_data.lua from scripts/dzVents/data (that's where the persistence is stored). Or do (somehow, somewhere) domoticz.globalData.thermostatGroups = nil and the next time your script is executed, the variable gets initialized again. (And I'll plan for a re-initialize method in a future dzVents update, thanks for the tip ;) )

Or... and I think that is better in your case, don't use persistence for this at all. If you only need that thermostatGroups table in the global_data module then you can just define it as a local table and edit it there:

Code: Select all

local thermostatGroups = {
    ['some name'] = {'Heating Living N', 'Heating Living S', 'Heating Living W'},
    ['b'] = {'Heating Kitchen'},
    ['c'] = {'Heating Victor'},
    ['d'] = {'Heating Tomas'},
    ['e'] = {'Heating Bedroom'},
    ['f'] = {'Heating Office'},
    ['g'] = {'Heating Bathroom Kids'}
    ---
}

return {
    helpers = {
         switchHeatingGroup = function(domoticz, thermostatGroupName, setPoint) 
            local thermostat
            local thermostats = thermostatGroups[thermostatGroupName]
            for i, thermostatName in pairs(thermostats) do
                thermostat = domoticz.devices(thermostatName)
                thermostat.updateSetPoint(setPoint)
            end
        end
    }
}
And stay tuned for dzVents 2.4. There you get a list of devices that are defined in a group or a scene. Then you can do this:

Code: Select all

domoticz.groups('Living').devices().forEach(function(d)
	d.updateSetPoint(20)
end)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Janco
Posts: 17
Joined: Monday 25 April 2016 10:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Accessing table elements

Post by Janco »

Thanks for the explanation and suggestions! And yes, it seems it is not necessary to make the table persistent. I only plan to use it from within global_data, so local will probably do.
Looking forward to the new version, by the way.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest