Page 1 of 1

Cancel queued customEvent

Posted: Monday 08 June 2020 19:38
by ronaldbro
Hi (waaren I suppose ;) ),

When I fire a delayed customEvent I use:
dz.emitEvent('myEvent').afterSec(60)

This works great and after 60 seconds I receive the event.

But is there any way to cancel the queued event? With some wishful thinking I tried:
dz.emitEvent('myEvent').cancelQueuedCommands()

But this doesn't work unfortunately. Is there any way to cancel a queued customEvent?

Background info:
I bought a door sensor for my freezer. After my kids left the freezer door open a few times and I had to through away lots of food I thought it would be a good investment to add a door sensor.
The idea was to schedule a customEvent when the door opens and cancel it when the door closes before the time expires.

Of course I can check if the door is closed then the event fires, but it could be that it is opened a second time and in the mean time the door was closed. I can also think of other solutions but cancelling a scheduled event would be super simple.

Thanks.

Re: Cancel queued customEvent

Posted: Monday 08 June 2020 22:12
by waaren
ronaldbro wrote: Monday 08 June 2020 19:38 Is there any way to cancel a queued customEvent?
:D besides stop / start domoticz ? No
Of course I can check if the door is closed when the event fires, but it could be that it is opened a second time and in the mean time the door was closed.
I don't see the problem. You want to check if the door is closed and that's what you do.

But if you want to check if there was only one set of open/close within the time frame, you could use the concept @rrozema and I discussed a couple of days ago on this forum

A solution for that would look like.

Code: Select all

return {
    on = {
        devices = {
            "myTest"
        },
        customEvents = {
            "myTestEvent*"
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },

    execute = function(dz, item)

        if item.isDevice then
            if item.active then
                local name = 'myTestEvent_' .. tostring(item.idx) .. '_' .. item.lastUpdate.raw
                dz.emitEvent(name, { idx = item.idx,  thisUpdate = item.lastUpdate.current } ).afterSec(10)
            end
            return
        end

        local myDevice = dz.devices('myTest')

        if not(myDevice.active) then
            dz.log('No action needed. Kids closed the door', dz.LOG_DEBUG )
            return
        end

        local lastUpdate = myDevice.lastUpdate

        local Time = require('Time')
        local thisUpdate = Time(os.date("%Y-%m-%d %H:%M:%S", os.time(item.json.thisUpdate)))

        dz.log('lastUpdate: ' .. lastUpdate.raw, dz.LOG_DEBUG )
        dz.log('thisUpdate: ' .. thisUpdate.raw, dz.LOG_DEBUG )

        dz.log('Times Equal? ' ..  tostring(lastUpdate.compare(thisUpdate).compare == 0 ) ,dz.LOG_DEBUG)

        if lastUpdate.compare(thisUpdate).compare == 0  then
            dz.log('Kids left the door open' ,dz.LOG_DEBUG)
        else
            dz.log('Kids opened the door again after I closed it for them..' ,dz.LOG_DEBUG)
        end
    end
}

Re: Cancel queued customEvent

Posted: Monday 08 June 2020 22:23
by ronaldbro
Thanks waaren,

Already was afraid it wasn't possible. Didn't see that post, could have used that solution too.
I solved it now using a 'squenceNumber' in the data to check if the customEvent belongs to the last event of the door sensor. It works great now, but would have been less complex if it would be possible to cancel the queued event.

Code: Select all

 local idx_vriezerdeur = idx_bijkeuken_vriezerdeur

local alarmAfterSec     = 120

local VRIEZERDEUR_ALARM_EVENT = 'VRIEZERDEUR_ALARM_EVENT'

return {
	on = {
		devices = {
            idx_vriezerdeur
		},

		customEvents = {
			VRIEZERDEUR_ALARM_EVENT
		}
	},

	data = {
		sequenceNumber = { initial = 0 },
		alarmCount = { initial = 0 }
	},

	logging = {
--        level = domoticz.LOG_DEBUG,
--        level = domoticz.LOG_INFO,
        level = domoticz.LOG_ERROR,
        marker = "Bijkeuken - Vriezerdeur"
    },

	execute = function(dz, trigger, info)
        if trigger.isDevice then
            dz.data.sequenceNumber = dz.data.sequenceNumber + 1
            if dz.data.sequenceNumber > 9999 then dz.data.sequenceNumber = 0 end
            dz.log('New sequenceNumber = ' .. dz.data.sequenceNumber)
	        if trigger.idx == idx_vriezerdeur and trigger.active then
	            dz.data.alarmCount = 0
	            dz.emitEvent(VRIEZERDEUR_ALARM_EVENT, dz.data.sequenceNumber).afterSec(alarmAfterSec)
	        end
        elseif trigger.isCustomEvent then
            dz.log('SequenceNumber of event = ' .. trigger.data .. ', last sequence = ' .. dz.data.sequenceNumber)
            if trigger.data == tostring(dz.data.sequenceNumber) then
                if dz.data.alarmCount < 10 then
	                dz.data.alarmCount = dz.data.alarmCount + 1
	                dz.emitEvent(VRIEZERDEUR_ALARM_EVENT, trigger.data).afterSec(alarmAfterSec * dz.data.alarmCount)

                    dz.notify(nil, 'De vriezerdeur staat nog open.', priority, nil, nil, dz.NSS_TELEGRAM)
                    dz.notify('VriezerOpen.mp3/' .. '40',nil, dz.PRIORITY_NORMAL,nil,nil, 'sonos')
                else
                    dz.notify(nil, 'De vriezerdeur staat nog open, dit is het laatste bericht.', priority, nil, nil, dz.NSS_TELEGRAM)
	            end
            else
                dz.log('Old event, ignoring...')
            end
        end
	end
}

Re: Cancel queued customEvent

Posted: Monday 08 June 2020 23:01
by waaren
ronaldbro wrote: Monday 08 June 2020 22:23 I solved it now using a 'squenceNumber' in the data to check if the customEvent belongs to the last event of the door sensor.
Creative ! Will try to remember this method. Thx !

Re: Cancel queued customEvent

Posted: Friday 23 July 2021 4:38
by blueberryPie
I second the request for dz.emitEvent('myEvent').cancelQueuedCommands()

Any of the workarounds are just way more complex and complex = mistakes. Simple is good. Thanks for emitEvent, now if it can be improved that would be even better.

Re: Cancel queued customEvent

Posted: Tuesday 10 August 2021 14:18
by Piacco
I use a script to turn my PC on or off with a fibaro push button.
With a single push a want to turn the PC on or off and with the double push i will cancel the script, but i don't understand wy the cancelQueuedCommands is not working :|

Code: Select all

 if item.isDevice and device.name == 'Reset_PC' then
         dz.devices('PC-2').cancelQueuedCommands()
         dz.notify("Domoticz",  "Uitschakelen Geannuleerd", dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , "telegram")

Code: Select all

local myCustomEvent = 'delayedSwitchOffPC'

return 
{
    on = 
    {
        devices = 
        { 
            '$PC_Boven',
            'Reset_PC',
        },
	    customEvents =
        {
            myCustomEvent,
        },	
    },


    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = "PC Boven Aan/Uit"
    },

    execute = function(dz, item, device)
       -- ****************************** Your settings below this line ***************************************************
       local VerbruikPC = dz.devices('Verbruik PC')
	   dz.log("Actueel verbruik PC = " ..VerbruikPC.actualWatt)  
	   -- ***********************************************************************************************************
      

      if item.isDevice and device.name == 'Reset_PC' then
         dz.notify("Domoticz",  "Geannuleerd", dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , "telegram")
      
      elseif item.isDevice and dz.devices('PC-2').state == 'Off' then
                    dz.devices('PC-2').switchOn()
                    dz.notify("Domoticz",  "PC Boven is aan", dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , "telegram")
		         
	  elseif item.isDevice and dz.devices('PC-2').state == 'On' then           
    		        dz.notify("Domoticz",  "Controle of PC uitgaat !!!", dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , "telegram")
    		        dz.emitEvent(myCustomEvent,'ControleVerbruikPC').afterSec(300)
        
      elseif item.data == 'ControleVerbruikPC' and VerbruikPC.actualWatt < 55  then
                    dz.devices('PC-2').switchOff()
		            dz.notify("Domoticz",  "PC is uitgeschakeld!!!", dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , "telegram")
	  elseif item.data == 'ControleVerbruikPC' and VerbruikPC.actualWatt > 56  then
	                dz.notify("Domoticz",  "PC gaat niet uit, stroomverbruik is te hoog  !!!", dz.PRIORITY_NORMAL,dz.SOUND_DEFAULT, "" , "telegram") 
	  end

    end
}