dzVents Wiki in pairs

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

Moderator: leecollings

Post Reply
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

dzVents Wiki in pairs

Post by jkimmel »

A special kind of persistent variables: history = true[edit]
In some situation, storing a previous value for a sensor is not enough and you would like to have more previous values for example when you want to calculate an average over several readings or see if there was a constant rise or decrease. Of course you can define a persistent variable holding a table:

Code: Select all

 return {
        active = true,
        on = {
            devices = {'MyTempSensor'}
        },
        data = {
            previousData = { initial = {} }
        },
        execute = function(domoticz, sensor)
            -- add new data
            table.insert(domoticz.data.previousData, sensor.temperature)

            -- calculate the average
            local sum = 0, count = 0
            for i, temp in pairs(domoticz.data.previousData) do
                sum = sum + temp
                count = count + 1
            end
            local average = sum / count
        end
    }

In pairs isn't valid anymore?
Rfxcom
Raspi 4
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents Wiki in pairs

Post by dannybloe »

You need to use forEach to iterate over the values. See the docs about this.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Re: dzVents Wiki in pairs

Post by jkimmel »

dannybloe wrote:You need to use forEach to iterate over the values. See the docs about this.
I quoted out of the docs!
Rfxcom
Raspi 4
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents Wiki in pairs

Post by dannybloe »

Eh yeah. Guess I didn't get your question. Sorry. What exactly is the problem?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Re: dzVents Wiki in pairs

Post by jkimmel »

A special kind of persistent variables: history = true[edit]
In some situation, storing a previous value for a sensor is not enough and you would like to have more previous values for example when you want to calculate an average over several readings or see if there was a constant rise or decrease. Of course you can define a persistent variable holding a table:
This quote is from the actual wiki. Docs should be updated, the example is outdated
Rfxcom
Raspi 4
User avatar
DewGew
Posts: 581
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Re: dzVents Wiki in pairs

Post by DewGew »

How do I convert this code from lua to dzVents:

Code: Select all

for i, v in pairs(devicechanged) do phone = i end
print('<font color="green">' ..phone.. ' is home</font>')
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents Wiki in pairs

Post by dannybloe »

Code: Select all

domoticz.changedDevices.forEach(function(device)
	domoticz.log(device.name .. ' is home', domoticz.LOG_INFO)
end)
RTLM
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
DewGew
Posts: 581
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Re: dzVents Wiki in pairs

Post by DewGew »

dannybloe wrote:

Code: Select all

domoticz.changedDevices.forEach(function(device)
	domoticz.log(device.name .. ' is home', domoticz.LOG_INFO)
end)
RTLM
GREAT Thanks :)

Edit:
Correct code is:

Code: Select all

domoticz.changedDevices().forEach(function(device)
	domoticz.log(device.name .. ' is home', domoticz.LOG_INFO)
end)
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest