Page 1 of 1

Device Snooze [Solved]

Posted: Thursday 17 August 2017 6:40
by Wob76
Hi,

Still fairly new to Domoticz and looking for some direction, I have managed to use dzVents to automate a few things with my AC, now I want to add a snooze function, before I go and look at a long script to do this, is there an easier option I am missing?

I have a wireless switch that is linked to Domoticz, it has multiple press options (Click, Double Click, Long Press, etc).

At first I was thinking of just using a scene to say turn the AC on then off after X min, but that seems a little limited.

Then I considered looking at some time function within dventz, say setting .PowerOn.forMin(30) but I read a post that said the timer can not be cancelled once it is issued. I would like to be able to add time or disable the timer.

So now I am thinking I could have the Switch record a time when Clicked, then turn off when it has been 30 min, and the same for a double click, but make it 60 min, and if I long press have the timer cancel.

It would be nice to have the script just add another 30min to the existing time for each press.

I am thinking of using my RGB light to indicate the timer is on, and use a different colour for the 30min\60min timer, or maybe just flash it to indicate the timer has been set. My partner doesn't want to open the app. But I would like a counter, so linking it back to a dummy counter would be nice.

Looking forward to thoughts from the experts ;)

Thanks,
Wob

Re: Device Snooze

Posted: Sunday 20 August 2017 11:02
by BakSeeDaa
Wob76 wrote: Thursday 17 August 2017 6:40 ...
Looking forward to thoughts ...
...
Hi Wob

What if you use 2 script level persistent variables in your script.

Code: Select all

singleClick = { history = true, maxItems = 10 }

Code: Select all

doubleClick = { history = true, maxItems = 10 }
Then, Invoke your script every minute and also each time you click one of your buttons. Every time you click a button you add a new data entry. Then you do some arithmetic to calculate the remaining minutes (without storing the result). Use the timestamp of the first data entry and accumulate the time added by all clicks. Then subtract the current time. If the remaining minutes is <= 0, or if you press the cancel button, do a reset( ) on the data. You can't reset() your data until all accumulated time has been used. Now when you have an accumulated time, turn the AC on or OFF if necessary.

Works in theory, at least in my brain. :D

Re: Device Snooze

Posted: Monday 21 August 2017 8:50
by Wob76
Thanks for the reply, I'll give your suggestion a try when I get a chance, and If can wrap my head around it :)

Re: Device Snooze

Posted: Tuesday 27 February 2018 2:57
by Wob76
Finally got around to revisiting this, I have since started using Dashticz and wanted to include a countdown timer to show, Only just started testing my idea, but it seems to work.

I created 2 devices, a selector switch, using the Label Add 15, Add 30, etc, and a text device to use as a countdown timer.

Pushing one of the selector values will add that value to the timer and turn on the "device" if its off, off will reset it to 0, but leave device on.

Script activates both with the selector switch and every minute.

If timer triggered it will take 1 from the countdown timer. This does result in some rounding of the timer as it trigger on the minute, but accurate enough for my needs.

I will look at just linking the push button I have to the above selector values, this enables a little more flexibility to enable both a physical button and some control in the app.

Still testing so I am sure I will modify some more, but thought I would post for anyone else looking for something similar.

Code: Select all

-- Switches
local TIMER_SWITCH = "AC Timer Switch"
local TIMER = "AC Timer"
local AC_SWITCH = "Test Switch" -- AC Power

local LOGGING = true

return {
	on = {
        timer = { "every 1 minutes", },
		devices = { 
			TIMER_SWITCH
		}
	},
	execute = function(domoticz, item)
        local timer_out = domoticz.devices(TIMER)
        local timer = timer_out.text:gsub(' Mins','')
        timer = tonumber(timer)
        local ac_power = domoticz.devices(AC_SWITCH)
        
        if (item.isDevice) then
            if (item.state ~= "Off") then
                local addtime = item.state:gsub('Add ','')
                domoticz.log('Adding time: '..addtime, domoticz.LOG_ERROR)
                timer = timer + addtime
                timer_out.updateText(timer.." Mins")
                ac_power.switchOn().checkFirst()
            else
                domoticz.log('Turning off timer', domoticz.LOG_ERROR)
                timer = 0
                timer_out.updateText(timer.." Mins")
            end
        end
        
        if (item.isTimer and timer > 0) then
            timer = timer - 1
            timer_out.updateText(timer.." Mins")
            if (timer == 0) then 
                domoticz.log('Turning the AC Off', domoticz.LOG_ERROR)
                ac_power.switchOff().checkFirst()
                domoticz.devices(TIMER_SWITCH).switchOff().checkFirst()
            end
        end
	end
}

Re: Device Snooze

Posted: Tuesday 27 February 2018 6:14
by Wob76
Some slight changes,

I should note it was written for 2.41 of dzVents.

Needed to shrink my buttons so reduced from "Add 15" to "+15".
Wanted to have Mins shown Off rather than 0 Mins.
I made the Selector switch silently return to Off, so it actions like a multi toggle switch.

A couple of test runs, happy with how it functions, and code is pretty simple, so hopefully shouldn't need much tweaking.

Scripts has 3 devices,
TIMER_SWITCH = Selector = For Adding Time
TIMER = Text Device = For Storing\Showing Time
AC_SWITCH = Controlled Device = Switch that is actioned.

They look like this in my Mobile Dashitcz Page
Capture.JPG
Capture.JPG (12.87 KiB) Viewed 689 times

Code: Select all

-- Switches
local TIMER_SWITCH = "AC Timer Switch"
local TIMER = "AC Timer"
local AC_SWITCH = "AC Power"

local LOGGING = true

return {
	on = {
        timer = { "every 1 minutes", },
		devices = { 
			TIMER_SWITCH
		}
	},
	execute = function(domoticz, item)
        local timer_out = domoticz.devices(TIMER)
        local timer = timer_out.text:gsub(" Mins","")
        if (timer ~= "Off") then
            timer = tonumber(timer)
        else 
            timer = 0
        end
        local ac_power = domoticz.devices(AC_SWITCH)
        
        if (item.isDevice) then
            if (item.state ~= "Off") then
                local addtime = item.state:gsub("+","")
                domoticz.log('Adding time: '..addtime, domoticz.LOG_FORCE)
                timer = timer + addtime
                timer_out.updateText(timer.." Mins")
                ac_power.switchOn().checkFirst()
                item.switchOff().silent()
            else
                domoticz.log('Turning off timer', domoticz.LOG_FORCE)
                timer = 0
                timer_out.updateText("Off")
            end
        end
        
        if (item.isTimer and timer > 0) then
            timer = timer - 1
            timer_out.updateText(timer.." Mins")
            if (timer == 0) then 
                domoticz.log('Turning the AC Off', domoticz.LOG_FORCE)
                ac_power.switchOff().checkFirst()
                domoticz.devices(TIMER_SWITCH).switchOff().checkFirst()
                timer_out.updateText("Off")
            end
        end
	end
}