Page 1 of 1

two motion sensors together to active notification - solved

Posted: Thursday 21 April 2022 9:48
by nalivikam
Hello!
I'm new and I'm still learning, but there's a lot of information and I can't find what I need.
I'm looking for a script for the notification to be sent to me only when there are 2 motion sensors that are active at the same time.
If sensor1 is activated and there is no movement on sensor2 for up to 3 seconds, do not send me a notification. If both sensors are activated, only then will a notification arrive. In this way I want to reduce the false effects of the sensors.
Tnx!

Re: two motion sensors together to active notification

Posted: Thursday 21 April 2022 10:58
by heggink
The challenge is probably that your motion sensors may switch off within the 3 second window so you need some way of tracking that either sensor was on within the 3 second window. dzvents has a lastupdate attribute but there appear to be (?) issues with that for python plugins + it does not indicate what the last state is.
One way to go about it is to have 2 dummy motion sensors which get switched on if the corresponding motion sensor gets switched on. If you then set the off delay in the configuation of the dummy sensor to 3 seconds, it will stay on for 3 seconds. You can then wite a simple script that gets triggered if both are on. You also need a script that switches the dummy on if one of the motion sensors is on.
This would be one way to do this.

Re: two motion sensors together to active notification

Posted: Thursday 21 April 2022 11:54
by thomasbaetge
I did something similar with node-red by using a join node that will trigger the alarm, if 2 motions are detected in a certain period of time (to avoid false positives).

Re: two motion sensors together to active notification

Posted: Thursday 21 April 2022 11:59
by nalivikam
I would like to ask someone if they can help me with the code? I tried to search for topics for similar projects already done, but I did not find exactly such code.
I ask for help!

Re: two motion sensors together to active notification

Posted: Thursday 21 April 2022 13:47
by heggink
Try this (NOT TESTED!!!):
Say your 2 motion sensors are called M1 and M2
Create dummy hardware under hardware
Then create 2 switch devices under that dummy hardware and call them DM1 and DM2
Go to the switches tab and edit first DM1 and then DM2 as follows: enter 3 in the off delay field and save
You now have the 2 dummy devices that will switch off after 3 seconds when switched on.
Now let's write a simple dzvents script that switches them on if the motion sensor is triggered:

Code: Select all

return {
    on = {
            devices = { 'M1', 'M2' }
    },
    execute = function(dz, dev)
        if (dev.active) then
            if (dev.name == 'M1') then
                dz.devices('DM1').switchOn()
            else
                dz.devices('DM2').switchOn()
            end
        end
    end
}
You now have a script that switches the dummy devices on if based on the motion sensors

Now the script that triggers if both are on:

Code: Select all

return {
    on = {
            devices = { 'DM1', 'DM2' }
    },
    execute = function(dz, dev)
        if (dev.state == 'On' and dz.devices('DM1').state == 'On' and dz.devices('DM1').state == 'On') then
--            do whatever you need to do
        end
    end
}
This is just one very simple approach. There are many more (sophisticated) ways to do this. You can remane DM1 and DM2 to $DM1 and $DM2 (everywhere!!) and then they will not show up in the devices list.

Re: two motion sensors together to active notification

Posted: Friday 22 April 2022 19:34
by nalivikam
heggink wrote: Thursday 21 April 2022 13:47 Try this (NOT TESTED!!!):
Say your 2 motion sensors are called M1 and M2
Create dummy hardware under hardware
Then create 2 switch devices under that dummy hardware and call them DM1 and DM2
Go to the switches tab and edit first DM1 and then DM2 as follows: enter 3 in the off delay field and save
You now have the 2 dummy devices that will switch off after 3 seconds when switched on.
Now let's write a simple dzvents script that switches them on if the motion sensor is triggered:

Code: Select all

return {
    on = {
            devices = { 'M1', 'M2' }
    },
    execute = function(dz, dev)
        if (dev.active) then
            if (dev.name == 'M1') then
                dz.devices('DM1').switchOn()
            else
                dz.devices('DM2').switchOn()
            end
        end
    end
}
You now have a script that switches the dummy devices on if based on the motion sensors

Now the script that triggers if both are on:

Code: Select all

return {
    on = {
            devices = { 'DM1', 'DM2' }
    },
    execute = function(dz, dev)
        if (dev.state == 'On' and dz.devices('DM1').state == 'On' and dz.devices('DM1').state == 'On') then
--            do whatever you need to do
        end
    end
}
This is just one very simple approach. There are many more (sophisticated) ways to do this. You can remane DM1 and DM2 to $DM1 and $DM2 (everywhere!!) and then they will not show up in the devices list.
Thank you very much for your time and help with the code!
But I think I still have a problem. Something is wrong. Maybe I'm mistaken somewhere or?

https://youtu.be/bbm3YOpMLfk

Each works individually and sends a notification. They are still independent of each other.
I made a video so you can better see how they work.
Have I missed an option somewhere to choose?

Re: two motion sensors together to active notification

Posted: Monday 09 May 2022 22:47
by nalivikam
I lost hope today!
I thought someone could help, but it took so long and no one could help me!
Every day I check if someone has given a working code, but no one has written. How to make the code so that the notification works only when there are 2 sensors activated.

Re: two motion sensors together to active notification

Posted: Tuesday 10 May 2022 13:19
by plugge
nalivikam wrote: Monday 09 May 2022 22:47 I lost hope today!
I thought someone could help, but it took so long and no one could help me!
Every day I check if someone has given a working code, but no one has written. How to make the code so that the notification works only when there are 2 sensors activated.
Could you try this: (Tested with dummy light switches)
(Edit1: simpler version. Edit2: forgot to 'or')

Code: Select all

return {
	on = {
		devices = {'Sensor1',
		           'Sensor2'},
	},
	data = {},
	logging = {},

	execute = function(dz, trigger)

	 if trigger.isDevice and
            (dz.devices('Sensor1').active or
             dz.devices('Sensor2').active) and
            (dz.devices('Sensor1').lastUpdate.secondsAgo < 4 or
             dz.devices('Sensor2').lastUpdate.secondsAgo < 4 )
         then
            print('================> Both sensors ON within 3 seconds window')
         end
	end
}

Re: two motion sensors together to active notification

Posted: Wednesday 11 May 2022 5:37
by nalivikam
plugge wrote: Tuesday 10 May 2022 13:19
nalivikam wrote: Monday 09 May 2022 22:47 I lost hope today!
I thought someone could help, but it took so long and no one could help me!
Every day I check if someone has given a working code, but no one has written. How to make the code so that the notification works only when there are 2 sensors activated.
Could you try this: (Tested with dummy light switches)
(Edit1: simpler version. Edit2: forgot to 'or')

Code: Select all

return {
	on = {
		devices = {'Sensor1',
		           'Sensor2'},
	},
	data = {},
	logging = {},

	execute = function(dz, trigger)

	 if trigger.isDevice and
            (dz.devices('Sensor1').active or
             dz.devices('Sensor2').active) and
            (dz.devices('Sensor1').lastUpdate.secondsAgo < 4 or
             dz.devices('Sensor2').lastUpdate.secondsAgo < 4 )
         then
            print('================> Both sensors ON within 3 seconds window')
         end
	end
}
Hello! First of all, let me express my gratitude for the help you are giving me!

But I still can't stop notifications coming from 1 device.
I followed your code, but maybe I'm missing something. Someone option may need to be included or the path of the code to specify?
I don't know where the mistake is. The code does not generate any error, but still receives notification from only one sensor / switch.

Would you look at the video I made so we could finish the project?!
This is my setting and script => https://youtu.be/nVjCk2Sbmps

This step is very important to me!
Thanks!

Re: two motion sensors together to active notification

Posted: Monday 16 May 2022 16:25
by heggink
There was a typo in my second script where it chedcked for DM1 twice instead of testing for DM1 and DM2:

Code: Select all

return {
    on = {
            devices = { 'DM1', 'DM2' }
    },
    execute = function(dz, dev)
        if (dev.state == 'On' and dz.devices('DM1').state == 'On' and dz.devices('DM2').state == 'On') then
--            do whatever you need to do
        end
    end
}
I tested it myself and it works.

Re: two motion sensors together to active notification

Posted: Monday 16 May 2022 18:58
by nalivikam
to understand! now I saw the mistake too, but I think it should be as it is written. so I just copied it.

changed DM1 to DM2. just like you say change it!
upload a screenshot to take a look please.
dm1 dm2.png
dm1 dm2.png (141.46 KiB) Viewed 1807 times



please take a look at my settings as well. do I have to describe the path of the script somewhere or something else?
I still receive notifications from only one sensor.
looking at the logos there is no mistake. maybe I missed something somewhere. if I have to shoot somewhere else some setting?
d11.png
d11.png (149.91 KiB) Viewed 1807 times
d22.png
d22.png (63.47 KiB) Viewed 1807 times

Re: two motion sensors together to active notification

Posted: Monday 16 May 2022 18:59
by nalivikam
more 3 pics
d33.png
d33.png (73.59 KiB) Viewed 1807 times
d44.png
d44.png (64.61 KiB) Viewed 1807 times

Re: two motion sensors together to active notification  [Solved]

Posted: Monday 16 May 2022 20:20
by besix
Your mistake is activating notifications for each switch.
You need to add a notification in the script, or add a third virtual switch that will turn on when the first two are ON
Then the third switch may have an active notification
DM1 = no notifications
DM2 = no notifications
DM3 = notification but then DM1 and DM2 = ON
or This script is what colleagues suggest but

Code: Select all

return {
    on = {
            devices = { 'DM1', 'DM2' }
    },
    execute = function(dz, dev)
        if (dev.state == 'On' and dz.devices('DM1').state == 'On' and dz.devices('DM2').state == 'On') then
        dz.notify('Two switch open', dz.PRIORITY_HIGH)
        end
    end
}

Re: two motion sensors together to active notification

Posted: Monday 16 May 2022 20:29
by heggink
Exactly! In the script, replace:

Code: Select all

--            do whatever you need to do
with something like:

Code: Select all

            dz.notify('Domoticz notification', 'Both sensors were active', dz.PRIORITY_HIGH)
That should be enough. Get rid of the other notifications.
Find all the info here: https://www.domoticz.com/wiki/DzVents:_ ... terface.29

Re: two motion sensors together to active notification - solved

Posted: Thursday 19 May 2022 7:07
by nalivikam
Thanks for all !!!
Finally my problem is fixed.

Re: two motion sensors together to active notification - solved

Posted: Thursday 19 May 2022 10:08
by heggink
Wonderful. Hope you learnt some new (dzvents) skills in the process ;-).

Re: two motion sensors together to active notification - solved

Posted: Thursday 19 May 2022 22:10
by nalivikam
heggink wrote: Thursday 19 May 2022 10:08 Wonderful. Hope you learnt some new (dzvents) skills in the process ;-).
Of course! That I'm going to hit my head a lot.
Good thing it was you. If it weren't for you, I wouldn't be able to accomplish my task. Every day I hoped someone would help. Be good people and help people like me.
God bless you! :)