Page 1 of 1

dzVents Wiki in pairs

Posted: Monday 07 August 2017 10:37
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?

Re: dzVents Wiki in pairs

Posted: Monday 07 August 2017 12:25
by dannybloe
You need to use forEach to iterate over the values. See the docs about this.

Re: dzVents Wiki in pairs

Posted: Monday 07 August 2017 16:53
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!

Re: dzVents Wiki in pairs

Posted: Monday 07 August 2017 16:58
by dannybloe
Eh yeah. Guess I didn't get your question. Sorry. What exactly is the problem?

Re: dzVents Wiki in pairs

Posted: Tuesday 08 August 2017 17:30
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

Re: dzVents Wiki in pairs

Posted: Wednesday 09 August 2017 8:25
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>')

Re: dzVents Wiki in pairs

Posted: Wednesday 09 August 2017 9:36
by dannybloe

Code: Select all

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

Re: dzVents Wiki in pairs

Posted: Wednesday 09 August 2017 10:10
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)