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

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

Moderator: leecollings

Post Reply
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

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

Post 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
Last edited by rbisschops on Saturday 27 January 2018 16:43, edited 2 times in total.
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents trying to use persistant data with concat variable

Post by dannybloe »

I guess it should be domoticz.log(prevWifiSwitch)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: dzVents trying to use persistant data with concat variable

Post 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
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents trying to use persistant data with concat variable

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: dzVents trying to use persistant data with concat variable

Post 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
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents trying to use persistant data with concat variable

Post 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 ].
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

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

Post by rbisschops »

Thx @dannybloe, that was exactly what I was looking for. This works! :D
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest