DzVents script for bathroom lights

Easy to use, 100% Lua-based event scripting framework.
Post Reply
snellejellep
Posts: 105
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi
Domoticz version: 3.97xx
Location: The Neterlands
Contact:

DzVents script for bathroom lights

Post by snellejellep » Wednesday 06 June 2018 20:11

Dear all,

i started using dzvents and it is really nice once you get the logic in your head, i created a script which would turn on my bathroom lights (Group badkamer boven = the name in the script) when my motion sensor(PIR badkamer boven = the name in the script) is triggered or when there is water in the shower(Water douche boven = the name in the script). it works well but i had something in my blockly which i was using before where i could check the lux value of my motionsensor and act upon it, also i had time values for example before 23:00 set light to 100% after 23:00 set light at 40% but only if the shower is off. and this all if the lux value was below 30
while i think dzvents would be a much better solution then my 4 combined blockly scripts which stopped magically working i can not seem to find out from the documentation or the awesome getting started video how to get the above functions in my script. so if someone can help me, please... also, do not laugh at my basic level of dzvents plz XD

Code: Select all

return {
	on = {
		devices = {
			'Water douche boven',
			'PIR badkamer boven',
			'licht tk was aan'
		}
	},
	execute = function(domoticz, device)
		if(device.state == 'On') then
		    local badkamer = domoticz.devices('Group badkamer boven').dimTo(100)
		    badkamer.switchOn()
		else
		    local badkamer = domoticz.devices('Group badkamer boven')
		    badkamer.switchOff()
		end
	end
}
raspberry pi | xiaomi gateway/vacuum | yeelight | philips hue | zwave |ubiquiti unifi | harmony

User avatar
waaren
Posts: 709
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents script for bathroom lights

Post by waaren » Wednesday 06 June 2018 23:52

Could be something like this:

Code: Select all

--[[ ShowerTest.lua for 
]]--

return {
    on = {
        devices = {
            "Water douche boven",                
            "PIR badkamer boven",
            "licht tk was aan",
        }
    },

    execute = function(dz, device)
    
        local luxLevel      = dz.devices("motion sensor").lux
        local shower        = dz.devices("water douche boven").state
        local badkamerLicht = dz.devices("Group badkamer boven")
        
        local lightStrength = 100
        if (dz.time.matchesRule("at 23:00-23:59") or dz.time.matchesRule("at 00:00-08:00")) and shower.state == "Off"  and luxLevel < 30 then 
            lightStrength = 40
        end
    
        if(device.state == "On") then
            badkamerLicht.dimTo(lightStrength)
        else
            badkamerLicht.switchOff()
        end
    end
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

snellejellep
Posts: 105
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi
Domoticz version: 3.97xx
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep » Thursday 07 June 2018 8:10

wow, thanks again!
this is an amazing start, now i just have to figure out how to let the lights stay on for two minutes or something like that when motion was triggered because that hue motion sensor is really sensitive, when you stand still it immediately stops saying that there is motion and flickering lights are a bit anoying :)
but this is really amazing, i hope you did not only help me but also others who are searching for something similar
raspberry pi | xiaomi gateway/vacuum | yeelight | philips hue | zwave |ubiquiti unifi | harmony

User avatar
waaren
Posts: 709
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents script for bathroom lights

Post by waaren » Thursday 07 June 2018 8:59

snellejellep wrote:
Thursday 07 June 2018 8:10

.. now i just have to figure out how to let the lights stay on for two minutes or something like that when motion was triggered because that hue motion sensor is really sensitive, when you stand still it immediately stops saying that there is motion and flickering lights are a bit anoying :)
but this is really amazing, i hope you did not only help me but also others who are searching for something similar..
To give an idea how to solve this; added a minimum of 2 minutes between switch "On" and - "Off"
Spoiler: show

Code: Select all

--[[ ShowerTest.lua 
]]--

return {
    on = {
        devices = {
            "Water douche boven",
            "PIR badkamer boven",
            "licht tk was aan"
        }
    },
    
        
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "ShowerTest" },
    

    execute = function(dz, trigger)
    
        local luxLevel      = dz.devices("motion sensor").lux
        local shower        = dz.devices("water douche boven").state
        local badkamerLicht = dz.devices("Group badkamer boven")
        
        local lightStrength = 100
        if (dz.time.matchesRule("at 23:00-23:59") or dz.time.matchesRule("at 00:00-08:00")) and shower.state == "Off"  and luxLevel < 30 then 
            lightStrength = 40
        end
    
    
        if trigger.state == "On"  then 
            if badkamerLicht.level ~= lightStrength then 
                badkamerLicht.dimTo(lightStrength) 
            end
        elseif badkamerLicht.lastUpdate.minutesAgo > 2 then
            badkamerLicht.switchOff()
        else
            dz.log("No action now. " .. badkamerLicht.name .. 
                " switched to \"" .. badkamerLicht.state .. 
                "\" " .. badkamerLicht.lastUpdate.secondsAgo .. 
                " seconds ago.",dz.LOG_DEBUG)    
        end
    end
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

snellejellep
Posts: 105
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi
Domoticz version: 3.97xx
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep » Thursday 07 June 2018 19:46

waaren wrote:
Thursday 07 June 2018 8:59

To give an idea how to solve this; added a minimum of 2 minutes between switch "On" and - "Off"
Spoiler: show
wow, amazing! cant thank you enough!
raspberry pi | xiaomi gateway/vacuum | yeelight | philips hue | zwave |ubiquiti unifi | harmony

bozidar
Posts: 17
Joined: Saturday 09 September 2017 16:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: DzVents script for bathroom lights

Post by bozidar » Saturday 09 June 2018 11:44

Hi snellejellep
?
Still new to domoticz wondering how do domoticz know shower is on

snellejellep
Posts: 105
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi
Domoticz version: 3.97xx
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep » Monday 11 June 2018 21:11

@bozidar, i use a xiaomi gateway with water sensor, those sensors are really small and fit in the thingy the water flows to (drain?)
links:
gateway: https://www.gearbest.com/living-applian ... id=1433363
sensor: https://www.gearbest.com/home-smart-imp ... id=1433363
other compatible stuff: https://www.domoticz.com/wiki/Compatibl ... n_gearbest
raspberry pi | xiaomi gateway/vacuum | yeelight | philips hue | zwave |ubiquiti unifi | harmony

rrozema
Posts: 107
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi
Domoticz version: beta
Location: Delft
Contact:

Re: DzVents script for bathroom lights

Post by rrozema » Monday 25 June 2018 22:10

waaren wrote:
Thursday 07 June 2018 8:59
snellejellep wrote:
Thursday 07 June 2018 8:10

.. now i just have to figure out how to let the lights stay on for two minutes or something like that when motion was triggered because that hue motion sensor is really sensitive, when you stand still it immediately stops saying that there is motion and flickering lights are a bit anoying :)
but this is really amazing, i hope you did not only help me but also others who are searching for something similar..
To give an idea how to solve this; added a minimum of 2 minutes between switch "On" and - "Off"
Spoiler: show

Code: Select all

--[[ ShowerTest.lua 
]]--

return {
    on = {
        devices = {
            "Water douche boven",
            "PIR badkamer boven",
            "licht tk was aan"
        }
    },
    
        
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "ShowerTest" },
    

    execute = function(dz, trigger)
    
        local luxLevel      = dz.devices("motion sensor").lux
        local shower        = dz.devices("water douche boven").state
        local badkamerLicht = dz.devices("Group badkamer boven")
        
        local lightStrength = 100
        if (dz.time.matchesRule("at 23:00-23:59") or dz.time.matchesRule("at 00:00-08:00")) and shower.state == "Off"  and luxLevel < 30 then 
            lightStrength = 40
        end
    
    
        if trigger.state == "On"  then 
            if badkamerLicht.level ~= lightStrength then 
                badkamerLicht.dimTo(lightStrength) 
            end
        elseif badkamerLicht.lastUpdate.minutesAgo > 2 then
            badkamerLicht.switchOff()
        else
            dz.log("No action now. " .. badkamerLicht.name .. 
                " switched to \"" .. badkamerLicht.state .. 
                "\" " .. badkamerLicht.lastUpdate.secondsAgo .. 
                " seconds ago.",dz.LOG_DEBUG)    
        end
    end
}
Since this script is only triggered by 3 device triggers, how do you make it so that

Code: Select all

elseif badkamerLicht.lastUpdate.minutesAgo > 2 then
gets executed at some time > 2 minutes after the light is no longer on?

snellejellep
Posts: 105
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi
Domoticz version: 3.97xx
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep » Monday 25 June 2018 22:25

rrozema wrote:
waaren wrote:
Thursday 07 June 2018 8:59
snellejellep wrote:
Thursday 07 June 2018 8:10

.. now i just have to figure out how to let the lights stay on for two minutes or something like that when motion was triggered because that hue motion sensor is really sensitive, when you stand still it immediately stops saying that there is motion and flickering lights are a bit anoying :)
but this is really amazing, i hope you did not only help me but also others who are searching for something similar..
To give an idea how to solve this; added a minimum of 2 minutes between switch "On" and - "Off"
Spoiler: show

Code: Select all

--[[ ShowerTest.lua 
]]--

return {
    on = {
        devices = {
            "Water douche boven",
            "PIR badkamer boven",
            "licht tk was aan"
        }
    },
    
        
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   "ShowerTest" },
    

    execute = function(dz, trigger)
    
        local luxLevel      = dz.devices("motion sensor").lux
        local shower        = dz.devices("water douche boven").state
        local badkamerLicht = dz.devices("Group badkamer boven")
        
        local lightStrength = 100
        if (dz.time.matchesRule("at 23:00-23:59") or dz.time.matchesRule("at 00:00-08:00")) and shower.state == "Off"  and luxLevel < 30 then 
            lightStrength = 40
        end
    
    
        if trigger.state == "On"  then 
            if badkamerLicht.level ~= lightStrength then 
                badkamerLicht.dimTo(lightStrength) 
            end
        elseif badkamerLicht.lastUpdate.minutesAgo > 2 then
            badkamerLicht.switchOff()
        else
            dz.log("No action now. " .. badkamerLicht.name .. 
                " switched to \"" .. badkamerLicht.state .. 
                "\" " .. badkamerLicht.lastUpdate.secondsAgo .. 
                " seconds ago.",dz.LOG_DEBUG)    
        end
    end
}
Since this script is only triggered by 3 device triggers, how do you make it so that

Code: Select all

elseif badkamerLicht.lastUpdate.minutesAgo > 2 then
gets executed at some time > 2 minutes after the light is no longer on?
I ran in to that problem myself so i added a five minute timer as an extra trigger. So it gets triggered every five minutes to check if the lights still need to be on
raspberry pi | xiaomi gateway/vacuum | yeelight | philips hue | zwave |ubiquiti unifi | harmony

rrozema
Posts: 107
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi
Domoticz version: beta
Location: Delft
Contact:

Re: DzVents script for bathroom lights

Post by rrozema » Tuesday 26 June 2018 4:54

ah, thanks. I was wondering if I had overlooked something :-) I have a separate, generic script called "Auto Off" that gets executed once a minute to check for devices that need to be switched off. This way I can keep the scripts to switch things on simple.

rogzon
Posts: 11
Joined: Monday 14 November 2016 10:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: DzVents script for bathroom lights

Post by rogzon » Wednesday 27 June 2018 14:21

In the off event for the PIR you could put domoticz.devices('bathroom lights name').switchOff().afterMin(2) which would switch off lights two minutes after PIR has reported no motion. You then need to put domoticz.devices('bathroom lights name').cancelQueuedCommands() in PIR On event otherwise it will switch off no matter if PIR has detected motion or not.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests