Page 1 of 1

[Solved] dzVents trying to use persistant data with concat variable

Posted: Friday 26 January 2018 19:32
by rbisschops
Hi all,

I need some help on this, I guess my LUA knowledge runs short here.
I try to do this:

Code: Select all

return {
    active = true,
    on = {
        timer = {
            'every 1 minutes'
        }
    },
    data = {
        prevPresenceRalph = {initial='Off'},
        prevPresenceAnneke = {initial='Off'},
        prevPresenceKim = {initial='Off'}
    },
    logging = {
        level= domoticz.LOG_DEBUG,
        marker = 'Wifi presence check'       
    },
    execute = function(domoticz)
        local assetName
        local gVirtHome = domoticz.devices('VirtualPresence') -- variable for Virtual presence switch
        local presence = 'VirtualAtHome'
        local oldPresence = 'prevPresence'
        local wifiPresence = 'VirtualArping'
        local presenceSwitch

        local _members = domoticz.devices().filter(function(device)
            return (device.name:sub(1,13) == wifiPresence)
        end).forEach(function(_members)
            domoticz.log (_members.name)
            assetName = _members.name:sub(14)
            presenceSwitch = domoticz.devices(presence..assetName)
            prevWifiSwitch = (oldPresence..assetName)
            domoticz.log (domoticz.data.prevWifiSwitch) 
            domoticz.data.prevWifiSwitch = _members.state
        end)
    end
    }
This line:

Code: Select all

domoticz.log (domoticz.data.prevWifiSwitch) 
throws an error in the log as it is 'nil'
I believe this has to do with the fact that I tried to concat two variables and us that as part of the domoticz.data.prevWifiSwitch. If I replace it with a hard pesistent variable it works, but obviously I want it to run with all variables in the loop.
How do I solve this is correct LUA.

I'm using V2.2.0.
The total code might not be working as I stripped out a large part of the code for readability.

Thx for your suggestions,

Ralph

Re: dzVents trying to use persistant data with concat variable

Posted: Friday 26 January 2018 19:54
by dannybloe
I guess it should be domoticz.log(prevWifiSwitch)

Re: dzVents trying to use persistant data with concat variable

Posted: Saturday 27 January 2018 7:54
by rbisschops
dannybloe wrote: Friday 26 January 2018 19:54 I guess it should be domoticz.log(prevWifiSwitch)
Hi @dannybloe. Thx for yor as always fast answer. However that is not goig to work. It will only log the concat string that is in prevWifiSwitch. So in my case prevPresenceRalph, PrevPresenceAnneke, ect. I need the value of the persistant data variables, so Off, Off, etc. that is why i tried using domoticz.data.prevWifiSwitch.

Thx again

Ralph

Re: dzVents trying to use persistant data with concat variable

Posted: Saturday 27 January 2018 8:01
by dannybloe
But that variable doesn’t exist in domoticz.data for what I can see. It’s not in the data section. So you are printing a nil value.

Re: dzVents trying to use persistant data with concat variable

Posted: Saturday 27 January 2018 9:18
by rbisschops
I haven't had coffee yet, so l'm at risk here. As far as I can see it is. Let me explane my thinking.
prevWifiSwitch takes the values of oldPresence (=prevPresence). It is concatenated with the value of a part of a virtual Switch that is in assetName These are values like Ralph, Anneke. So in the first case this makes prevPresenceRalph. So if i do domoticz.log (domoticz.data.prevWifiSwitch, it should translate to domoticz.data.prevPresenceSwitchRalph. And that variable is in the data section. But this construction is not working as for the reason described above. So my million dollar questions is how to do that. I can't use the variable itself, because it needs to change for the various persons within the loop.

Thx

Ralph

Re: dzVents trying to use persistant data with concat variable

Posted: Saturday 27 January 2018 10:41
by dannybloe
Well, I still think you are wrong here. If you look in your data section there's this:

Code: Select all

data = {
	prevPresenceRalph = {initial='Off'},
	prevPresenceAnneke = {initial='Off'},
	prevPresenceKim = {initial='Off'}
}
Then, in your loop you do this:

Code: Select all

	prevWifiSwitch = (oldPresence..assetName)
	domoticz.log (domoticz.data.prevWifiSwitch)
	domoticz.data.prevWifiSwitch = _members.state
So, the first time you do domoticz.log (domoticz.data.prevWifiSwitch) it doesn't exist and Lua will throw an error. In the next line you create the entry in domoticz.data (which is useless for persistence reasons coz dzVents doesn't store it as it is not defined in the data section).

I suspect that you intent to do:

Code: Select all

	prevWifiSwitch = (oldPresence..assetName)
	domoticz.log (domoticz.data[prevWifiSwitch])
	domoticz.data[prevWifiSwitch] = _members.state
Which is significantly different. Note the [ and ].

Re: [Solvedl] dzVents trying to use persistant data with concat variable

Posted: Saturday 27 January 2018 16:42
by rbisschops
Thx @dannybloe, that was exactly what I was looking for. This works! :D