Problem with the blockly script turning on the light

Moderator: leecollings

Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Problem with the blockly script turning on the light

Post by Maciek90 »

I have two lamps that I want to turn on with a motion sensor at specific times. I created a Blockly script as shown below. It turns the lamp on when the sensor detects motion, and turns it off after a set period of time after the motion sensor switches to off. It usually works well, but occasionally it won't turn off the lamp 30 seconds after the motion sensor switches to off, leaving it on continuously until I turn it off in Domoticz. Could someone explain why this is happening and help me improve the script?
Attachments
1.png
1.png (42.31 KiB) Viewed 250 times
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

Hi,
Maybe it is easier to use a DZvents script.
More flexibel and options

Maybe this example can help you

Code: Select all

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

return {
    on = {
        devices  = {450,451,452,453 },
    },

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

    execute = function(dz, device)

        local pirDevice1      = dz.devices(450)
        local pirDevice2      = dz.devices(451)
        local lamp1           = dz.devices(452)
        local lamp1           = dz.devices(453)


            if pirDevice1.state == "On" and lamp1.state == "On"
                then pirDevice1.switchOn().checkFirst()
                dz.log('Switch lamp1 ON', LOG_LEVEL)
            elseif pirDevice1.state == "Off" and lamp1.state == "On"
                then pirDevice1.switchOff().checkFirst().afterSec(30)
                dz.log('Switch lamp1 OFF', LOG_LEVEL)
            end
            
            if pirDevice2.state == "On" and lamp2.state == "On"
                then pirDevice2.switchOn().checkFirst()
                dz.log('Switch lamp2 ON', LOG_LEVEL)
            elseif pirDevice2.state == "Off" and lamp2.state == "On"
                then pirDevice2.switchOff().checkFirst().afterSec(30)
                dz.log('Switch lamp2 OFF', LOG_LEVEL)
            end
end
}
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

Thank you for your reply. I changed the IDX in the script to match my devices, but unfortunately it doesn't work. My lights don't turn on when the motion sensor is activated. DzVents isn't my strong suit, unfortunately.

Code: Select all

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

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

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

    execute = function(dz, device)

        local pirDevice1      = dz.devices(69)
        local pirDevice2      = dz.devices(1575)
        local lamp1           = dz.devices(68)
        local lamp1           = dz.devices(1574)


            if pirDevice1.state == "On" and lamp1.state == "On"
                then pirDevice1.switchOn().checkFirst()
                dz.log('Switch lamp1 ON', LOG_LEVEL)
            elseif pirDevice1.state == "Off" and lamp1.state == "On"
                then pirDevice1.switchOff().checkFirst().afterSec(30)
                dz.log('Switch lamp1 OFF', LOG_LEVEL)
            end
            
            if pirDevice2.state == "On" and lamp2.state == "On"
                then pirDevice2.switchOn().checkFirst()
                dz.log('Switch lamp2 ON', LOG_LEVEL)
            elseif pirDevice2.state == "Off" and lamp2.state == "On"
                then pirDevice2.switchOff().checkFirst().afterSec(30)
                dz.log('Switch lamp2 OFF', LOG_LEVEL)
            end
end
}
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

Hi,

Let add some log to see what is happening

Code: Select all

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

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

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

    execute = function(dz, device)

        -- Log which device triggered the script 
        dz.log('Script triggered by device: ' .. device.name .. ' (ID: ' .. device.id .. ')', LOG_LEVEL)

        -- Apparaten ophalen
        local pirDevice1 = dz.devices(69)
        local pirDevice2 = dz.devices(1575)
        local lamp1 = dz.devices(68)
        local lamp2 = dz.devices(1574)

        -- PIR1 and lamp1
        if pirDevice1.state == "On" and lamp1.state == "On" then
            pirDevice1.switchOn().checkFirst()
            dz.log('PIR1 triggered. Switching Lamp1 ON', LOG_LEVEL)
        elseif pirDevice1.state == "Off" and lamp1.state == "On" then
            pirDevice1.switchOff().checkFirst().afterSec(30)
            dz.log('PIR1 triggered. Switching Lamp1 OFF in 30 sec', LOG_LEVEL)
        end

        -- PIR2 andlamp2
        if pirDevice2.state == "On" and lamp2.state == "On" then
            pirDevice2.switchOn().checkFirst()
            dz.log('PIR2 triggered. Switching Lamp2 ON', LOG_LEVEL)
        elseif pirDevice2.state == "Off" and lamp2.state == "On" then
            pirDevice2.switchOff().checkFirst().afterSec(30)
            dz.log('PIR2 triggered. Switching Lamp2 OFF in 30 sec', LOG_LEVEL)
        end

    end
}
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

2026-01-04 16:52:38.875 dzVents: !Info: PIR : Script triggered by device: Drzwi wejściowe sensor (ID: 69)
2026-01-04 16:52:58.322 dzVents: !Info: PIR : Script triggered by device: Drzwi wejściowe sensor (ID: 69)
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

my mistake
I switchon the PIR

Code: Select all

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

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

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

    execute = function(dz, device)

        -- Log which device triggered the script 
        dz.log('Script triggered by device: ' .. device.name .. ' (ID: ' .. device.id .. ')', LOG_LEVEL)

        -- Apparaten ophalen
        local pirDevice1 = dz.devices(69)
        local pirDevice2 = dz.devices(1575)
        local lamp1 = dz.devices(68)
        local lamp2 = dz.devices(1574)

        -- PIR1 and lamp1
        if pirDevice1.state == "On" and lamp1.state == "On" then
            lamp1.switchOn().checkFirst()
            dz.log('PIR1 triggered. Switching Lamp1 ON', LOG_LEVEL)
        elseif pirDevice1.state == "Off" and lamp1.state == "On" then
            lamp1.switchOff().checkFirst().afterSec(30)
            dz.log('PIR1 triggered. Switching Lamp1 OFF in 30 sec', LOG_LEVEL)
        end

        -- PIR2 andlamp2
        if pirDevice2.state == "On" and lamp2.state == "On" then
            lamp2.switchOn().checkFirst()
            dz.log('PIR2 triggered. Switching Lamp2 ON', LOG_LEVEL)
        elseif pirDevice2.state == "Off" and lamp2.state == "On" then
            lamp2.switchOff().checkFirst().afterSec(30)
            dz.log('PIR2 triggered. Switching Lamp2 OFF in 30 sec', LOG_LEVEL)
        end

    end
}
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

Still not working

2026-01-04 20:34:26.691 dzVents: !Info: PIR : Script triggered by device: Drzwi wejściowe sensor (ID: 69)
2026-01-04 20:34:39.419 dzVents: !Info: PIR : Script triggered by device: Drzwi wejściowe sensor (ID: 69)
2026-01-04 20:34:49.283 dzVents: !Info: PIR : Script triggered by device: Dom światło sensor (ID: 1575)
2026-01-04 20:34:58.025 dzVents: !Info: PIR : Script triggered by device: Dom światło sensor (ID: 1575)
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

Sorry.. i was too quick

When the lamp = On switch On doesn't make sense
Lamp should be Off to switch On


Code: Select all

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

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

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

    execute = function(dz, device)

        -- Log which device triggered the script 
        dz.log('Script triggered by device: ' .. device.name .. ' (ID: ' .. device.id .. ')', LOG_LEVEL)

        -- Apparaten ophalen
        local pirDevice1 = dz.devices(69)
        local pirDevice2 = dz.devices(1575)
        local lamp1 = dz.devices(68)
        local lamp2 = dz.devices(1574)

        -- PIR1 and lamp1
        if pirDevice1.state == "On" and lamp1.state == "Off" then
            lamp1.switchOn()
            dz.log('PIR1 triggered. Switching Lamp1 ON', LOG_LEVEL)
        elseif pirDevice1.state == "Off" and lamp1.state == "On" then
            lamp1.switchOff().afterSec(30)
            dz.log('PIR1 triggered. Switching Lamp1 OFF in 30 sec', LOG_LEVEL)
        end

        -- PIR2 and lamp2
        if pirDevice2.state == "On" and lamp2.state == "Off" then
            lamp2.switchOn()
            dz.log('PIR2 triggered. Switching Lamp2 ON', LOG_LEVEL)
        elseif pirDevice2.state == "Off" and lamp2.state == "On" then
            lamp2.switchOff().afterSec(30)
            dz.log('PIR2 triggered. Switching Lamp2 OFF in 30 sec', LOG_LEVEL)
        end

    end
}
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

It seems to be working perfectly now. However, I'd like to expand this script a bit. I think I can handle most of the issues myself, but I'm not sure how to approach one particular issue. I'd like the lights to stay on after I turn them on in Domoticz with the motion sensor disabled until I turn them off again via Domoticz. Could you please help?
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

Can you try this.
Not sure if it will work

Basically we want to check if the lamp1 is on before PIR1

Code: Select all

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

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

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

    execute = function(dz, device)

        -- Log which device triggered the script 
        dz.log('Script triggered by device: ' .. device.name .. ' (ID: ' .. device.id .. ')', LOG_LEVEL)

        -- Apparaten ophalen
        local pirDevice1 = dz.devices(69)
        local pirDevice2 = dz.devices(1575)
        local lamp1 = dz.devices(68)
        local lamp2 = dz.devices(1574)
        local diff1 = lamp1.lastUpdate < pirDevice1.lastUpdate
        local diff2 = lamp2.lastUpdate < pirDevice2.lastUpdate


        -- PIR1 and lamp1
        if pirDevice1.state == "On" 
            and lamp1.state == "Off" 
             and diff1 then
            lamp1.switchOn()
            dz.log('PIR1 triggered. Switching Lamp1 ON', LOG_LEVEL)
        elseif pirDevice1.state == "Off" and lamp1.state == "On" then
            lamp1.switchOff().afterSec(30)
            dz.log('PIR1 triggered. Switching Lamp1 OFF in 30 sec', LOG_LEVEL)
        end

        -- PIR2 and lamp2
        if pirDevice2.state == "On" 
            and lamp2.state == "Off" 
            and diff2 then
            lamp2.switchOn()
            dz.log('PIR2 triggered. Switching Lamp2 ON', LOG_LEVEL)
        elseif pirDevice2.state == "Off" and lamp2.state == "On" then
            lamp2.switchOff().afterSec(30)
            dz.log('PIR2 triggered. Switching Lamp2 OFF in 30 sec', LOG_LEVEL)
        end

    end
}
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

Doesn't work, doesn't turn on the lights. Logs below.

2026-01-06 22:51:24.270 dzVents: !Info: PIR2 : Script triggered by device: Dom światło sensor (ID: 1575)
2026-01-06 22:51:25.249 dzVents: !Info: PIR2 : Script triggered by device: Drzwi wejściowe sensor (ID: 69)
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Problem with the blockly script turning on the light

Post by habahabahaba »

I think you need 2 different scripts and 1 user variable.

A user variable will be a flag - were your lights turned on from Dz by hands or not (0 or 1 value)

Create a dummy switch and put a script on it where:
a) if you turning it on - change the flag( user varible) to 1, turn on all lights you need
b) at off action - chenge the flag to 0, turn off all lights

And in the current working script just add the IF condition where you chek whether the flag is "1" or not
User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

You can try this

This wil use the last update of the device

Code: Select all

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

return {
    on = {
        devices = {68, 69, 1574, 1575}, -- Lamp and PIR devices
    },

    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)

        ----------------------------------------------------------------
        -- PIR 1 / Lamp 1 logic
        ----------------------------------------------------------------
        -- PIR triggers → turn lamp on
        if pir1.state == 'On' and lamp1.state == 'Off' then
            lamp1.switchOn().checkFirst()
            dz.log('PIR1 → Lamp1 ON', LOG_LEVEL)
        end

        -- Only turn off if PIR turned lamp on (lamp last updated before PIR)
        if pir1.state == 'Off' and lamp1.state == 'On' then
            if lamp1.lastUpdate < pir1.lastUpdate then
                lamp1.switchOff().afterSec(30)
                dz.log('PIR1 → Lamp1 OFF in 30 sec', LOG_LEVEL)
            else
                dz.log('Lamp1 manually turned on, not turning off', LOG_LEVEL)
            end
        end

        ----------------------------------------------------------------
        -- PIR 2 / Lamp 2 logic
        ----------------------------------------------------------------
        if pir2.state == 'On' and lamp2.state == 'Off' then
            lamp2.switchOn().checkFirst()
            dz.log('PIR2 → Lamp2 ON', LOG_LEVEL)
        end

        if pir2.state == 'Off' and lamp2.state == 'On' then
            if lamp2.lastUpdate < pir2.lastUpdate then
                lamp2.switchOff().afterSec(30)
                dz.log('PIR2 → Lamp2 OFF in 30 sec', LOG_LEVEL)
            else
                dz.log('Lamp2 manually turned on, not turning off', LOG_LEVEL)
            end
        end
    end
}

Or by using User variables which is better/more secure
Make 2 new User Variables in Domoticz Setup

Lamp1_PIR (Type: Boolean, false)
Lamp2_PIR (Type: Boolean, false)

Code: Select all

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

return {
    on = {
        devices = {68, 69, 1574, 1575}, -- Lamp and PIR devices
    },

    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)

        -- Get user variables for tracking PIR-controlled lamps
        local lamp1PirVar = dz.variables('Lamp1_PIR')
        local lamp2PirVar = dz.variables('Lamp2_PIR')

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

        -- PIR triggers → turn lamp on and mark as PIR-controlled
        if pir1.state == 'On' and lamp1.state == 'Off' then
            lamp1.switchOn().checkFirst()
            lamp1PirVar.set(true)
            dz.log('PIR1 → Lamp1 ON (PIR mode)', LOG_LEVEL)
        end

        -- PIR stops → only turn off if lamp was turned on by PIR
        if pir1.state == 'Off' and lamp1.state == 'On' and lamp1PirVar.value == true then
            lamp1.switchOff().afterSec(30)
            dz.log('PIR1 → Lamp1 OFF in 30 sec (PIR mode)', LOG_LEVEL)
        end

        -- Manual lamp activation → disable PIR mode
        if device.id == lamp1.id and lamp1.state == 'On' then
            lamp1PirVar.set(false)
            dz.log('Lamp1 manually turned on → PIR mode disabled', LOG_LEVEL)
        end

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

        if pir2.state == 'On' and lamp2.state == 'Off' then
            lamp2.switchOn().checkFirst()
            lamp2PirVar.set(true)
            dz.log('PIR2 → Lamp2 ON (PIR mode)', LOG_LEVEL)
        end

        if pir2.state == 'Off' and lamp2.state == 'On' and lamp2PirVar.value == true then
            lamp2.switchOff().afterSec(30)
            dz.log('PIR2 → Lamp2 OFF in 30 sec (PIR mode)', LOG_LEVEL)
        end

        if device.id == lamp2.id and lamp2.state == 'On' then
            lamp2PirVar.set(false)
            dz.log('Lamp2 manually turned on → PIR mode disabled', LOG_LEVEL)
        end
    end
}
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

I tried the variable script, and it doesn't work correctly. When the PIR detects motion and turns on the lamp, Domoticz thinks the lamp was manually turned on and leaves it on continuously. This can be seen in the log screenshot.

One more question: Is a Boolean variable the same as a string?
Attachments
1.png
1.png (26.64 KiB) Viewed 146 times
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Problem with the blockly script turning on the light

Post by habahabahaba »

1. Create a user variable and Name it LightHandMode , Type - integer, default value - 0
2. Create a virtual Dummy switch, name as you want
3. create a Dzvents script and put this code (check Idx of your devices) :
Spoiler: show

Code: Select all

local dummySwitchId = 101  -- Idx of virtual switch

return {

	on = {
		devices = {dummySwitchId},

	    },
	
    logging = {
	            level = domoticz.LOG_INFO,
	             marker = 'Hand mode light',
	           },
	
	execute = function(domoticz, device)
	    
	    local handSwitch = domoticz.devices(dummySwitchId)
		
		local SwitchLightIds = { 
            68, 1574 -- Ids of light switches for hand mode, just add or remove
        }
		
		if handSwitch.state == 'On' then
		    
		    for _, SwitchLightId in ipairs(SwitchLightIds) do
                domoticz.devices(SwitchLightId).switchOn().checkFirst()
                domoticz.log('Device '.. domoticz.devices(SwitchLightId).name .. ' is ON', domoticz.LOG_INFO)
            end
        
            domoticz.variables('LightHandMode').set('1')
            domoticz.log('User variable <LightHandMode> set to 1', domoticz.LOG_INFO)
		    
		else
		    
		    for _, SwitchLightId in ipairs(SwitchLightIds) do
                domoticz.devices(SwitchLightId).switchOff().checkFirst()
                domoticz.log('Device '..domoticz.devices(SwitchLightId).name .. 'is OFF', domoticz.LOG_INFO)
            end
        
            domoticz.variables('LightHandMode').set('0')
            domoticz.log('User variable <LightHandMode> set to 0', domoticz.LOG_INFO)

        end
		
	end
}
4. Edit your working script
Spoiler: show

Code: Select all

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

return {
    on = {
        devices  = { 69, 1575}, -- only PIR sensors, no switches
    },

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

    execute = function(dz, device)
        
        
        if dz.variables('LightHandMode').value = 0 then

            -- Log which device triggered the script 
            dz.log('Script triggered by device: ' .. device.name .. ' (ID: ' .. device.id .. ')', LOG_LEVEL)
    
            -- Apparaten ophalen
            local pirDevice1 = dz.devices(69)
            local pirDevice2 = dz.devices(1575)
            local lamp1 = dz.devices(68)
            local lamp2 = dz.devices(1574)
    
            -- PIR1 and lamp1
            if pirDevice1.state == "On" and lamp1.state == "Off" then
                lamp1.switchOn()
                dz.log('PIR1 triggered. Switching Lamp1 ON', LOG_LEVEL)
            elseif pirDevice1.state == "Off" and lamp1.state == "On" then
                lamp1.switchOff().afterSec(30)
                dz.log('PIR1 triggered. Switching Lamp1 OFF in 30 sec', LOG_LEVEL)
            end
    
            -- PIR2 and lamp2
            if pirDevice2.state == "On" and lamp2.state == "Off" then
                lamp2.switchOn()
                dz.log('PIR2 triggered. Switching Lamp2 ON', LOG_LEVEL)
            elseif pirDevice2.state == "Off" and lamp2.state == "On" then
                lamp2.switchOff().afterSec(30)
                dz.log('PIR2 triggered. Switching Lamp2 OFF in 30 sec', LOG_LEVEL)
            end
        else
        
            dz.log('Hand mode is ON, do nothing', LOG_LEVEL)
        
        end


end
}

User avatar
madpatrick
Posts: 758
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: Problem with the blockly script turning on the light

Post by madpatrick »

Hi,

This script should work. I've tested is shortly with some dumy devices

Code: Select all

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

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 logic
        ----------------------------------------------------------------

        -- PIR triggers → turn lamp on and mark as PIR-controlled
        if device.id == pir1.id and pir1.state == 'On' and lamp1.state == 'Off' and lamp1PirVar.value == 0 then
            lamp1.switchOn().checkFirst()
            lamp1PirVar.set(1)
            dz.log('PIR1 → Lamp1 ON (PIR mode)', LOG_LEVEL)
        end

        -- PIR stops → only turn off if lamp was turned on by PIR
        if device.id == pir1.id and pir1.state == 'Off' and lamp1.state == 'On' and lamp1PirVar.value == 1 then
            lamp1.switchOff().afterSec(30)
            lamp1PirVar.set(0)
            dz.log('PIR1 → Lamp1 OFF in 30 sec (PIR mode)', LOG_LEVEL)
        end

        -- Manual lamp activation → disable PIR mode
        if device.id == lamp1.id and lamp1.state == 'On' and pir1.state == 'Off' and lamp1PirVar.value == 0 then
            dz.log('Lamp1 manually turned on → PIR mode remains disabled', LOG_LEVEL)
        end

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

        -- PIR triggers → turn lamp on and mark as PIR-controlled
        if device.id == pir2.id and pir2.state == 'On' and lamp2.state == 'Off' and lamp2PirVar.value == 0 then
            lamp2.switchOn().checkFirst()
            lamp2PirVar.set(1)
            dz.log('PIR2 → Lamp2 ON (PIR mode)', LOG_LEVEL)
        end

        -- PIR stops → only turn off if lamp was turned on by PIR
        if device.id == pir2.id and pir2.state == 'Off' and lamp2.state == 'On' and lamp2PirVar.value == 1 then
            lamp2.switchOff().afterSec(30)
            lamp2PirVar.set(0)
            dz.log('PIR2 → Lamp2 OFF in 30 sec (PIR mode)', LOG_LEVEL)
        end

        -- Manual lamp activation → disable PIR mode
        if device.id == lamp2.id and lamp2.state == 'On' and pir2.state == 'Off' and lamp2PirVar.value == 0 then
            dz.log('Lamp2 manually turned on → PIR mode remains disabled', LOG_LEVEL)
        end
    end
}
You need to make a UserVariable

Name : Lamp1_PIR
Type : Integer
Value : 0

Name : Lamp2_PIR
Type : Integer
Value : 0
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
akamming
Posts: 422
Joined: Friday 17 August 2018 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by akamming »

Maciek90 wrote: Tuesday 13 January 2026 19:43 I tried the variable script, and it doesn't work correctly. When the PIR detects motion and turns on the lamp, Domoticz thinks the lamp was manually turned on and leaves it on continuously. This can be seen in the log screenshot.

One more question: Is a Boolean variable the same as a string?
Just a hint: ask ChatGPT these questions while referring to the dzvents socumentation. You’ll be surprised how well it can do dzvents scriptie. Sometime the scripts have bugs, But if you aso ChatGPT to solve them (just feed with the error or behaviour) it Will also correct them…
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

This script should work. I've tested is shortly with some dumy devices
I ran your script today and it's working fine so far. I'll try to test all the possibilities in the next few days and let you know if everything works as it should.
Just a hint: ask ChatGPT these questions while referring to the dzvents socumentation. You’ll be surprised how well it can do dzvents scriptie. Sometime the scripts have bugs, But if you aso ChatGPT to solve them (just feed with the error or behaviour) it Will also correct them…
Last year, I tried to write what turned out to be a fairly simple script using chatGPT, and unfortunately, I gave up after a few days. I managed to get the script working on the forum within a day :)
ChatGPT is constantly evolving, and perhaps dzvents is as well. Maybe I'll give it another try some other time.
Maciek90
Posts: 55
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Problem with the blockly script turning on the light

Post by Maciek90 »

Unfortunately, the script doesn't work perfectly. There's a similar problem to the Blockly script presented in the first post: sometimes, the script won't turn off the light even though 30 seconds have passed since the motion sensor signal went off. This happens very rarely, once every few days. Below are screenshots of the Domoticz logs and the light button logs. You can see that the light turned on at 8:38 PM, and at 9:13 PM, I manually turned it off because it didn't automatically turn off after 30 seconds. There's also another problem with this script. If the light doesn't turn off after 30 seconds, the value of the variable doesn't change from 1 to 0. Therefore, when I manually turn off the light, I also have to manually change the value of the variable to 0. Otherwise, the motion sensor signal doesn't turn on the light.

Code: Select all

2026-01-28 20:30:58.924 Status: Set UserVariable Lamp2_PIR = 1
2026-01-28 20:31:07.483 Status: Set UserVariable Lamp2_PIR = 0
2026-01-28 20:35:11.881 Status: Set UserVariable Lamp1_PIR = 1
2026-01-28 20:35:20.750 Status: Set UserVariable Lamp1_PIR = 0
2026-01-28 20:38:11.479 Status: Set UserVariable Lamp2_PIR = 1
2026-01-28 20:45:48.930 Status: Set UserVariable Lamp1_PIR = 1
2026-01-28 20:45:57.460 Status: Set UserVariable Lamp1_PIR = 0 
Attachments
2.jpg
2.jpg (29.71 KiB) Viewed 65 times
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Problem with the blockly script turning on the light

Post by habahabahaba »

Do you need to turn on the lights individually or together?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest