Page 1 of 1

Script help for PIR / light

Posted: Thursday 31 December 2020 14:33
by rolandtwilt
Friends I've worked with Blocky so far, however I can't possibly make what is needed in Blocky. So I started dzVents. I copied some ideas and ended up with this script ... with the necessary errors. I'll leave the script and error message here and hope you guys can see where I'm going wrong? Thanks in advance and a happy New Year!:

Code: Select all

--Declarations
local HBSNum1       = 85 -- Sensor Boven (vul het id in) (dit geeft het id aan van de sensor)
local HBSNum2       = 87 -- Sensor Hal (vul het id in) (dit geeft het id aan van de sensor)

return {
    on = { devices = {
            HBSNum1,  -- Sensor Boven (Maakt een device aan met het id dat gegeven is.)
            HBSNum2 -- Sensor Hal (Maakt een device aan met het id dat gegeven is.)
        }
    },
    execute = function(domoticz, device) 

        local LampHal     = domoticz.devices(96) --Lamp Hal (Haalt het device op voor het device met het gegeven id)
        local HBS1       = domoticz.devices(HBSNum1) -- Sensor Boven (Haalt het device op met het gegeven id.)
        local HBS2       = domoticz.devices(HBSNum2) -- Sensor Hal (Haalt het device op met het gegeven id.)
        
        if (HBS1.active) then -- (Als Sensor Boven aan gaat dan)
                LampHal.switchOn() -- (Zet Lamp Hal aan)
                repeat -- (Herhaal)
                      local now = os.time() + 60  -- (Simpele methode om te kijken hoe laat het over 1 minuut is)
                    if (LampHal.state == "Off") then  --(Als LampHal staat uit dan)
                        LampHal.switchOn() --(Zet lamphal aan)
                    end -- (einde als statement)
             until os.time() >= now --(herhaal tot tijd gelijk is aan de tijd 1 minuut geleden)\
                 LampHal.switchOff()
    elseif (HBS2.active) then -- (Als Sensor hal aan gaat dan)
        LampHal.switchOn() -- (Zet Lamp Hal aan)
                repeat -- (Herhaal)
                      local now = os.time() + 60  -- (Simpele methode om te kijken hoe laat het over 1 minuut is)
                    if (LampHal.state == "Off") then  --(Als LampHal staat uit dan)
                        LampHal.switchOn() --(Zet lamphal aan)
                    end -- (einde als statement)
             until os.time() >= now --(herhaal tot tijd gelijk is aan de tijd 1 minuut geleden)\
                 LampHal.switchOff()
     end
         LampHal.switchOff() --(Zet lamp Hal uit)
end
}

Code: Select all

020-12-31 12:46:26.094 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-12-31 12:46:26.093 Error: dzVents: Error: (3.0.2) An error occurred when calling event handler hbs
2020-12-31 12:46:26.093 Error: dzVents: Error: (3.0.2) Lua script execution exceeds maximum number of lines
2020-12-31 12:46:27.016 (RFLink) Light/Switch (verlichting hal bg)

Re: dzVents not working after upgrade from 11670 to 11724.

Posted: Thursday 31 December 2020 15:34
by waaren
rolandtwilt wrote: Thursday 31 December 2020 14:33 Friends I've worked with Blocky so far, however I can't possibly make what is needed in Blocky. So I started dzVents. I copied some ideas and ended up with this script ... with the necessary errors. I'll leave the script and error message here and hope you guys can see where I'm going wrong?
You don't' want to use loops in event scripts like this. You will block the event system completely because the event system is single threaded. To protect the rest of domoticz the main worker of domoticz will kill the script after about 10 seconds.

If I understand your intended use of the script correctly, below version will do the same using standard dzVents methods.

Code: Select all

return 
{
    on = 
    { 
        devices = 
        {
            85,  -- Sensor Boven 
            87,  -- Sensor Hal 
        },
    },

    execute = function(domoticz, item) 

        local LampHal = domoticz.devices(96) -- Lamp Hal (dzVents device object)

        if item.active then -- If one of the sensors are updated to active
           LampHal.cancelQueuedCommands() -- Cancel all scheduled commands for this device
           LampHal.switchOn().checkFirst() 
           LampHal.switchOff().afterSec(60)
        end
    end
}

Re: Script help for PIR / light

Posted: Thursday 31 December 2020 17:06
by rolandtwilt
Thank you!! when I walk from bottom to top, the lamp turns on and then turns off after 60 seconds. When I walk from top to bottom the lamp stays on ..... what am I doing wrong? :roll:

Re: Script help for PIR / light

Posted: Thursday 31 December 2020 17:14
by waaren
rolandtwilt wrote: Thursday 31 December 2020 17:06 Thank you!! when I walk from bottom to top, the lamp turns on and then turns off after 60 seconds. When I walk from top to bottom the lamp stays on ..... what am I doing wrong? :roll:
Hard to tell without log...

I added a log line in below version. It will not solve the issue but might help in identifying a cause.

Code: Select all

return
{
    on =
    {
        devices =
        {
            85,  -- Sensor Boven
            87,  -- Sensor Hal
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'PIR control',
    },


    execute = function(domoticz, item)

        domoticz.log(item.name .. ', State: ' .. item.state .. ', active is ' .. tostring(item.active), domoticz.LOG_DEBUG )

        local LampHal = domoticz.devices(96) -- Lamp Hal (dzVents device object)

        if item.active then -- If one of the sensors are updated to active
           LampHal.cancelQueuedCommands() -- Cancel all scheduled commands for this device
           LampHal.switchOn().checkFirst()
           LampHal.switchOff().afterSec(60)
        end
    end
}

Re: Script help for PIR / light

Posted: Thursday 31 December 2020 18:39
by rolandtwilt
:D Thanx! En happy New Year

Re: Script help for PIR / light

Posted: Saturday 02 January 2021 20:00
by rolandtwilt
It works!! thank you

Re: Script help for PIR / light

Posted: Saturday 02 January 2021 23:02
by waaren
rolandtwilt wrote: Saturday 02 January 2021 20:00 It works!! thank you
Did you change something to make it work ?

Re: Script help for PIR / light

Posted: Sunday 03 January 2021 8:31
by rolandtwilt
No, it was not necessary ..... an old script was still running and that caused problems :oops: . Now that the old script is turned off, your script works like a charm. Super thanks!!