for statement - I can't get it to work

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

Moderator: leecollings

Post Reply
bertbigb
Posts: 147
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

for statement - I can't get it to work

Post by bertbigb »

Dear All,

First of all I want to wish everyone on this board a very good and healthy 2021.

I hope someone can help me with my problem that I don't get the " for i=x to y do" statement to work proper.
It gives me the following error
dzVents: Error: (3.0.2) ...s/dzVents/generated_scripts/SendCommandAgainAfterSec.lua:27: attempt to call a nil value


It would be appreciated when someone can help or guide me how to solve this problem.
Many thanks in advange.

Code: Select all

-- #SENDCOMMANDMULTIPLEAFTER SECONDS

local attempts = 20

return {
        active = true,
        on = {
            devices = { 
                "Tuinlamp",
                "Veranda",
                "Kerst"
                } -- add the list of devices you want to boost here
        },
        data = {
            counter = {initial=0} -- initiate the counter to zero
        },
        execute = function(domoticz, currentSwitch) -- gives a burst of On/Off signals
            if (currentSwitch.state == 'On') then
                for i,attempts in counter do
                -- for i in (counter:attempts) do
            	    currentSwitch.switchOn().afterSec(i)
            	    domoticz.log('Switch '..currentSwitch..' set SwitchOn after '.. i .. 'seconds', domoticz.LOG_INFO)
            	    i=i+1
            	end
            end
            if (currentSwitch.state == 'Off') then
                for i,attempts in counter do
            	    currentSwitch.switchOff().afterSec(i)
            	    domoticz.log('Switch '..currentSwitch..' set SwitchOff after '.. i .. 'seconds', domoticz.LOG_INFO)
            	    i=i+1
            	end
            end
        end
}
Best regards Bert

Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: for statement - I can't get it to work

Post by waaren »

bertbigb wrote: Saturday 02 January 2021 15:44 I hope someone can help me with my problem that I don't get the " for i=x to y do" statement to work proper.
Can you try this one?

Code: Select all

-- #SENDCOMMANDMULTIPLEAFTER SECONDS

local attempts = 20

return
{
    on =
    {
        devices =
        {
            "Tuinlamp",
            "Veranda",
            "Kerst"
        },
    },

    logging =
    {
        level = domoticz.LOG_INFO,
        marker = 'Multiple commands',
    },

        execute = function(dz, item)

            item.cancelQueuedCommands()

            for i = 1, attempts do
                if item.active then
                    item.switchOn().afterSec(i).silent()
                else
                    item.switchOff().afterSec(i).silent()
                end
                dz.log('Switch '..item.name  ..' set to ' .. item.state .. '( ' .. i .. ' ) seconds', dz.LOG_INFO)
           end
        end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: for statement - I can't get it to work

Post by erem »

@waaren
you might want to change one off the SwitchOn statements to SwitchOff :D :D
Regards,

Rob
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: for statement - I can't get it to work

Post by waaren »

erem wrote: Saturday 02 January 2021 16:35 @waaren
you might want to change one off the SwitchOn statements to SwitchOff :D :D
It wasn't me.. :oops:
Copy / paste did it :!:


Thx!
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bertbigb
Posts: 147
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

Re: for statement - I can't get it to work

Post by bertbigb »

Wonderfull, this did the job.
Thanks for your very quick help.
Now my RF433 KAKU will not miss a signal anymore I expect.
Best regards Bert

Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest