dzVents for motion sensor

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

Moderator: leecollings

Post Reply
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Mayki wrote: Tuesday 22 January 2019 10:39 I need to be in the time period between 23:00 and 6:30 they turned on the lights, but only if not already switched on in some other way. Five minutes without moving to turn off again. But I want them to shine through the night mode. Which in the LUA script is set as follows:
Can you please help me?
If you study the working example scripts on this forum ( combined devices / time , cancelQueuedCommands() and os.execute ) you should have enough information on how to build your dzVents script. Please feel free to ask if you don't understand the logic in any of these examples.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Mayki wrote: Tuesday 22 January 2019 20:26 I needed to help with :

Code: Select all

if (light.active) then
	light.switchOff().afterMin(5)
else
	light.switchOn().forMin(5)
end
Probably over complicated but you still might find it useful.

Code: Select all

--[[ 
Motion sensor "PIR chodba" Idx 27
Yeelight ceiling "Světlo chodba" Idx 14 and "Světlo hala" Idx 19

I need to be in the time period between 23:00 and 6:30 they turned on the lights, but only if not already switched on in some other way. Five minutes without moving to turn off again. But I want them to shine through the night mode. Which in the LUA script is set as follows:
    
    local runcommandbaby = "sudo echo -ne '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 1, 500]}\\r\\n"

]]--

local nightTime = {"at 21:00-06:30" } 
return {
            on =    {  
                        devices =    {
                                         ["PIR chodba"] =  nightTime ,      -- PIR need to react during nightTime
                                         "Světlo chodba","Světlo hala"  ,    -- controlling selector devices
                                    },
                        
                        timer   =   { 
                                        "every 5 minutes between 23:00 and 06:30", 
                                        "at 06:32",
                                    },            
                    },
    
    logging =   {  
                    level     =   domoticz.LOG_DEBUG,
                    marker    =   "PIRTest" 
                },
    

    execute = function(dz, item)
    
        local function logWrite(str,level)             -- Support function for shorthand debug log statements
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
       
        local chodba        = dz.devices("Světlo chodba")   
        local hala          = dz.devices("Světlo hala")
        local PIR           = dz.devices("PIR chodba")

        local function getLevel( levels, search )             -- support function to get level (0,10,20 etc.. based on levelNames)
            for index,levelName in ipairs(levels) do
                logWrite("getLevel, index: " .. index .. " ==>> " .. levelName .. " " .. search )
                if search == levelName then return ( ( index - 1 ) * 10 ) end
            end
            return nil
        end
        
        local function setLight(t,key)                        -- This function does the actual work 
            if not t[key] then 
                logWrite("No such defined setting (" .. key .. ")" ,dz.LOG_ERROR)
                return 
            else
                logWrite("Found " .. key .." ==> " .. t[key] )
                local osCommand = "sudo echo -ne " .. t[key] .. " \\r\\n | " .. t.executable .. t.parm .. t.address .. t.port .. t.background
                logWrite("Sending command: \r\n" .. osCommand)
                os.execute(osCommand)
                logWrite("Calculated level: " ..  getLevel(t.levels,key))
                t.device.switchSelector(getLevel(t.levels,key)).silent()            -- update the selector to reflect the new level but prevent loop 
            end
        end
        
                
        local chodbaControls =                                                       -- Table with all relevant settings
            {    
                device          = chodba,
                Off             = '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}',
                ["Bright 25%"]  = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 25, 500]}',     
                ["Bright 50%"]  = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 50, 500]}',
                ["Bright 75%"]  = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 75, 500]}',
                Sunrise         = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",2,2,\"50,2,4000,75,900000,2,4000,1\"]}',
                Sunset          = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",24,2,\"4000,2,4000,30,7000,2,4000,10,8000,2,4000,1\"]}',
                ["4-7-8"]       = '{\"id\":1,\"method\":\"set_scene\",\"params\":[\"cf\",24,2,\"4000,2,400[0,30,7000,2,4000,10,8000,2,4000,1\"]}',
                Baby            = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 1, 500]}', 
                levels          = {"Off","Bright 25%","Bright 50%","Bright 75%","Sunrise","Sunset","4-7-8","Baby"},
                executable      = "/opt/bin/ncat",
                parm            = " -w1 ",
                address         = "192.168.0.112 ",
                port            = " 55443",
                background      = " &"
            }
        
        local halaControls =
            {
                device          = hala,
                Off             = '{\"id\":1,\"method\":\"set_power\", \"params\":[\"off\", \"smooth\", 500]}',
                ["Bright 25%"]  = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 25, 500]}',     
                ["Bright 50%"]  = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 50, 500]}',
                ["Bright 75%"]  = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 75, 500]}',
                Sunrise         = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",2,1,\"50,2,4000,1,900000,2,4000,100\"]}',
                Sunset          = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"cf\",2,2,\"50,2,4000,75,900000,2,4000,1\"]}',
                ["4-7-8"]       = '{\"id\":1,\"method\":\"set_scene\",\"params\":[\"cf\",24,2,\"4000,2,4000,30,7000,2,4000,10,8000,2,4000,1\"]}',
                Baby            = '{\"id\":1,\"method\":\"set_scene\", \"params\":[\"bright\", 1, 500]}', 
                levels          = {"Off","Bright 25%","Bright 50%","Bright 75%","Sunrise","Sunset","4-7-8","Baby"},
                executable      = "/opt/bin/ncat",
                parm            = " -w1 ",
                address         = "192.168.0.105 ",
                port            = " 55443",
                background      = " &"
            } 
        
        if item.isTimer and dz.time.matchesRule("at 06:32") then               -- switch Off in the morning
            setLight(chodbaControls,"Off")
            setLight(halaControls,"Off")
        elseif item.isTimer and PIR.lastUpdate.minutesAgo > 5 and PIR.lastUpdate.minutesAgo < 15 then  -- No activity during the night ?
            if hala.level ~= halaControls.BabyLevel then setLight(halaControls,"Baby") end             -- switch to baby mode if not already there 
            if chodba.level ~= chodbaControls.BabyLevel then setLight(chodbaControls,"Baby") end      
        elseif item.name == "Světlo chodba" then                    -- triggered by manual selector change
            setLight(chodbaControls,item.levelName)      
        elseif item.name == "Světlo hala" then
            setLight(halaControls,item.levelName)                   -- triggered by manual selector change
        elseif item.name == "PIR chodba" and item.isActive then
            setLight(halaControls,"Baby")                           -- PIR see something    
            setLight(chodbaControls,"Baby")      
        else
            setLight(halaControls,"Off")      
        end
        
        -- test / debug only
        if _G.logLevel == dz.LOG_DEBUG then
            for key, value in pairs(halaControls) do
                logWrite(key .." => " .. tostring(value))
            end
        end
    end
}   
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Sorry to hear that. Does the command work if you enter it manually ? I am asking because I tried it on my yeelights and it does not work for me. Not from dzVents but also not if I enter it from the commandline.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Mayki wrote: Thursday 24 January 2019 10:55 I do not understand if, I use the dummy switch "Scény chodba" that controls the LUA script, I choose BABY mode, the light turns on.
What is the difference between what Domoticz sends from a LUA script or I through CMD?
The only difference I see it that the original Lua script use a relative path and dzVents (and you from the CLI) use a full qualified path to ncat
Where is the binary ncat on your system ?
[EDIT]
When I use netcat in the commandline and leave out the single quotes it works from the command line. I now "only" have to get the command to the os without losing the quotes. Need some time to puzzle so don't expect anything soon.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Mayki wrote: Thursday 24 January 2019 12:42
waaren wrote: Thursday 24 January 2019 12:29 The only difference I see it that the original Lua script use a relative path and dzVents (and you from the CLI) use a full qualified path to ncat
Where is the binary ncat on your system ?

Code: Select all

login as: root
[email protected]'s password:
root@DiskStation:~# cd ..
root@DiskStation:/# cd /opt/bin
root@DiskStation:/opt/bin# ls
ash  find  grep  islebe  localedef.new  locale.new  ncat  netstat  opkg  sh  xargs
root@DiskStation:/opt/bin#
I understand but in your Lua example I see that opt/bin is used. So if domoticz base directory is /home/pi , I expect that the Lua will look in
/home/pi/opt/bin

The dzVents script use /opt/bin and will look in the /opt/bin
Please correct me if I am wrong.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Mayki wrote: Thursday 24 January 2019 15:33 I use Domoticz installed on NAS Synology
Got it working on my system. Can you check what your result is on yours ?

Code: Select all

--[[ 
Motion sensor "PIR chodba" Idx 27
Yeelight ceiling "Světlo chodba" Idx 14 and "Světlo hala" Idx 19

I need to be in the time period between 23:00 and 6:30 they turned on the lights, but only if not already switched on in some other way. Five minutes without moving to turn off again. But I want them to shine through the night mode. Which in the LUA script is set as follows:
    
    local runcommandbaby = "sudo echo -ne '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 1, 500]}\\r\\n"

]]--

local nightTime = {"at 21:00-06:30" } 
return {
            on =    {  
                        devices =    {
                                         ["PIR chodba"] =  nightTime ,      -- PIR need to react during nightTime
                                         ["XButton-1"]  =  nightTime ,
                                          "Světlo chodba","Světlo hala"  ,    -- controlling selector devices
                                    },
                        
                        timer   =   { 
                                        "every 5 minutes between 23:00 and 06:30", 
                                        "at 06:32",
                                    },            
                    },
    
    logging =   {  
                    level     =   domoticz.LOG_DEBUG,
                    marker    =   "PIRTest" 
                },
    

    execute = function(dz, item)
    
        local function logWrite(str,level)             -- Support function for shorthand debug log statements
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
       
        local chodba        = dz.devices("Světlo chodba")   
        local hala          = dz.devices("Světlo hala")
        local PIR           = dz.devices("PIR chodba")

        local function getLevel( levels, search )             -- support function to get level (0,10,20 etc.. based on levelNames)
            for index,levelName in ipairs(levels) do
                logWrite("getLevel, index: " .. index .. " ==>> " .. levelName .. " " .. search )
                if search == levelName then return ( ( index - 1 ) * 10 ) end
            end
            return nil
        end
        
        local function setLight(t,key)                        -- This function does the actual work 
            if not t[key] then 
                logWrite("No such defined setting (" .. key .. ")" ,dz.LOG_ERROR)
                return 
            else
                logWrite("Found " .. key .." ==> " .. t[key] )
                local osCommand = "sudo echo -ne " .. t[key] .. "\\r\\\\n  | " .. t.executable .. t.parm .. t.address .. t.port ..";"
                logWrite("Sending command: \r\n" .. osCommand)
                os.execute(osCommand)
                local osCommand = "sudo echo -ne " .. t[key] .. "\\r\\\\n | " .. t.executable .. t.parm .. t.address .. t.port .. "; > fff.log"
                os.execute(osCommand)
                
                logWrite("Calculated level: " ..  getLevel(t.levels,key))
                t.device.switchSelector(getLevel(t.levels,key)).silent()            -- update the selector to reflect the new level but prevent loop 
            end
        end
        
                
        local chodbaControls =                                                       -- Table with all relevant settings
            {    
                device          = chodba,
                Off             = '{\\"id\\":1,\\"method\\":\\"set_power\\", \\"params\\":[\\"off\\", \\"smooth\\", 500]}\\',
                ["Bright 25%"]  = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 25, 500]}\\',     
                ["Bright 50%"]  = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 50, 500]}\\',
                ["Bright 75%"]  = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 75, 500]}\\',
                Sunrise         = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",2,2,\\"50,2,4000,75,900000,2,4000,1\\"]}\\',
                Sunset          = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",24,2,\\"4000,2,4000,30,7000,2,4000,10,8000,2,4000,1\\"]}\\',
                ["4-7-8"]       = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",24,2,\\"4000,2,400[0,30,7000,2,4000,10,8000,2,4000,1\\"]}\\',
                Baby            = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 1, 500]}\\', 
                levels          = {"Off","Bright 25%","Bright 50%","Bright 75%","Sunrise","Sunset","4-7-8","Baby"},
                executable      = "/opt/bin/ncat",
                parm            = " -w2 ",
                address         = "192.168.0.112 ",
                port            = " 55443",
                background      = " &"
            }
        
        local halaControls =
            {
                device          = hala,
                Off             = '{\\"id\\":1,\\"method\\":\\"set_power\\", \\"params\\":[\\"off\\", \\"smooth\\", 500]}\\',
                ["Bright 25%"]  = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 25, 500]}\\',     
                ["Bright 50%"]  = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 50, 500]}\\',
                ["Bright 75%"]  = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 75, 500]}\\',
                Sunrise         = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",2,1,\\"50,2,4000,1,900000,2,4000,100\\"]}\\',
                Sunset          = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"cf\\",2,2,\\"50,2,4000,75,900000,2,4000,1\\"]}\\',
                ["4-7-8"]       = '{\\"id\\":1,\\"method\\":\\"set_scene\\",\\"params\\":[\\"cf\\",24,2,\\"4000,2,4000,30,7000,2,4000,10,8000,2,4000,1\\"]}\\',
                Baby            = '{\\"id\\":1,\\"method\\":\\"set_scene\\", \\"params\\":[\\"bright\\", 1, 500]}\\', 
                levels          = {"Off","Bright 25%","Bright 50%","Bright 75%","Sunrise","Sunset","4-7-8","Baby"},
                executable      = "/opt/bin/ncat",
                parm            = " -w2 ",
                address         = "192.168.0.105 ",
                port            = " 55443",
                background      = " &"
            } 
        
        if item.isTimer and dz.time.matchesRule("at 06:32") then               -- switch Off in the morning
            setLight(chodbaControls,"Off")
            setLight(halaControls,"Off")
        elseif item.isTimer and PIR.lastUpdate.minutesAgo > 5 and PIR.lastUpdate.minutesAgo < 15 then  -- No activity during the night ?
            if hala.level ~= halaControls.BabyLevel then setLight(halaControls,"Baby") end             -- switch to baby mode if not already there 
            if chodba.level ~= chodbaControls.BabyLevel then setLight(chodbaControls,"Baby") end      
        elseif item.name == "Světlo chodba" then                    -- triggered by manual selector change
            setLight(chodbaControls,item.levelName)      
        elseif item.name == "Světlo hala" then
            setLight(halaControls,item.levelName)                   -- triggered by manual selector change
        elseif item.name == "PIR chodba" and item.isActive then
            setLight(halaControls,"Baby")                           -- PIR see something    
            setLight(chodbaControls,"Baby")      
        else
            setLight(halaControls,"Off")      
        end
        
        -- test / debug only
        if _G.logLevel == dz.LOG_DEBUG then
            for key, value in pairs(halaControls) do
                -- logWrite(key .." => " .. tostring(value))
            end
        end
    end
}   
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

Mayki wrote: Saturday 26 January 2019 14:17 Meanwhile outside night time ...

Code: Select all

2019-01-26 13:56:47.348 Status: dzVents: Info: Handling events for: "Světlo hala", value: "Off"
2019-01-26 13:56:47.348 Status: dzVents: Info: PIRTest: ------ Start internal script: PIR test: Device: "Světlo hala (YeeLight)", Index: 19
2019-01-26 13:56:47.349 Status: dzVents: Debug: PIRTest: Processing device-adapter for Světlo chodba: RGB(W) device adapter
2019-01-26 13:56:47.349 Status: dzVents: Debug: PIRTest: Processing device-adapter for Světlo chodba: Switch device adapter
2019-01-26 13:56:47.350 Status: dzVents: Debug: PIRTest: Processing device-adapter for PIR chodba: Switch device adapter
2019-01-26 13:56:47.350 Status: dzVents: Error (2.4.11): PIRTest: An error occured when calling event handler PIR test
2019-01-26 13:56:47.350 Status: dzVents: Error (2.4.11): PIRTest: ...oticz/var/scripts/dzVents/generated_scripts/PIR test.lua:52: attempt to concatenate local 'key' (a nil value)
2019-01-26 13:56:47.350 Status: dzVents: Info: PIRTest: ------ Finished PIR test
It is my understanding that Svetlo chodba and Svetlo hala are selector switches bothe with levelnames as
Off, Bright 25%,Bright 50%,Bright 75%,Sunrise, Sunset,4-7-8 and Baby
Can you confirm that ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents for motion sensor

Post by waaren »

That explains the error.
I send the an updated script via PM as I don't want to bather everyone on the forum.
I will post the endresult here when without problems
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest