Re: Problem with the blockly script turning on the light
Posted: Thursday 05 February 2026 17:07
Individually
Open source Home Automation System
https://forum.domoticz.com/
I thought about that tooSounds it is not an script issue.
I tried something like this in blockly but it didn't helpYou can give extra/second switch off command but that makes no sense
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
}