use of two motion sensors together in dzvents

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

Moderator: leecollings

Post Reply
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

use of two motion sensors together in dzvents

Post by kitopopo »

Hello dear friends,

I'm trying to script two motion sensors for the kitchen. The following script that I contribute works fine, but if I turn off the light manually using the switch, the "kitchen light" device logically goes off. But since the two sensors are still "on" a strange situation occurs. When the first sensor goes "off" this sensor triggers the script again and as the second sensor is still "on" for a few seconds, the result is that the kitchen light turns on for a few seconds and turns off.

Code: Select all

return {
  on = {
      
        devices = {  'movimiento cocina' , 'movimiento cocina2' }
  },
          	execute = function(domoticz, device)
          	    
	    		
    if  ((domoticz.devices('movimiento cocina').state == 'On') or (domoticz.devices('movimiento cocina2').state == 'On'))  then 
         
               domoticz.devices('Luz cocina').switchOn()
               
    end
      
-------------------------------------- APAGADO       
       
                       
    if (domoticz.devices('movimiento cocina').state == 'Off') and (domoticz.devices('movimiento cocina2').state == 'Off') and (domoticz.devices('cocina indefinido').state == 'Off') then
         
              domoticz.devices('Luz cocina').switchOff().checkFirst()
                        
                        
    elseif (domoticz.devices('movimiento cocina').state == 'Off') and (domoticz.devices('movimiento cocina2').state == 'Off') and (domoticz.devices('cocina indefinido').state == 'On') then
       
             domoticz.devices('Luz cocina').switchOff().afterSec(300)
             domoticz.devices('cocina indefinido').switchOff().afterSec(360)                    
           
    end                  
       
end
}
the other part of the script would simply be that when I press a switch that I have in the kitchen, keep the light on for 5 minutes in case I need to have it on permanently for 5 minutes.

I have tried to make a similar script with "item", but when one of the two sensors goes "off" then the kitchen light turns off and the other sensor is still "on" for a few seconds, I would like the light to continues on and that go to off when both sensors are "off"

Code: Select all

return {
  on = {
      
        devices = {  'movimiento cocina' , 'movimiento cocina2' }
  },
          	execute = function(domoticz, item)

      if (item.isDevice and item.active) then 
         
               domoticz.devices('Luz cocina').switchOn()
          
      end
      
      
      if item.state == "Off"  then

               domoticz.devices('Luz cocina').switchOff()
               
                 
      elseif item.state == "Off"  and domoticz.devices('Luz cocina').state == 'On' then -- and (domoticz.devices('cocina indefinido').state == 'On')
       
               domoticz.devices('Luz cocina').switchOff().afterSec(300).checkFirst()
               domoticz.devices('cocina indefinido').switchOff().afterSec(306).checkFirst()
            
             
             ---------- si yo la apago manualmente si el interruptor indefinido cocina = on pues apaga la luz a los 300 segundos (que ya esta apagada) y apaga el interruptor indefinido
             
      elseif item.state == "Off" and (domoticz.devices('cocina indefinido').state == 'On') and domoticz.devices('Luz cocina').state == 'Off' then
       
               domoticz.devices('Luz cocina').switchOff().afterSec(300).checkFirst()
               domoticz.devices('cocina indefinido').switchOff().afterSec(306).checkFirst()        
         
      end           
           
end
}
Could you help me improve either of these two scripts to get the desired use? Thank you very much in advance.
mgugu
Posts: 218
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: use of two motion sensors together in dzvents

Post by mgugu »

You have to enumerate all possible states taking into account manual and sensor switching then create a context variable with a logic to switch from a state to another. So at each instant you know in which state you are.
You have then to apply your actions accordingly
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

Thanks for your answer, I've been trying for a whole week with variables and in every possible way, but I can't. Could someone give an example? Thank you. @waaren , can you help me? thanks in advanced
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

Waaren is no longer available for helping us.

I typing this on my phone, so no example code from me for now.

What you need to do is get both devices. local d1 = domoticz.devices('name of pir 1') and the same for the other device. Then check if either is enabled then the ligt should be on else (both are off) turn the light off. So for example:
if d1.state or d2.state then
if not light.state then
light.switchOn()
end
else
if light.state then
light.switchOff()
end
end
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

rrozema wrote: Wednesday 06 October 2021 15:39 Waaren is no longer available for helping us.

I typing this on my phone, so no example code from me for now.

What you need to do is get both devices. local d1 = domoticz.devices('name of pir 1') and the same for the other device. Then check if either is enabled then the ligt should be on else (both are off) turn the light off. So for example:
if d1.state or d2.state then
if not light.state then
light.switchOn()
end
else
if light.state then
light.switchOff()
end
end
Thanks, i will test this idea soon. Best regards
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

If you want to do it to learn how to write scripts in dzvents, just go on, post what you've got and I'll try to help you when you get stuck.

If you just need a working solution, I suggest you have a look at my scripts Auto-On, Auto-Off, etc.
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

rrozema wrote: Thursday 07 October 2021 9:14 If you want to do it to learn how to write scripts in dzvents, just go on, post what you've got and I'll try to help you when you get stuck.

If you just need a working solution, I suggest you have a look at my scripts Auto-On, Auto-Off, etc.
Dear friend, thanks for your answers. i have my script and works correctly, but if i turn off the light manually (motion sensors are still on ) --> when one of these motion sensors goes off before the other sensor, triggers the script since one of them is still on and the light turns on for a few seconds.

I paste heare my script:

Code: Select all

return {
  on = {
       
        devices = {  'movimiento cocina' , 'movimiento cocina2' }
  },
          	execute = function(domoticz, device)
          	    
	    		
    if  ((domoticz.devices('movimiento cocina').state == 'On') or (domoticz.devices('movimiento cocina2').state == 'On'))  then 
         
               domoticz.devices('Luz cocina').switchOn()
               
    end
      
-------------------------------------- APAGADO       
       
                       
    if (domoticz.devices('movimiento cocina').state == 'Off') and (domoticz.devices('movimiento cocina2').state == 'Off') and (domoticz.devices('cocina indefinido').state == 'Off') then
         
           
                        domoticz.devices('Luz cocina').switchOff().checkFirst()
                        
                        
    elseif (domoticz.devices('movimiento cocina').state == 'Off') and (domoticz.devices('movimiento cocina2').state == 'Off') and (domoticz.devices('cocina indefinido').state == 'On') then
       
             domoticz.devices('Luz cocina').switchOff().afterSec(300)
             domoticz.devices('cocina indefinido').switchOff().afterSec(360)                    
           
    end                  
       
end
}
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

The script gets executed whenever either device changes state. I.e. when one of your motion detectors switches on, but also when one switches off.

The trick is to only do your check if your sensor is switched from off to on: the device that changed state is in the 2nd parameter, so you just need to to check for device.state == true before checking the both motion sensors. That way the execute function will do nothing anymore when the detector switches off.
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

rrozema wrote: Saturday 09 October 2021 23:58 The script gets executed whenever either device changes state. I.e. when one of your motion detectors switches on, but also when one switches off.

The trick is to only do your check if your sensor is switched from off to on: the device that changed state is in the 2nd parameter, so you just need to to check for device.state == true before checking the both motion sensors. That way the execute function will do nothing anymore when the detector switches off.
Dear friend, sorry,

I understand but i I can't get this strings to work in my script

Code: Select all

 domoticz.devices('movimiento cocina').state == true
 
  or

  domoticz.devices('movimiento cocina').state == 'true'

  or

  domoticz.devices('movimiento cocina').state == True
Thanks in advanced,

Best regards
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

Code: Select all

local M1_NAME = 'movimiento cocina'
local M2_NAME = 'movimiento cocina2'
local L_NAME = 'Luz cocina'

return {
    on = {
        devices = {  M1_NAME, M2_NAME }
    },
    execute = function(domoticz, device)
        local m1 = domoticz.devices(M1_NAME)
        local m2 = domoticz.devices(M2_NAME)
        local l = domoticz.devices(L_NAME)
        
        if nil == m1 then
            domoticz.log( 'Device ' .. M1_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == m2 then
            domoticz.log( 'Device ' .. M2_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == l then
            domoticz.log( 'Device ' .. L_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif device.active then
            if (m1.active or m2.active) then
                if not l.active then
                    l.switchOn()
                end
            end
        elseif not device.active then
            if not (m1.active or m2.active) then
                if l.active then
                    l.switchOff()
                end
            end
        end
    end
}
This should do what you want. If there's anything you don't understand: ask.
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

rrozema wrote: Sunday 10 October 2021 14:56

Code: Select all

local M1_NAME = 'movimiento cocina'
local M2_NAME = 'movimiento cocina2'
local L_NAME = 'Luz cocina'

return {
    on = {
        devices = {  M1_NAME, M2_NAME }
    },
    execute = function(domoticz, device)
        local m1 = domoticz.devices(M1_NAME)
        local m2 = domoticz.devices(M2_NAME)
        local l = domoticz.devices(L_NAME)
        
        if nil == m1 then
            domoticz.log( 'Device ' .. M1_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == m2 then
            domoticz.log( 'Device ' .. M2_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == l then
            domoticz.log( 'Device ' .. L_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif device.active then
            if (m1.active or m2.active) then
                if not l.active then
                    l.switchOn()
                end
            end
        elseif not device.active then
            if not (m1.active or m2.active) then
                if l.active then
                    l.switchOff()
                end
            end
        end
    end
}
This should do what you want. If there's anything you don't understand: ask.

Dear friend, i test your script but the script have the same problem that my script. If i turn off the light manually, when one of the motion sensors turns off and the other remains on, the light powers on some secons again.
when we get it to work I will study it and ask you some questions if possible to finish understanding it


Thanks in advanced,
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

That can't be: read the script and you'll see it only switches the light on when a motion sensor switches from off to on. You must have something else that switches the light on or your sensor switches to on when you don't expect it.

Code: Select all

local M1_NAME = 'movimiento cocina'
local M2_NAME = 'movimiento cocina2'
local L_NAME = 'Luz cocina'

return {
    on = {
        devices = {  M1_NAME, M2_NAME }
    },
    logging = {
      level = domoticz.LOG_INFO,
--   level = domoticz.LOG_DEBUG,
    },    
    execute = function(domoticz, device)
        local m1 = domoticz.devices(M1_NAME)
        local m2 = domoticz.devices(M2_NAME)
        local l = domoticz.devices(L_NAME)
        
        if nil == m1 then
            domoticz.log( 'Device ' .. M1_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == m2 then
            domoticz.log( 'Device ' .. M2_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == l then
            domoticz.log( 'Device ' .. L_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif device.active then
            domoticz.log( 'Device ' .. device.name .. ' switched on.', domoticz.LOG_INFO)
            if (m1.active or m2.active) then
                if not l.active then
                    l.switchOn()
                end
            end
        elseif not device.active then
            domoticz.log( 'Device ' .. device.name .. ' switched off.', domoticz.LOG_INFO)
            if not (m1.active or m2.active) then
                if l.active then
                    l.switchOff()
                end
            end
        end
    end
}
I've added 2 informational messages to help you find what's going on, plus an additional section "logging" to enable the informational messages to be shown in the log.
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

rrozema wrote: Monday 11 October 2021 20:57 That can't be: read the script and you'll see it only switches the light on when a motion sensor switches from off to on. You must have something else that switches the light on or your sensor switches to on when you don't expect it.

Code: Select all

local M1_NAME = 'movimiento cocina'
local M2_NAME = 'movimiento cocina2'
local L_NAME = 'Luz cocina'

return {
    on = {
        devices = {  M1_NAME, M2_NAME }
    },
    logging = {
      level = domoticz.LOG_INFO,
--   level = domoticz.LOG_DEBUG,
    },    
    execute = function(domoticz, device)
        local m1 = domoticz.devices(M1_NAME)
        local m2 = domoticz.devices(M2_NAME)
        local l = domoticz.devices(L_NAME)
        
        if nil == m1 then
            domoticz.log( 'Device ' .. M1_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == m2 then
            domoticz.log( 'Device ' .. M2_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == l then
            domoticz.log( 'Device ' .. L_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif device.active then
            domoticz.log( 'Device ' .. device.name .. ' switched on.', domoticz.LOG_INFO)
            if (m1.active or m2.active) then
                if not l.active then
                    l.switchOn()
                end
            end
        elseif not device.active then
            domoticz.log( 'Device ' .. device.name .. ' switched off.', domoticz.LOG_INFO)
            if not (m1.active or m2.active) then
                if l.active then
                    l.switchOff()
                end
            end
        end
    end
}
I've added 2 informational messages to help you find what's going on, plus an additional section "logging" to enable the informational messages to be shown in the log.
Dear friend, sorry, I ask a thousand pardons.

I have my old script + your script enabled. This was the reason why it did not work. I have a very basic level of dzvents. there are several things that are not clear to me, i am trying to learn more:

1)

Code: Select all

if nil == m1 then
            domoticz.log( 'Device ' .. M1_NAME .. ' not found.', domoticz.LOG_ERROR)
elseif nil == m2 then
            domoticz.log( 'Device ' .. M2_NAME .. ' not found.', domoticz.LOG_ERROR)
elseif nil == l then
            domoticz.log( 'Device ' .. L_NAME .. ' not found.', domoticz.LOG_ERROR)
This first part is only for information in the registry of dzvents? Why does nil compare with the 3 devices?

2)

Code: Select all

elseif device.active then
"device.active" which device are you referring to? to any of the 2 motion sensors? this is necessary if after go "if (m1.active or m2.active)"?

3) I did not know the phrase "if not" , it is interesting

Thanks in advanced. Best regards
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

Those are very good questions!

1 - you're asking domoticz for 3 devices in your script. If for some reason at some point in the future any of those devices are not/no longer available in domoticz, the call to domoticz.devices( <device name/idx> ) will return nil. By checking in the script and printing in the log an error if any of the devices was not found, you'll save yourself a lot of time in the future when you find the script suddenly no longer works.

2 - This is actually 2 questions in one:
2a : device in this context is the 2nd parameter passed into the execute() function. dzVents fills it with the item that caused the execute() function to get called. In this case: if M1 changed state, device will be the M1 device and if M2 changed state, device will be the M2 device.
2b device.active is an attribute defined for all device types. It is true when the device is On, Open, Active, etc depending on the device type. Please see https://www.domoticz.com/wiki/DzVents:_ ... ll_devices for more information.

3 - That is the logical NOT operator: https://www.tutorialspoint.com/lua/lua_ ... rators.htm used in an if expression. It simply negates the result of the expression after it.
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

rrozema wrote: Tuesday 12 October 2021 17:25 Those are very good questions!

1 - you're asking domoticz for 3 devices in your script. If for some reason at some point in the future any of those devices are not/no longer available in domoticz, the call to domoticz.devices( <device name/idx> ) will return nil. By checking in the script and printing in the log an error if any of the devices was not found, you'll save yourself a lot of time in the future when you find the script suddenly no longer works.

2 - This is actually 2 questions in one:
2a : device in this context is the 2nd parameter passed into the execute() function. dzVents fills it with the item that caused the execute() function to get called. In this case: if M1 changed state, device will be the M1 device and if M2 changed state, device will be the M2 device.
2b device.active is an attribute defined for all device types. It is true when the device is On, Open, Active, etc depending on the device type. Please see https://www.domoticz.com/wiki/DzVents:_ ... ll_devices for more information.

3 - That is the logical NOT operator: https://www.tutorialspoint.com/lua/lua_ ... rators.htm used in an if expression. It simply negates the result of the expression after it.
Thank you very much for your answers and your time and dedication, now everything is clearer to me, I love to continue learning. I have one last question. is there any way to deny just .active? that is, the opposite of ".active" without using "if not". For example "m1.notactive" or something similar.

the question comes because I am trying to modify the script but it does not work quite as I want since I think I deny the whole sentence completely.

" if not (m1.active or m2.active) and (domoticz.devices('cocina indefinido').state == 'Off') then"

Code: Select all

local M1_NAME = 'movimiento cocina'
local M2_NAME = 'movimiento cocina2'
local L_NAME = 'Luz cocina'

return {
    on = {
        devices = {  M1_NAME, M2_NAME }
    },
    logging = {
      level = domoticz.LOG_INFO,
--   level = domoticz.LOG_DEBUG,
    },    
    execute = function(domoticz, device)
        
        local m1 = domoticz.devices(M1_NAME)
        local m2 = domoticz.devices(M2_NAME)
        local l = domoticz.devices(L_NAME)
        
        if nil == m1 then
            domoticz.log( 'Device ' .. M1_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == m2 then
            domoticz.log( 'Device ' .. M2_NAME .. ' not found.', domoticz.LOG_ERROR)
        elseif nil == l then
            domoticz.log( 'Device ' .. L_NAME .. ' not found.', domoticz.LOG_ERROR)
        
    
        elseif device.active then
            domoticz.log( 'Device ' .. device.name .. ' switched on.', domoticz.LOG_INFO)
            
            if (m1.active or m2.active) and (domoticz.devices('cocina indefinido').state == 'Off') then
                if not l.active then
                    l.switchOn()
                end
            end
        elseif not device.active then
            domoticz.log( 'Device ' .. device.name .. ' switched off.', domoticz.LOG_INFO)
            if not (m1.active or m2.active) and (domoticz.devices('cocina indefinido').state == 'Off') then
                if l.active then
                    l.switchOff()
                end
            end
    
        if not ((m1.active or m2.active) and (domoticz.devices('cocina indefinido').state == 'On')) then
       -------- I tried to else if not"
       
             l.switchOff().afterSec(300)
             domoticz.devices('cocina indefinido').switchOff().afterSec(302)                    
           
        end  
        
    end
    
  end    

}

"cocina indefinido" is a simple switch that when I put it in "on" that keeps the kitchen light on for 5min and then goes to "off" and turns off the light.
but it does not quite work well, I will do more tests. Thanks again and best regards
kitopopo
Posts: 59
Joined: Monday 03 December 2018 11:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: use of two motion sensors together in dzvents

Post by kitopopo »

I think I found the answer. "inActive: Boolean. 3.1.0 true if active is false and vice versa. If active is nil inActive is also nil", I'll do more tests, thank you very much
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: use of two motion sensors together in dzvents

Post by rrozema »

"<device>.inActive" is a good option too. It will give you exactly the same result as "not <device>.active" (for most device types).

To answer your doubts: "not" operates on the expression after it. So "not x" where x is false will result in true and if x is true it will result in false. not works just on the one expression after it. So "not x or y" is true when x is false or y is true (i.e. only x is affected by the logical not). BUT, if you enclose an expression in ( ), you effectively group anything inside those ( ) into one expression. so "not (x or y)" evaluates to true if both x and y are false and to false in all other cases, because (x or y) evaluates to true if either of or both x and y are true.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests