DzVents script for bathroom lights

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

Moderator: leecollings

Post Reply
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

DzVents script for bathroom lights

Post by snellejellep »

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 vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents script for bathroom lights

Post by waaren »

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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep »

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 vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents script for bathroom lights

Post by waaren »

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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep »

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 vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
bozidar
Posts: 19
Joined: Saturday 09 September 2017 16:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DzVents script for bathroom lights

Post by bozidar »

Hi snellejellep
?
Still new to domoticz wondering how do domoticz know shower is on
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep »

@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 vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: DzVents script for bathroom lights

Post by rrozema »

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: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: DzVents script for bathroom lights

Post by snellejellep »

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 vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: DzVents script for bathroom lights

Post by rrozema »

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 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DzVents script for bathroom lights

Post by rogzon »

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.
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: DzVents script for bathroom lights

Post by pvm »

snellejellep wrote:@bozidar, i use a xiaomi gateway with water sensor, those sensors are really small and fit in the thingy the water flows to (drain?)
Brilliant idea, thnx! Nice solution for switching ventilation
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
Post Reply

Who is online

Users browsing this forum: andrehj, Bing [Bot], Google [Bot] and 1 guest