Flashing lights, howto?  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Flashing lights, howto?

Post by rgroothuis »

I've the following script that is triggered when the doorbell is pressed.
I want the lights to go on and off 5 times but I cannot get this working.
On top of this the current status of the lamp needs to be saved so that I can but the light back on or off again after the flashing is completed.
This is my script, what am I doing wrong?

Code: Select all

return {
 	logging = {
 		level = domoticz.LOG_DEBUG,
 		marker = 'ControlDeurBel'
 	},
	on = {
		devices = {
			'Deurbel Input 1'
		}
	},
	execute = function(domoticz, device)

        -- If the Doorbell is pressed, take action and ring the bell. 
		if (device.name == 'Deurbel Input 1') and (device.state == 'On') then
		    local deurBelOut = domoticz.devices('Deurbel Output 2')
        
            -- Log information to the debug window
            domoticz.log('Doorbell was pressed, lets ring the bell!', domoticz.LOG_INFO)

            -- Send a message to Telegram that the doorbell was pressed.
            local DateTimePressed = os.date("%d/%m/%Y %X")
        	domoticz.notify("HoekHuis Deurbel", "Doorbell was pressed at " .. DateTimePressed .. ".", domoticz.PRIORITY_HIGH)

            -- Enable the doorbell, smart implant output.
            -- Smart implant is configured to switch off the output automatically.
        	deurBelOut.switchOn()
        	
        	-- Enable the gang door light
		    local VoorDeurLight = domoticz.devices('Lamp Hal Voordeur')
		    domoticz.log('Switch on the gang front door lamp.', domoticz.LOG_DEBUG)
        	VoorDeurLight.switchOn()
        	VoorDeurLight.switchOff().afterSec(10)

        	-- Switch On and Off the Kitchen light
		    local KeukenLamp = domoticz.devices('Keuken Lamp')
		  
		    KeukenLamp.cancelQueuedCommands()
		  
		    -- Save the status of the keuken Lamp to put it back to this status later
		    local KeukenLampStatus = KeukenLamp.state 
		    domoticz.log('Keuken lamp status: ' .. KeukenLampStatus .. '.', domoticz.LOG_DEBUG)
		    -- Flash the kitchen lamp a couple of times
		    KeukenLamp.switchOn().forSec(1).repeatAfterSec(1, 5)

		    domoticz.log('Keuken lamp was flashed a number of times.', domoticz.LOG_DEBUG)

            if (KeukenLampStatus == 'On') then
                domoticz.log('Switch keuken lamp on again.', domoticz.LOG_DEBUG)
                KeukenLamp.switchOn().afterMin(1)
            else
                domoticz.log('Switch keuken lamp off again.', domoticz.LOG_DEBUG)
                KeukenLamp.switchOff().afterMin(1)
            end
        end
	
	end
}
The log that is generated is as follow:

Code: Select all

2020-12-19 15:49:22.484 Status: dzVents: Info: ControlDeurBel: ------ Start internal script: ControlDeurBel: Device: "Deurbel Input 1 (HoekHuisZwave)", Index: 275
2020-12-19 15:49:22.484 Status: dzVents: Debug: ControlDeurBel: ============================================================================================
2020-12-19 15:49:22.484 Status: dzVents: Debug: ControlDeurBel: Device Deurbel Input 1 was changed. Status is: On.
2020-12-19 15:49:22.485 Status: dzVents: Debug: ControlDeurBel: Processing device-adapter for Deurbel Output 2: Switch device adapter
2020-12-19 15:49:22.485 Status: dzVents: Info: ControlDeurBel: Doorbell was pressed, lets ring the bell!
2020-12-19 15:49:22.485 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Processing device-adapter for Lamp Hal Voordeur: Switch device adapter
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Switch on the gang front door lamp.
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: Off
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: Off AFTER 10 SECONDS
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Processing device-adapter for Huiskamer Lamp: Switch device adapter
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Keuken lamp status: On.
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On
2020-12-19 15:49:22.486 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On FOR 1 SECONDS
2020-12-19 15:49:22.487 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On FOR 1 SECONDS REPEAT 6 INTERVAL 1 SECONDS
2020-12-19 15:49:22.487 Status: dzVents: Debug: ControlDeurBel: Keuken lamp was flashed a number of times.
2020-12-19 15:49:22.487 Status: dzVents: Debug: ControlDeurBel: Switch keuken lamp on again.
2020-12-19 15:49:22.487 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On
2020-12-19 15:49:22.487 Status: dzVents: Debug: ControlDeurBel: Constructed timed-command: On AFTER 60 SECONDS
2020-12-19 15:49:22.487 Status: dzVents: Debug: ControlDeurBel: ============================================================================================
2020-12-19 15:49:22.487 Status: dzVents: Info: ControlDeurBel: ------ Finished ControlDeurBel
2020-12-19 15:49:22.768 Status: dzVents: Info: ControlDeurBel: ------ Start internal script: ControlDeurBel: Device: "Deurbel Input 1 (HoekHuisZwave)", Index: 275
2020-12-19 15:49:22.768 Status: dzVents: Debug: ControlDeurBel: ============================================================================================
2020-12-19 15:49:22.768 Status: dzVents: Debug: ControlDeurBel: Device Deurbel Input 1 was changed. Status is: Off.
2020-12-19 15:49:22.768 Status: dzVents: Debug: ControlDeurBel: ============================================================================================
2020-12-19 15:49:22.769 Status: dzVents: Info: ControlDeurBel: ------ Finished ControlDeurBel
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Flashing lights, howto?  [Solved]

Post by erem »

this works for me

Code: Select all

return {
	on = {
		devices = {
			'DoorBell'
		}
	},
	execute = function(domoticz, device)
	    if (domoticz.devices('DoorBell').state == 'On' and domoticz.devices('SomeoneHome').state == 'On')
	    then
	        if domoticz.devices('Staande driehoek lamp').state == 'Off' 
	        then
    	        domoticz.devices('Staande driehoek lamp').switchOn().forSec(1).repeatAfterSec(1, 3)  -- repeat=(aftersec, nroftimes)
    	        domoticz.log('Device ' .. device.name .. ' was engaged while lamp was off', domoticz.LOG_INFO)
		    else
		        domoticz.devices('Staande driehoek lamp').switchOff().forSec(1).repeatAfterSec(1, 3) -- repeat=(aftersec, nroftimes)
		        domoticz.log('Device ' .. device.name .. ' was engaged while lamp was on', domoticz.LOG_INFO)
		    end
		    --domoticz.log('Device ' .. device.name .. ' was engaged', domoticz.LOG_INFO)
		end
	    if domoticz.devices('DoorBell').state == 'On'
	    then
		    domoticz.devices('DoorBell').switchOff().silent()
		end
	end
}

Regards,

Rob
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Flashing lights, howto?

Post by waaren »

rgroothuis wrote: Saturday 19 December 2020 15:54 This is my script, what am I doing wrong?
If you use forSec() the before state is significant

Can you try this and if not OK yet, please describe what you expect and what is not working like expected.

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Deurbel Input 1'
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'ControlDeurBel',
     },

    execute = function(dz, item)
        local deurBelOut = dz.devices('Deurbel Output 2')
        local VoorDeurLight = dz.devices('Lamp Hal Voordeur')
        local KeukenLamp = dz.devices('Keuken Lamp')

        -- If the Doorbell is pressed, take action and ring the bell.
        if item.name == 'Deurbel Input 1' and item.active then

            -- Log information to the debug window
            dz.log('Doorbell was pressed, lets ring the bell!', dz.LOG_INFO)

            -- Send a message to Telegram that the doorbell was pressed.
            local DateTimePressed = os.date("%d/%m/%Y %X")
            dz.notify("HoekHuis Deurbel", "Doorbell was pressed at " .. DateTimePressed .. ".", dz.PRIORITY_HIGH)

            -- Enable the doorbell, smart implant output.
            -- Smart implant is configured to switch off the output automatically.
            deurBelOut.switchOn()

            -- Enable the gang door light
            dz.log('Switch on the gang front door lamp.', dz.LOG_DEBUG)
            VoorDeurLight.switchOn()
            VoorDeurLight.switchOff().afterSec(10)

          -- Save the status of the keuken Lamp to put it back to this status later
            local KeukenLampStatus = KeukenLamp.state
            dz.log('Keuken lamp status: ' .. KeukenLampStatus .. '.', dz.LOG_DEBUG)

            -- Flash the kitchen lamp a couple of times
            if KeukenLamp.active then
                KeukenLamp.switchOff().forSec(1).repeatAfterSec(1, 5)
            else
                KeukenLamp.switchOn().forSec(1).repeatAfterSec(1, 5)
            end

            dz.log('Keuken lamp was flashed a number of times.', dz.LOG_DEBUG)

            if (KeukenLampStatus == 'On') then
                dz.log('Switch keuken lamp on again.', dz.LOG_DEBUG)
                KeukenLamp.switchOn().afterMin(1)
            else
                dz.log('Switch keuken lamp off again.', dz.LOG_DEBUG)
                KeukenLamp.switchOff().afterMin(1)
            end
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Flashing lights, howto?

Post by rgroothuis »

erem wrote: Saturday 19 December 2020 16:36 this works for me

Code: Select all

return {
	on = {
		devices = {
			'DoorBell'
		}
	},
	execute = function(domoticz, device)
	    if (domoticz.devices('DoorBell').state == 'On' and domoticz.devices('SomeoneHome').state == 'On')
	    then
	        if domoticz.devices('Staande driehoek lamp').state == 'Off' 
	        then
    	        domoticz.devices('Staande driehoek lamp').switchOn().forSec(1).repeatAfterSec(1, 3)  -- repeat=(aftersec, nroftimes)
    	        domoticz.log('Device ' .. device.name .. ' was engaged while lamp was off', domoticz.LOG_INFO)
		    else
		        domoticz.devices('Staande driehoek lamp').switchOff().forSec(1).repeatAfterSec(1, 3) -- repeat=(aftersec, nroftimes)
		        domoticz.log('Device ' .. device.name .. ' was engaged while lamp was on', domoticz.LOG_INFO)
		    end
		    --domoticz.log('Device ' .. device.name .. ' was engaged', domoticz.LOG_INFO)
		end
	    if domoticz.devices('DoorBell').state == 'On'
	    then
		    domoticz.devices('DoorBell').switchOff().silent()
		end
	end
}

Excellent, this trick worked for me. Thanks for the support.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest