Exclude option in selector switch after pressing it

Moderator: leecollings

Post Reply
WietseM143
Posts: 2
Joined: Monday 06 April 2020 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Netherlands
Contact:

Exclude option in selector switch after pressing it

Post by WietseM143 »

Hi,
With the help of Domoticz I have automated my curtain at home. For this I used two ESP relays. I then created two switches in Domoticz for these relays called $ESP1 and $ESP2. I then created a selector switch called 'Gordijn_Wietse' with the options 'open', 'dicht' (closed), 'open met interval' (open with intervals, great for the mornings) and 'stop' (to stop if anything would go wrong). I made a script which toggles the ESP switches when performing an action on the selector switch. In this way, when pressing 'open' on the selector switch, the curtain automatically opens and after X seconds the motor stops. I included that in the script. Right now, it is however still possible to press 'open' after I have already opened the curtain, which means the motor tries to open an already open curtain and I'm afraid that at some point in the future these excessive forces will become fatal for my setup. I therefore want to include a restriction. I want to make it impossible to press 'open' or 'open met interval' on the selector switch when the switch is already on 'open'. In other words, the only options to press when the curtain is open should be 'dicht' or 'stop'. I have tried to implement this myself, but till now I'm not managing to do so. Does anyone know if this is even possible and if yes, how?
For switching to open, my straight-forward LUA code right now looks like this:

Code: Select all

if (devicechanged['Gordijn Wietse'] == 'Open') then
    commandArray['$ESP1']="On FOR 69 SECONDS"
    commandArray['$ESP2']='Off'
end
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Exclude option in selector switch after pressing it

Post by waaren »

WietseM143 wrote: Monday 06 April 2020 19:09 I have automated my curtain at home. For this I used two ESP relays. I then created two switches in Domoticz for these relays called $ESP1 and $ESP2. I want to make it impossible to press 'open' or 'open met interval' on the selector switch when the switch is already on 'open'.
Don't know how to tackle this in classic Lua but in dzVents (100% Lua) it could be implemented like below.

When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Gordijn Wietse',
        },
    },
    
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Gordijn manager',
    },
    
    data =
    {
        lastLevel = 
        { 
            initial = 'stop' ,
        },
    },
    
    execute = function(dz, item)
        
        local openRelay = dz.devices('$ESP1')
        local closeRelay = dz.devices('$ESP2')

        local blockOpen = dz.data.lastLevel == 'open' or dz.data.lastLevel =='open met interval'
        local blockClose = dz.data.lastLevel == 'dicht' 
        
        if blockOpen and (item.levelName == 'open' or item.levelName =='open met interval') then 
            dz.log(item.name .. ' already open. Action not allowed', dz.LOG_ERROR)
            return
        elseif blockClose and item.levelName == 'dicht' then 
            dz.log(item.name .. ' already closed. Action not allowed', dz.LOG_ERROR)
            return
        end
        
        dz.log(item.name .. ' switched to ' .. item.levelName ..' (' .. item.level ..'). LastLevel was: ' .. dz.data.lastLevel, dz.LOG_DEBUG)
        dz.data.lastLevel = item.levelName
        
        if item.levelName == 'open'  then
            openRelay.cancelQueuedCommands()
            openRelay.switchOn()
            openRelay.switchOff().afterSec(69)
            closeRelay.switchOff().checkFirst()
        elseif item.levelName == 'open met interval' then
            openRelay.cancelQueuedCommands()
               
            for delay = 0, 3600, 360 do -- 10 steps of 6 minutes each
                openRelay.switchOn().afterSec(delay)
                openRelay.switchOff().afterSec(delay + 7) 
            end    
            
            closeRelay.switchOff().checkFirst()
        elseif item.levelName == 'stop' then
            openRelay.cancelQueuedCommands()
            openRelay.switchOff()
            closeRelay.switchOff()
        elseif item.levelName == 'dicht' then
            openRelay.cancelQueuedCommands()
            closeRelay.switchOn()
            closeRelay.switchOff().afterSec(69)
            openRelay.switchOff()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
WietseM143
Posts: 2
Joined: Monday 06 April 2020 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Netherlands
Contact:

Re: Exclude option in selector switch after pressing it

Post by WietseM143 »

waaren wrote: Monday 06 April 2020 21:27
WietseM143 wrote: Monday 06 April 2020 19:09 I have automated my curtain at home. For this I used two ESP relays. I then created two switches in Domoticz for these relays called $ESP1 and $ESP2. I want to make it impossible to press 'open' or 'open met interval' on the selector switch when the switch is already on 'open'.
Don't know how to tackle this in classic Lua but in dzVents (100% Lua) it could be implemented like below.

When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Gordijn Wietse',
        },
    },
    
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Gordijn manager',
    },
    
    data =
    {
        lastLevel = 
        { 
            initial = 'stop' ,
        },
    },
    
    execute = function(dz, item)
        
        local openRelay = dz.devices('$ESP1')
        local closeRelay = dz.devices('$ESP2')

        local blockOpen = dz.data.lastLevel == 'open' or dz.data.lastLevel =='open met interval'
        local blockClose = dz.data.lastLevel == 'dicht' 
        
        if blockOpen and (item.levelName == 'open' or item.levelName =='open met interval') then 
            dz.log(item.name .. ' already open. Action not allowed', dz.LOG_ERROR)
            return
        elseif blockClose and item.levelName == 'dicht' then 
            dz.log(item.name .. ' already closed. Action not allowed', dz.LOG_ERROR)
            return
        end
        
        dz.log(item.name .. ' switched to ' .. item.levelName ..' (' .. item.level ..'). LastLevel was: ' .. dz.data.lastLevel, dz.LOG_DEBUG)
        dz.data.lastLevel = item.levelName
        
        if item.levelName == 'open'  then
            openRelay.cancelQueuedCommands()
            openRelay.switchOn()
            openRelay.switchOff().afterSec(69)
            closeRelay.switchOff().checkFirst()
        elseif item.levelName == 'open met interval' then
            openRelay.cancelQueuedCommands()
               
            for delay = 0, 3600, 360 do -- 10 steps of 6 minutes each
                openRelay.switchOn().afterSec(delay)
                openRelay.switchOff().afterSec(delay + 7) 
            end    
            
            closeRelay.switchOff().checkFirst()
        elseif item.levelName == 'stop' then
            openRelay.cancelQueuedCommands()
            openRelay.switchOff()
            closeRelay.switchOff()
        elseif item.levelName == 'dicht' then
            openRelay.cancelQueuedCommands()
            closeRelay.switchOn()
            closeRelay.switchOff().afterSec(69)
            openRelay.switchOff()
        end
    end
}
Thanks a lot!! I'm definitely going to try this soon!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest