Page 1 of 1

for statement - I can't get it to work

Posted: Saturday 02 January 2021 15:44
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
}

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

Posted: Saturday 02 January 2021 16:26
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
}

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

Posted: Saturday 02 January 2021 16:35
by erem
@waaren
you might want to change one off the SwitchOn statements to SwitchOff :D :D

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

Posted: Saturday 02 January 2021 16:51
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!

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

Posted: Saturday 02 January 2021 18:41
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.