Page 1 of 1

Re: Problem with checkFirst() ?

Posted: Tuesday 28 November 2023 8:05
by waltervl
The problem is that the script does not know if the light is switched on by the script or by you manually. So it will just switch off if lux is not < 38.

So in your first if statement add a boolean true/false for script switched on and then check in the else if for the boolean.

Re: Problem with checkFirst() ?

Posted: Tuesday 28 November 2023 9:51
by waltervl
CheckFirst() will only check if it is already in the desired state. If so it will not send the switch command. This will prevent switch triggers for other scripts using that device.

I added the switchedonbyscript boolean in your script. I hope it works....
Edit: added switchedonbyscript = false in switch off section.

Code: Select all

local 
return  {
            on          =   { devices           =  {['PIR chodba'] = {'between 23:00 and 06:00'}}   -- <<< --- set activity time 
                            },
            data = {  switchedonbyscript  = { initial = false }
                           },
            
            logging     =   {   
                                level           =   domoticz.LOG_DEBUG,
                                marker          =   "Pohyb v chodbě" 
                            },
                    
    execute = function(dz, item)
    
        local function logWrite(str,level)
            dz.log(str,level or dz.LOG_DEBUG)
        end

       -- local Lux = dz.devices("Motion Sensor LUX").lux
                local LuxDevice = dz.devices(26)                                -- <<<--- replace xxx by IDX of your Lux device
                local Lux = LuxDevice.lux
        -- local Lights = dz.devices("WLED - Color & Brightness")
                local Lights = dz.devices(172)                                   -- <<<--- replace xxx by IDX dummy switch
                
                
                            
        logWrite(LuxDevice.name .. " ==>> Lux:   " .. Lux )
        logWrite(Lights.name ..    " ==>> State: " .. Lights.state)
        logWrite(item.name ..      " ==>> State: " .. item.state)
        
        
       
        if Lux < 38 and item.state == "On" and Lights.state == "Off" then       -- <<< --- set value LUX for switching
            Lights.cancelQueuedCommands()
            switchedonbyscript = true 							 --  Switched on by script
            Lights.switchOn()									-- checkfirst not needed as it is Off
            Lights.dimTo(10)                                                    -- <<< --- set value dummy switch "LEVEL"
        elseif item.state == "Off" and  switchedonbyscript then    -- only switch off when switched on by script
            Lights.switchOff().checkFirst().afterSec(5)					-- If light already off do nothing
            switchedonbyscript = false
        end
    end
}

Re: Problem with checkFirst() ?

Posted: Wednesday 29 November 2023 20:08
by waltervl
Mayki wrote: Tuesday 28 November 2023 18:11 So I tried it and unfortunately this doesn't work. The LED strip lights up after a signal from the PIR sensor, but it doesn't go off.
Is there any option (command) to ensure that the script checks if the dimmer is in ON state before executing the command and does not start? If it is in the OFF state, everything will run normally.
That check you described is the checkFirst() function. Check first before doing it.

Also it could be that in my script the check first and aftersec should be switched in order.