dzvents script for garage door

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

Moderator: leecollings

Post Reply
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: dzvents script for garage door

Post by hoeby »

I modified some things. Don't know if this is what you are looking for, but could be some start.

What is changed:
On the top of the code, there is a part on = { device = { }}
This is for the devices which needs to trigger the script. You also added the action devices.
When the action devices are added, they also trigger the script, which could give strange behavior.

You used quietOn and quietOff, this sets the deviceStatus to on/off without physically switching it
Changed this to switchOn/Off with addon .silent() to not trigger other scripts.

This part i don't get what you would like to do with it.
It is always triggered if the script is triggerd. Is Garage 2 sensor closed is active, then also this will activate the garage 2 relay for 2 sec. But not direct, but indirect. Because the else part of this code is true in this case and will set the device Garage 2 door to ON, because of that, the script if triggered again and then the if is true and will switch Garage 2 Relay

Code: Select all

            -- if Dummi switch is pressed to activate the relay for 2sec that will open the door
            if (domoticz.devices('Garage 2 Door').state == 'On') then                    
                domoticz.devices('Garage 2 Relay').switchOn().forSec(2)
            else
                domoticz.devices('Garage 2 Door').switchOn().forSec(2)
            end
The complete modified code

Code: Select all

return {
	on = {
			devices = {
			   'Garage 2 Door',           -- Dummy switch                    Garage 2 Door idx 73
			   'Garage 2 Sensor Closed',  -- Magnet sensor          Garage 2 Sensor Closed idx 76
             } 
	},
	
        logging = {
		level = domoticz.LOG_INFO,
		marker = 'Garage 2',
	},

	execute = function(domoticz)
	    -- if standart Remote control is used only to indicate the condition
            if (domoticz.devices('Garage 2 Sensor Closed').state == 'On') then                       
                domoticz.devices('Garage 2 Door').switchOn().silent()
                domoticz.notify('Garage 2', 'Garage 2 Open', domoticz.PRIORITY_EMERGENCY)
            else
                domoticz.devices('Garage 2 Door').switchOff().silent()
                domoticz.notify('Garage 2', 'Garage 2 Closed', domoticz.PRIORITY_EMERGENCY)
            end
        
            -- if Dummi switch is pressed to activate the relay for 2sec that will open the door
            if (domoticz.devices('Garage 2 Door').state == 'On') then                    
                domoticz.devices('Garage 2 Relay').switchOn().forSec(2)
            else
                domoticz.devices('Garage 2 Door').switchOn().forSec(2)
            end
    
            -- to switch On the garage light between sunset and sunrise   
            if domoticz.devices('Garage 2 Sensor Closed').state == 'On' and domoticz.time.('at nighttime') then  
                domoticz.devices('Garage 2 Light Light').switchOn()
            else
                domoticz.devices('Garage 2 Light Light').switchOff()
            end 
    end
}
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: dzvents script for garage door

Post by heggink »

Looks like spam

Sent from my SM-G980F using Tapatalk

Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
tonito1509
Posts: 4
Joined: Thursday 08 August 2019 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Marseille
Contact:

Re: dzvents script for garage door

Post by tonito1509 »

So i manage to make 3 scripts that are working together but i don't know how to combine them in one script. Is anybody able to help? Thanks.

script 1

Code: Select all

return {
        on = {
            devices = {
            'Garage 2 Sensor Closed'
            }
        },
        execute = function(domoticz, switch)
            if (switch.state == 'On') then
                domoticz.devices('Garage 2 Door').switchOn().silent()
                domoticz.notify('Garage 2 ', 'GARAGE 2 Open', domoticz.PRIORITY_EMERGENCY) 
            else
                domoticz.devices('Garage 2 Door').switchOff().silent()
                domoticz.notify('Garage 2 ', 'GARAGE 2 Closed', domoticz.PRIORITY_EMERGENCY) 
            end
        end
    }
script 2

Code: Select all

return {
    on = {
        timer = {
            'at nighttime'
        }

    },
    execute = function(domoticz, timer)
        if (domoticz.devices('Garage 2 Sensor Closed').state == 'On') then
            domoticz.devices('Garage 2 Light').switchOn()
            domoticz.log('Garage Light On!', domoticz.LOG_INFO)
            
        else
            domoticz.devices('Garage 2 Light').switchOff()
            domoticz.log('Garage Light Off!', domoticz.LOG_INFO)
        end
    end
}
script 3

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Garage 2 Door',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,                                              
        marker = 'Relay',
    },

    execute = function(dz, item)

        local relay = dz.devices(74)                                              -- Garage 2 Relay idx 74

        dz.log('State of ' .. item.name .. ' is ' .. item.state, dz.LOG_DEBUG)
        if item.active then
            relay.switchOn().forSec(2)
        else
            relay.switchOn().forSec(2)
        end 
    end
}
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: dzvents script for garage door

Post by hoeby »

heggink wrote: Friday 22 April 2022 15:31 Looks like spam
Don't think it is spam.
But the thread looks strange, the openingspost from tonito1509 is gone.
My reply is the first post, now it looks like i started it, but it is not


@Tonito1509,
Why put everything in 1 script, when it works like you have now?
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
tonito1509
Posts: 4
Joined: Thursday 08 August 2019 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Marseille
Contact:

Re: dzvents script for garage door

Post by tonito1509 »

hoeby wrote: Monday 25 April 2022 7:04
heggink wrote: Friday 22 April 2022 15:31 Looks like spam
Don't think it is spam.
But the thread looks strange, the openingspost from tonito1509 is gone.
My reply is the first post, now it looks like i started it, but it is not


@Tonito1509,
Why put everything in 1 script, when it works like you have now?
@hoeby
My post is gone because i was banned for some misunderstanding due to a spam replay on my first post. After discussion with the moderators I am back. Good question why in one script. I also have it in blockly and it works perfect. In one script it will be everything in one place and it will not look messy. As i said from the beginning i learn how to make a scripts in dzvents and LUA. I am self learning in my free time which is not so much so any help will be appreciated.
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: dzvents script for garage door

Post by hoeby »

It is not tested, but give it a try.
I just copied your parts and needed to do little modifications, to get it in 1 script
Maybe there are better ways, but that is the fun with scripts. More ways to get to the working environment

If there is an error. Copy the error codeline from domoticz-log to here

Code: Select all

return {
        on = {
            devices = { 'Garage 2 Sensor Closed',
                        'Garage 2 Door' },
            timer = { 'at nighttime' },
        },
        execute = function(domoticz, item)
            if (item.trigger == 'Garage 2 Sensor Closed') then
                if  (domoticz.devices('Garage 2 Sensor Closed').state == 'On') then
                    domoticz.devices('Garage 2 Door').switchOn().silent()
                    domoticz.notify('Garage 2 ', 'GARAGE 2 Open', domoticz.PRIORITY_EMERGENCY) 
                else
                    domoticz.devices('Garage 2 Door').switchOff().silent()
                    domoticz.notify('Garage 2 ', 'GARAGE 2 Closed', domoticz.PRIORITY_EMERGENCY) 
                end
            end

            if (item.isTimer) then
                if (domoticz.devices('Garage 2 Sensor Closed').state == 'On') then
                    domoticz.devices('Garage 2 Light').switchOn()
                    domoticz.log('Garage Light On!', domoticz.LOG_INFO)
                else
                    domoticz.devices('Garage 2 Light').switchOff()
                    domoticz.log('Garage Light Off!', domoticz.LOG_INFO)
                end
            end
            
            if (item.trigger == 'Garage 2 Door') then
                if (domoticz.devices('Garage 2 Door').state == 'On') then
                    domoticz.devices(74).switchOn().forSec(2)
                else
                    domoticz.devices(74).switchOn().forSec(2)
                end 
            end
        end
}
            
            
            
        end 
}
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
tonito1509
Posts: 4
Joined: Thursday 08 August 2019 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Marseille
Contact:

Re: dzvents script for garage door

Post by tonito1509 »

The script was giving me an error on line 40 where was "end & }" so i removed them and no error anymore. Then when i press "Garage 2 Door" it doesn't trigger "Garage 2 Relay". When i simulate that ("Garage 2 Sensor Closed" == On ) it doesn't run " ('Garage 2 Door').switchOn().silent() ". I noticed that when the "timer = { 'at nighttime' }" is added this way is taking action every complete minute and also the script is checking every minute , but if is this way if (domoticz.time.isNightTime and switch.state == 'On') it is reacting immediately. If i am not wrong the same kind of timer problem was mentioned on another thread.
Attachments
Screenshot (518).png
Screenshot (518).png (39.61 KiB) Viewed 685 times
Screenshot (522).png
Screenshot (522).png (157.74 KiB) Viewed 685 times
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: dzvents script for garage door

Post by hoeby »

You where correct, it wasn't working, sorry my fault.
Made some changes

Only tested the "Garage 2 Door" reaction. that works in my environment.

That you get a trigger every minut is correct.
"timer = { 'at nighttime' }" means between sunset and sunrise. This will be checked every minut and therefor it is every minut true when it is between sunset and sunrise. If you need to let the timer work at 00:00hour. Then you need to modify 'at nighttime' to 'at midnight'

This line isn't going to work correctly (domoticz.time.isNightTime and switch.state == 'On')
You haven't defined 'switch' in the command switch.state, which devices you mean with it.

Code: Select all

return {
        on = {
            devices = { 'Garage 2 Sensor Closed',
                        'Garage 2 Door' },
            timer = { 'at nighttime' },
        },
        execute = function(domoticz, item)
            if (item.name == 'Garage 2 Sensor Closed') then
                if  (domoticz.devices('Garage 2 Sensor Closed').state == 'On') then
                    domoticz.devices('Garage 2 Door').switchOn().silent()
                    domoticz.notify('Garage 2 ', 'GARAGE 2 Open', domoticz.PRIORITY_EMERGENCY) 
                else
                    domoticz.devices('Garage 2 Door').switchOff().silent()
                    domoticz.notify('Garage 2 ', 'GARAGE 2 Closed', domoticz.PRIORITY_EMERGENCY) 
                end
            end

            if (item.isTimer) then
                if (domoticz.devices('Garage 2 Sensor Closed').state == 'On') then
                    domoticz.devices('Garage 2 Light').switchOn()
                    domoticz.log('Garage Light On!', domoticz.LOG_INFO)
                else
                    domoticz.devices('Garage 2 Light').switchOff()
                    domoticz.log('Garage Light Off!', domoticz.LOG_INFO)
                end
            end
            
            if (item.name == 'Garage 2 Door') then
                if (domoticz.devices('Garage 2 Door').state == 'On') then
                    domoticz.devices(74).switchOn().forSec(2)
                else
                    domoticz.devices(74).switchOn().forSec(2)
                end 
            end
        end
}
Just copied your parts, like said.
Maybe it is because you want to extend the scripting.

But this part:

Code: Select all

 if (item.name == 'Garage 2 Door') then
                if (domoticz.devices('Garage 2 Door').state == 'On') then
                    domoticz.devices(74).switchOn().forSec(2)
                else
                    domoticz.devices(74).switchOn().forSec(2)
                end 
            end
Could also be written like. You do a check on the state, but there is no difference in reaction, both do the same, why check it then?

Code: Select all

 if (item.name == 'Garage 2 Door') then
                    domoticz.devices(74).switchOn().forSec(2)
            end
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
tonito1509
Posts: 4
Joined: Thursday 08 August 2019 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Marseille
Contact:

Re: dzvents script for garage door

Post by tonito1509 »

hoeby wrote: Thursday 28 April 2022 14:46 [/code]

Just copied your parts, like said.
Maybe it is because you want to extend the scripting.

But this part:

Code: Select all

 if (item.name == 'Garage 2 Door') then
                if (domoticz.devices('Garage 2 Door').state == 'On') then
                    domoticz.devices(74).switchOn().forSec(2)
                else
                    domoticz.devices(74).switchOn().forSec(2)
                end 
            end
Could also be written like. You do a check on the state, but there is no difference in reaction, both do the same, why check it then?

Code: Select all

 if (item.name == 'Garage 2 Door') then
                    domoticz.devices(74).switchOn().forSec(2)
            end
Thank you for the help! The script work as it was expected. So, 'Garage 2 Door' is a dummy switch which is showing the condition with the garage Icon and triggering "Relay idx74" that is acting as a inching relay made from "sonoff basic" with "Tasmota " modified to only close the circuit when is "On" (main power is isolated from relay contact). When 'Garage 2 Door' is pressed it will remain "active" but the relay will goes "Off" after 2sec and the door will open. So next time when it is pressed and goes inactive it will trigger the relay again for only 2sec and the door will close. Somebody will ask why two sensors. The purpose is that something has to tell domoticz to show 'Garage 2 Door' dummy as a active when i am using the original remote control which is specific for Hörmann devices. The second sensor has to show if the door is in intermediate position or is fully open which can give you another benefits.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest