LUA script - prevent shutter from closing while window is open

Moderator: leecollings

Post Reply
brianhunt
Posts: 8
Joined: Monday 26 September 2016 18:18
Target OS: -
Domoticz version:
Contact:

LUA script - prevent shutter from closing while window is open

Post by brianhunt »

Since I couldn't find a script on the forum, I wrote my own and thought it may be interesting for others with similar situations.

In my home, I have three Fibaro roller shutters to control the shutters in front of my windows. However, if the window is open, the shutter (or the window) can get damaged if the shutter closes. Since I don't want to put a lot of 'window checks' in all scripts/scenes where I want to a automatically close the shutters, I needed a general script that checks if the window is open when someone (or somewhat ;) ) tries to close the shutter.

When analyzing the log of the shutter, I found out that domoticz uses two ways to issue a close command: it can either say that it needs to switch to 'Closed' or it can set the level of the shutter to the closed level (100 for a regular shutter, 0 for an inverted shutter). So the script needs to deal with both situations.

Also, domoticz does not show if the shutter is moving, it just shows two events: when a motion is started and when the motion has ended. So in order to check if a motion is started (e.g. closing the shutter), I use a user variable. If the current position of the shutter is the same as the old position (saved in the variable), this implies that a motion is starting. If they differ, the motion has ended.

Unfortunately, it turns out that activating the shutter through the hardware button connected to the Fibaro control, does not generate an event. So any user using that button, should pay attention. Fortunately, he or she is next to the window so it is quite easy to see if anything is going wrong... :shock:

Since I have three shutters, two with one window and one with two windows, I created a script that can deal with all of these shutters at once. It also takes into account the fact that two of the shutters have inverted wiring (need to change that sometime) so the values of the shut position differ from the regular wired shutter.

Feel free to adjust the code at your will or let me know how to improve it further.

Code: Select all


-- script to ensure that the shutter cannot be closed while there is a window open (blocking the shutter)
-- script only works on closing events initiated from domoticz
-- unfortunately, if the button next to the window is used, it only generates an event when 
-- the shutter has stopped moving
--
-- it turns out that closing the shutter can be done in two ways:
--  1. the svalue is set to 99
--  2. the device is set to 'closed'
-- In both cases, the device first changes one of these attributes but after that
-- tells domoticz again the current starting position for the movement
-- therefore line ... checks if the two attributes are 'in sync'
-- If they are not in sync, the situation is ignored
--
-- The device normally sends two situations to Domoticz: when the movement 
-- starts and when the movement has ended.
-- The script requires a separate variable to check the inital position
-- If the current position and the initial position (variable) are the same,
-- this implies that a movement has started.
-- If the current position and the initial position (variable) are not the same,
-- this implies that the movement has ended.

-- local shutter_device = 'Rolluik rechts'
-- local shutter_open_value = '0'      -- Could differ per device if you use an inverted shutter switch
-- local window_device = 'Slaapkamerraam rechts'
-- local luik_positie_variable = 'RolluikRechtsPositie'


commandArray = {}

  -- Array of shutters to be checked
    shutters = {
            Masterrechts = {shutter_device = 'Rolluik rechts' ; shutter_open_value = '0'; window_device_1 = 'Slaapkamerraam rechts'; window_device_2 = 'Slaapkamerraam rechts'; luik_positie_variable = 'RolluikRechtsPositie'};
            Masterlinks = {shutter_device = 'Rolluik Links' ; shutter_open_value = '100'; window_device_1 = 'Slaapkamerraam links';window_device_2 = 'Slaapkamerraam links'; luik_positie_variable = 'RolluikLinksPositie'};
            Kamerdaniel = {shutter_device = 'Rolluik Daniël' ; shutter_open_value = '0'; window_device_1 = 'Raam Daniël rechts';window_device_2 = 'Raam Daniël links'; luik_positie_variable = 'RolluikDanielPositie'};
          }

    for shutter ,credentials in pairs(shutters) do
        if (devicechanged[credentials.shutter_device]) then
    		print('<font color="blue"> Stand rolluik ' ..shutter ..': ' ..otherdevices[credentials.shutter_device])
    		print('<font color="blue"> Positie rolluik ' ..shutter ..': ' ..otherdevices_svalues[credentials.shutter_device])
    		print('<font color="blue"> Rolluik positie variabele: ' ..uservariables[credentials.luik_positie_variable])
            if ((otherdevices_svalues[credentials.shutter_device] == credentials.shutter_open_value) and (otherdevices[credentials.shutter_device] == 'Open')) then -- ignore if two attributes are not in sync
                if (otherdevices_svalues[credentials.shutter_device] == tostring(uservariables[credentials.luik_positie_variable])) then --als gelijk dan wordt beweging ingezet
                    print('<font color="blue"> rolluik ' ..shutter ..' beweging naar beneden ingezet')
              		if ((otherdevices[credentials.window_device_1]== 'Open') or (otherdevices[credentials.window_device_2]== 'Open')) then
                        print('<font color="red">Rolluik ' ..shutter ..' wordt gesloten terwijl raam nog open staat')
            		    commandArray[1] = {[credentials.shutter_device]='Set Level ' ..credentials.shutter_open_value}  
            		    commandArray[2]={['SendNotification']='Rolluik ' ..shutter ..' wordt gesloten terwijl raam nog open staat'}
            		    commandArray[3] = {['Variable:' ..credentials.luik_positie_variable] = tostring('50')}
    		        end
                else 
               		print('<font color="blue"> variabele en positie rolluik waren niet gelijk dus dat wordt gecorrigeerd')
                    commandArray[1] = {['Variable:' ..credentials.luik_positie_variable] = tostring(otherdevices_svalues[credentials.shutter_device])}
                end
            end
    	end
    end	

return commandArray
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: LUA script - prevent shutter from closing while window is open

Post by thaui »

Thanks for the great work. I will use it to protect myself that my terrace shutter isn't me locking out when I am doing my barbecue.
I will get an error message in the log file but don't find a solution to fix it. Can you pls. help?

Error: EventSystem: in Terrasse roller shutter controll: [string " ..."]:37: attempt to index global 'devicechanged' (a nil value)
brianhunt
Posts: 8
Joined: Monday 26 September 2016 18:18
Target OS: -
Domoticz version:
Contact:

Re: LUA script - prevent shutter from closing while window is open

Post by brianhunt »

Hi Thaui,
Glad to help you. Quite important that you are not shut out :lol:

Can you let me know what line 37 is in your script? (in my script, it's an empty line)
thaui
Posts: 58
Joined: Sunday 15 March 2015 19:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Hamburg
Contact:

Re: LUA script - prevent shutter from closing while window is open

Post by thaui »

Hi brianhunt,
for sure I have edited your original script because I have only one shutter. So line 34 & 35 is deleted. In line 37 we have than the following content,

if (devicechanged[credentials.shutter_device]) then
....
....
brianhunt
Posts: 8
Joined: Monday 26 September 2016 18:18
Target OS: -
Domoticz version:
Contact:

Re: LUA script - prevent shutter from closing while window is open

Post by brianhunt »

It could be that Domoticz doesn't recognize the device, probably because of an incorrect name.
Can you check that you wrote the name of the shutter exactly the same in the Shutters array? (including spaces and capitals!)

One way to make sure that it's completely the same, is by selecting the device in the dashboard, clicking Edit, copying the name of the device and pasting it into the script.

If that doesn't work, please send me the complete script by private message and I'll check it
gerard76
Posts: 53
Joined: Wednesday 22 March 2017 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: LUA script - prevent shutter from closing while window is open

Post by gerard76 »

devicechanged is nil when the script is triggered by anything else then Device. Make sure it's not set to All or Time in the pulldown below Lua.
mcjackdus
Posts: 15
Joined: Monday 22 October 2018 10:34
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: LUA script - prevent shutter from closing while window is open

Post by mcjackdus »

Hi,

I am trying to implement your script but without succes.
Can you explain how this user variable works?

This is what the log shows.
2020-12-23 15:32:58.116 Status: User: Admin initiated a switch command (59/Rolluik Woonkamer Achter/Off)
2020-12-23 15:32:58.119 Status: LUA: <font color="blue"> Stand rolluik Shutter1: Open
2020-12-23 15:32:58.119 Status: LUA: <font color="blue"> Positie rolluik Shutter1: 99
2020-12-23 15:32:58.119 Status: LUA: <font color="blue"> Rolluik positie variabele: 99

Opening or closing the window contact does not make a difference.
commandArray = {}

This is the line i changes in the script.
-- Array of shutters to be checked
shutters = {
Shutter1 = {shutter_device = 'Rolluik Woonkamer Achter' ; shutter_open_value = '0'; window_device_1 = 'Raam Sensor'; luik_positie_variable = 'Rolluik Woonkamer Achter Positie'};
}

Thanks in advance,
Jack
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest