Help with dummy selector switch

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

Moderator: leecollings

Post Reply
User avatar
Phantom
Posts: 87
Joined: Saturday 31 December 2016 14:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11652
Location: The Netherlands
Contact:

Help with dummy selector switch

Post by Phantom »

Hey, i made this for a dummy switch i have. When pressed it will trigger this script and turns on my airco for 1 hour and then turns it off again:

Code: Select all

return {
    active = true,
    on = {
        devices = {
            'Airco 20C voor 1u'
        }
    },
    execute = function(domoticz, roomSwitch)
        if (roomSwitch.state == 'On') then
            domoticz.devices('Airco nachtstand').switchOn()
            domoticz.devices('Airco uit').switchOn().afterSec(3600)
            domoticz.notify('Airco staat nu voor 1u aan')
        end
    end
}
But i want to use it for a selector with 3 options.
Like 1 hour, 2 hours and 3 hours options.

How can i achieve this?
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Help with dummy selector switch

Post by BakSeeDaa »

Phantom wrote: Sunday 01 October 2017 16:08 ...But i want to use it for a selector with 3 options.
Like 1 hour, 2 hours and 3 hours options.

How can i achieve this?
I Haven't tried it out but something similar to this maybe:

Code: Select all

return {
    active = true,
    on = {
        devices = {
            'MY SELECTOR SWITCH'
        }
    },
    execute = function(domoticz, selectorSwitch)
        if (selectorSwitch.levelName == 'XXX') then
            domoticz.notify('This rocks!',
                            'Turns out that it is getting warm here',
                            domoticz.PRIORITY_LOW)
        elseif (selectorSwitch.levelName == 'YYY') then
            domoticz.notify('This su**s!',
                            'Turns out that it is getting cold here',
                            domoticz.PRIORITY_LOW)
        end
    end
}
The selector switch is documented here.
User avatar
Phantom
Posts: 87
Joined: Saturday 31 December 2016 14:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11652
Location: The Netherlands
Contact:

Re: Help with dummy selector switch

Post by Phantom »

Thanks, i'll go play around with that example.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Help with dummy selector switch

Post by emme »

mh... I have a similar issue.... that I did not really fix, but I used a workaround

Use your dummy device as a PushOn button instead of a switch
Use a Variable with a number value of 1, 2 or 3 hours
any push of the dummy button will move ahead and back to 0 once arrived at 3

pushOn button name: pushButton
variable name: pushTime - NUMBER
heater device name: heater
heater Off device name: heatOff

the script would be something like:

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'pushButton'
		},
	},
	execute = function(dz, push)
		local varName = 'pushTime'
		local varActVal = dz.variables(varName).value
		local varObj = dz.variables(varName)
		local honObj = dz.devices('heater')
		local hofObj = dz.devices('heatOff')
		
		-- This will set the variable as a timer... values are hours
		if varActVal >= 0 and varActVal <= 3 then
			varActVal = varAcVal + 1
		else
			varActVal = 0
		end

		-- Let's Update the variable
		-- note that the local varActVal has now the new timer value
		varObj.set(varActVal)

		-- Now let's operate the heater based on the variable value
		-- This could be done in the same if cycle above, but here is much clear
		if varActVal >= 0 and varActVal <= 3 then
			honObj.switchOn()
			hofObj.switchOn().afterMins(varActVal*60)
		else
			hofObj.switchOn()
		end		
				
	end
}
the only issue is.... I'm unclear and unsure, what happens if you queue a list of .afterMins commands... by my understanding there is no way to remove them from the domoticz scheduler, so it could be that the device will be switched off at the wrong time:
if you click once it will go for 1hour.... it will beam up and schedule off after 1 hour
if you click again it will go for 2hour.... it will beam up and schedule off after 2 hour
if you click again it will go for 3hour.... it will beam up and schedule off after 3 hour
...but after 1 hour you have the switchOff scheduled command, than again another off command at the second hour, while you are still in need of it!!
and finally a third off at the 3rd hour....
unless there is a way to avoid this, the script should be moved to a timediff parameter and probably split in 2 different script: a device trigger (like above) and a time script to check the time difference

...Thant's my way to approach it :P
ciao
M
The most dangerous phrase in any language is:
"We always done this way"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest