Page 2 of 2

Re: Problem with the blockly script turning on the light

Posted: Thursday 05 February 2026 17:07
by Maciek90
Individually

Re: Problem with the blockly script turning on the light

Posted: Friday 06 February 2026 9:05
by Dave21w
If turned on individually do you want to disable the auto on via pir of both or only the one you have manually turned on

Re: Problem with the blockly script turning on the light

Posted: Friday 06 February 2026 18:31
by Maciek90
Only the one I turned on manually.
The script from madpatrick works exactly as I want. However, there's a problem, and I don't know what's causing it. Every few days, one of the lights won't turn off after a set time (30 seconds) after the motion sensor signal is turned off. I had the same problem with my script in Blockly.

Re: Problem with the blockly script turning on the light

Posted: Friday 06 February 2026 19:10
by madpatrick
Sounds it is not an script issue.
You can give extra/second switch off command but that makes no sense

Re: Problem with the blockly script turning on the light

Posted: Friday 06 February 2026 19:40
by Maciek90
Sounds it is not an script issue.
I thought about that too
You can give extra/second switch off command but that makes no sense
I tried something like this in blockly but it didn't help
However, I noticed that this problem was more common with blockly. With dzVents, it happens once every few days. Any ideas what might be causing this?

Re: Problem with the blockly script turning on the light

Posted: Friday 06 February 2026 19:44
by madpatrick
Is the switched off command triggered when it happens
If the command is send to the switch then it is not a acriot issue but hardware

Re: Problem with the blockly script turning on the light

Posted: Friday 06 February 2026 20:01
by madpatrick
Can you try this

it should switch off the lamp 30 after after the last movement. When there will be new movement within the 30 sec it will reset to 30 sec again

Code: Select all

local SCRIPT_NAME = 'PIR'
local LOG_LEVEL = domoticz.LOG_FORCE
local OFF_DELAY = 30

return {
    on = {
        devices = {68, 69, 1574, 1575},
    },

    logging = {
        level = LOG_LEVEL,
        marker = SCRIPT_NAME
    },

    execute = function(dz, device)

        local pir1 = dz.devices(69)
        local pir2 = dz.devices(1575)
        local lamp1 = dz.devices(68)
        local lamp2 = dz.devices(1574)
        local lamp1PirVar = dz.variables('Lamp1_PIR')
        local lamp2PirVar = dz.variables('Lamp2_PIR')

        ----------------------------------------------------------------
        -- PIR 1 / Lamp 1
        ----------------------------------------------------------------

        -- PIR detects motion → lamp ON or timer reset
        if device.id == pir1.id and pir1.state == 'On' then

            if lamp1.state == 'Off' then
                lamp1.switchOn()
                dz.log('PIR1 → Lamp1 ON', LOG_LEVEL)
            end

            -- mark lamp as PIR-controlled
            lamp1PirVar.set(1)

            -- cancel any pending OFF by reasserting ON
            lamp1.switchOn().checkFirst()
            dz.log('PIR1 → OFF timer reset', LOG_LEVEL)
        end

        -- PIR no motion → plan OFF
        if device.id == pir1.id
            and pir1.state == 'Off'
            and lamp1.state == 'On'
            and lamp1PirVar.value == 1
        then
            lamp1.switchOff().afterSec(OFF_DELAY)
            dz.log('PIR1 → Lamp1 OFF in ' .. OFF_DELAY .. ' sec', LOG_LEVEL)
        end

        -- Lamp actually turned off → reset PIR flag
        if device.id == lamp1.id
            and lamp1.state == 'Off'
            and lamp1PirVar.value == 1
        then
            lamp1PirVar.set(0)
            dz.log('Lamp1 OFF → PIR mode reset', LOG_LEVEL)
        end

        -- Manual ON → disable PIR control
        if device.id == lamp1.id
            and lamp1.state == 'On'
            and pir1.state == 'Off'
        then
            lamp1PirVar.set(0)
            dz.log('Lamp1 manually ON → PIR disabled', LOG_LEVEL)
        end

        ----------------------------------------------------------------
        -- PIR 2 / Lamp 2
        ----------------------------------------------------------------

        -- PIR detects motion → lamp ON or timer reset
        if device.id == pir2.id and pir2.state == 'On' then

            if lamp2.state == 'Off' then
                lamp2.switchOn()
                dz.log('PIR2 → Lamp2 ON', LOG_LEVEL)
            end

            lamp2PirVar.set(1)

            -- cancel pending OFF
            lamp2.switchOn().checkFirst()
            dz.log('PIR2 → OFF timer reset', LOG_LEVEL)
        end

        -- PIR no motion → plan OFF
        if device.id == pir2.id
            and pir2.state == 'Off'
            and lamp2.state == 'On'
            and lamp2PirVar.value == 1
        then
            lamp2.switchOff().afterSec(OFF_DELAY)
            dz.log('PIR2 → Lamp2 OFF in ' .. OFF_DELAY .. ' sec', LOG_LEVEL)
        end

        -- Lamp actually turned off → reset PIR flag
        if device.id == lamp2.id
            and lamp2.state == 'Off'
            and lamp2PirVar.value == 1
        then
            lamp2PirVar.set(0)
            dz.log('Lamp2 OFF → PIR mode reset', LOG_LEVEL)
        end

        -- Manual ON → disable PIR control
        if device.id == lamp2.id
            and lamp2.state == 'On'
            and pir2.state == 'Off'
        then
            lamp2PirVar.set(0)
            dz.log('Lamp2 manually ON → PIR disabled', LOG_LEVEL)
        end
    end
}

Re: Problem with the blockly script turning on the light

Posted: Monday 09 February 2026 21:45
by Maciek90
In this script, when I manually turn on the lamp and then the motion sensor is triggered and then turned off, after 30 seconds the lamps also turn off. The variables also change their value to 1.