two motion sensors together to active notification - solved  [Solved]

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

Moderator: leecollings

Post Reply
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

two motion sensors together to active notification - solved

Post 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!
Last edited by nalivikam on Thursday 19 May 2022 7:05, edited 1 time in total.
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: two motion sensors together to active notification

Post 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.
Docker in Truenas scale, close to latest beta
DASHTICZ ๐Ÿ™ƒ
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
thomasbaetge
Posts: 153
Joined: Wednesday 02 October 2019 11:47
Target OS: Linux
Domoticz version: 2023.1
Location: DE / BY / LT
Contact:

Re: two motion sensors together to active notification

Post 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).
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification

Post 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!
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: two motion sensors together to active notification

Post 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.
Docker in Truenas scale, close to latest beta
DASHTICZ ๐Ÿ™ƒ
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification

Post 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?
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification

Post 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.
plugge

Re: two motion sensors together to active notification

Post 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
}
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification

Post 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!
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: two motion sensors together to active notification

Post 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.
Docker in Truenas scale, close to latest beta
DASHTICZ ๐Ÿ™ƒ
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification

Post 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 1794 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 1794 times
d22.png
d22.png (63.47 KiB) Viewed 1794 times
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification

Post by nalivikam »

more 3 pics
d33.png
d33.png (73.59 KiB) Viewed 1794 times
d44.png
d44.png (64.61 KiB) Viewed 1794 times
Attachments
d55.png
d55.png (215.83 KiB) Viewed 1794 times
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

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

Post 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
}
Last edited by besix on Monday 16 May 2022 20:36, edited 2 times in total.
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: two motion sensors together to active notification

Post 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
Docker in Truenas scale, close to latest beta
DASHTICZ ๐Ÿ™ƒ
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification - solved

Post by nalivikam »

Thanks for all !!!
Finally my problem is fixed.
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: two motion sensors together to active notification - solved

Post by heggink »

Wonderful. Hope you learnt some new (dzvents) skills in the process ;-).
Docker in Truenas scale, close to latest beta
DASHTICZ ๐Ÿ™ƒ
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
nalivikam
Posts: 18
Joined: Sunday 20 March 2022 4:32
Target OS: Linux
Domoticz version:
Contact:

Re: two motion sensors together to active notification - solved

Post 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! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest