Page 1 of 2

Camera motion detection to trigger device (IMOU)

Posted: Saturday 22 April 2023 18:16
by QNTL
Hello hello,

Okay, I'll start by saying I have little knowledge in scripting.
I've tried myself, but don't seem to get things working.

What I want:
IMOU has an API, which alows to control the device. My hope was, that when it detected motion, I could trigger a device in Domoticz.
Why I had this hope? Because of this website: https://github.com/user2684/imou_life and https://github.com/user2684/imouapi.
It was written to use in HA, but on this site we want it to work in Domoticz.

I've installed the library (pip install imouapi). I also already have an APP ID and APP Secret, but I don't know what the next steps are from here on out.

So I've come here for help.
HELP :oops:

It says here (https://user2684.github.io/imouapi/usage/) that you can see the sensors. I've also entered 'python -m imouapi.cli --help' but I didn't get any further after ready that. This was the output:

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/pi/.local/lib/python3.7/site-packages/imouapi/cli.py", line 10, in <module>
    from .api import ImouAPIClient
  File "/home/pi/.local/lib/python3.7/site-packages/imouapi/api.py", line 27, in <module>
    class ImouAPIClient:
  File "/home/pi/.local/lib/python3.7/site-packages/imouapi/api.py", line 252, in ImouAPIClient
    async def async_api_deviceBaseDetailList(self, devices: list[str]) -> dict:  # pylint: disable=invalid-name
TypeError: 'type' object is not subscriptable

Does someone has this working, or is able/wanting to help me?

Re: Camera motion detection to trigger device (IMOU)

Posted: Saturday 22 April 2023 19:29
by lost
QNTL wrote: Saturday 22 April 2023 18:16 IMOU has an API, which alows to control the device. My hope was, that when it detected motion, I could trigger a device in Domoticz
Not sure that's possible to have an http request configured (for instance to trigger a virtual PIR switch in Domoticz using HTTP/JSON api). On Dahua's it's not.

Re: Camera motion detection to trigger device (IMOU)

Posted: Saturday 22 April 2023 19:35
by QNTL
In the developers console, there is a Message Push Service.

Set Your Callback Address. Imou Cloud Will Push The Corresponding Message To The Callback Address Once Triggerred

It talks about a Callback Address. Maybe that can help ?

Re: Camera motion detection to trigger device (IMOU)

Posted: Sunday 23 April 2023 7:54
by lost
QNTL wrote: Saturday 22 April 2023 19:35 Set Your Callback Address. Imou Cloud Will Push...
OK, that's still what I dislike in the early days with Dahua's consumer targeted IMOU brand: No cloud, no service! And my cams are not allowed internet access.

But as an embedded function of the cam FW, I would have been surprised to find this feature as IMOU devices are Dahua design.

Re: Camera motion detection to trigger device (IMOU)

Posted: Sunday 23 April 2023 8:25
by QNTL
So, if I understand correctly, this would be a function only for people with a subscription?
I don't have that (won't get it either) so this will never happen for me?

I thought, since it doesn't save the videos on the cloud, but only a notification, it could be free.

Re: Camera motion detection to trigger device (IMOU)

Posted: Sunday 23 April 2023 17:16
by QNTL
Okay,

Tried another route. I've installed MotionEye.
All set up on my raspberry.

When there is motion, can get notifications.
Either send a email(works), Call a webhook, Run a command or Run an end command.

I tried:

Code: Select all

http://<domoticz-ip/json.htm?type=command&param=switchlight&idx=236&switchcmd=On
But nothing happens. I do get the email when I use that simultaneous. Maybe someone can help with that :)

Re: Camera motion detection to trigger device (IMOU)

Posted: Sunday 23 April 2023 18:05
by FireWizard
Hi @QNTL,

If you are familiar with Node-RED. you may want to look at: https://flows.nodered.org/node/node-red ... hua-device

And perhaps, this may be interesting as well: https://ipcamtalk.com/threads/dahua-cam ... red.44722/

And: viewtopic.php?t=33882

Regards

Re: Camera motion detection to trigger device (IMOU)

Posted: Sunday 23 April 2023 18:53
by lost
QNTL wrote: Sunday 23 April 2023 17:16 Tried another route. I've installed MotionEye.
Problem is you'll have continuous video streams loading your LAN... This may be a concern depending on your LAN config & IP cam nb.
On my side I rely on cams embedded motion & ability to FTP captures (take care then snapshots are usually sent immediately, videos usually after configured video time and maybe more if some motion is detected for a longer time: Videos may be merged for more time => using both makes sense to be responsive).

On FTP server side I monitor cams configured subdirs by script & feed a virtual motion PIR per IP cam on domoticz side.

A bit more complex but ability to FTP snapshots/videos is probably the most common feature whatever the brand.

Re: Camera motion detection to trigger device (IMOU)

Posted: Sunday 23 April 2023 20:40
by QNTL
Hi,

I'll look into both options (whenever i've got time).
Just wanted to tell there i've found a way.

From MotionEye, you can run a command. With that, it can trigger a script, which will swith a devices with a specific IDX on.

Code: Select all

#!/usr/bin/env python3

import requests

DOMOTICZ_IP = '192.168.X.X'

DOMOTICZ_PORT = '80XX'

DEVICE_IDX = 'XX'

url = f'http://{DOMOTICZ_IP}:{DOMOTICZ_PORT}/json.htm?type=command&param=switchlight&idx={DEVICE_IDX}&switchcmd=On'

response = requests.get(url)

if response.status_code == 200:

    print('Device turned on successfully!')

else:

    print('An error occurred:', response.text)
I won't know what the impact on my LAN will be.
That is why, in the beginning of the thread, I wanted to do this in the developers settings of IMOU. So, I'm still open for suggestions :)

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 14:04
by QNTL
FireWizard wrote: Sunday 23 April 2023 18:05 Hi @QNTL,

If you are familiar with Node-RED. you may want to look at: https://flows.nodered.org/node/node-red ... hua-device

And perhaps, this may be interesting as well: https://ipcamtalk.com/threads/dahua-cam ... red.44722/

And: viewtopic.php?t=33882

Regards
The Node-RED option gives me an 'socket hang up' error.
I also tried https://github.com/nayrnet/domoticz-ipcc but it seems it is outdated and doesn't work with the current FW versiions.

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 15:04
by jvdz
I have the IMOU motion detection linked to Domoticz by using an option they have on their website: https://open.imoulife.com/consoleNew/vas/messagePush
This will send a pushmessage to the specified external-IP/port when an event occurs, which I have mapped to my Nodered Server and have a Nodered flow which listens to the mapped port for IMOU push messages.

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 15:33
by QNTL
jvdz wrote: Thursday 27 April 2023 15:04 I have the IMOU motion detection linked to Domoticz by using an option they have on their website: https://open.imoulife.com/consoleNew/vas/messagePush
This will send a pushmessage to the specified external-IP/port when an event occurs, which I have mapped to my Nodered Server and have a Nodered flow which listens to the mapped port for IMOU push messages.
I was trying this today with no luck yet. Care to write down the steps? Also, do you need a subscription to IMOU Cloud?

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 16:23
by jvdz
QNTL wrote: Thursday 27 April 2023 15:33 I was trying this today with no luck yet. Care to write down the steps? Also, do you need a subscription to IMOU Cloud?
No subscription and I have done these steps to make it safe from being hacked/port scanned.
It might be a little complex when you haven't used a reverse-proxy before, but here we go:
  • Login with your IMOU account and go to the linked page:
  • Add/Edit the Push link to link to your Domain/External IP address. eg http://111.222.333.444:1234/imou or http://Your-DynDNS-Domain-Name:1234/imou
  • Check both Alarm & Device Message for now so you can see what is information can be pushed.
  • Now Go into your router and map external port 1234 to internal IP with the same port where NGINX is running
  • Add this to the NGINX active websites config to revers-proxy only the 1234/imou requests and ignoring all others on port 1234:

    Code: Select all

    server  {
    	listen 1234;
    	server_name IMOU push messages;
    	access_log /var/log/nginx/imou.access.log;
    	error_log /var/log/nginx/imou.error.log;
    	if ($request_uri !~ ^/imou$) {return 444;}
    	location /imou {
    		if ($request_uri !~ ^/imou$) {return 444;}
    		proxy_pass http://localhost:1880/imou;
    	}
    }
    
    This example assumes that NGINX runs on the same PI as nodered, else change the proxy pass line to the target ip of the nodered server.
  • Go into Nodered and add an "HTTP IN" block into a flow and set Method POST and url /imou, and add an debug block like this:

    Code: Select all

    [{"id":"c97f88ed.b3acf8","type":"http in","z":"431ec64b.fae078","name":"POST Imou-push","url":"/imou","method":"post","upload":false,"swaggerDoc":"","x":188.1999969482422,"y":420.1999816894531,"wires":[["bb134bf4.a08848"]]},{"id":"bb134bf4.a08848","type":"debug","z":"431ec64b.fae078","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":381.20006370544434,"y":419.9999599456787,"wires":[]}]
  • Save the flow.
  • Have a look at the received message and build your logic in a formula to translate the received info into events you like to handle.
Change the 1234 port references to the port you would like to use!
Hope this makes the setup clear but else shoot! :)

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 20:45
by QNTL
Okay, yeah, great. I'm there :D

I see a lot in the debug, so now it is time for me to figuere out how to get them do do anything in domoticz. I haven't used NodeRed before (even though it is strongly suggested by some on this site).

I'll try myself first, but if I'm back, i hope you guys can help :)

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 21:12
by jvdz
Great to hear you got it working so fast!
The rest is just filtering the data in NodeRed and creating an MQTT or HTTP Json request for Domoticz which actually is the easy part as is pretty common stuff for many. ;)
You can for example trigger an Dummy Alarm switch for each defined Camera and trigger those with each movement detected so you get an Domoticz event for each movement. Just realize you could get a lot of triggers as my cams will do a lot of False-Positives when it rains and is dark and the IR is on.

Re: Camera motion detection to trigger device (IMOU)

Posted: Thursday 27 April 2023 21:59
by QNTL
jvdz wrote: Thursday 27 April 2023 21:12 Great to hear you got it working so fast!
The rest is just filtering the data in NodeRed and creating an MQTT or HTTP Json request for Domoticz which actually is the easy part as is pretty common stuff for many. ;)
You can for example trigger an Dummy Alarm switch for each defined Camera and trigger those with each movement detected so you get an Domoticz event for each movement. Just realize you could get a lot of triggers as my cams will do a lot of False-Positives when it rains and is dark and the IR is on.
Thanks. I thought It would be easier.

Again, i'll look into it since the main thing worked. The camera talks with something else then only their app.

But if it isn't to much work I would appriciate a little extra help.
I want a device in domoticz (IDX 236) triggered when I get a message with this label:
labelType: "motionAlarm"

Or point me in a good direction. I'll check back this weekend.

Thanks for all the help so far!!

Re: Camera motion detection to trigger device (IMOU)

Posted: Friday 28 April 2023 9:26
by jvdz
Something like this should do it I hope. Haven't tested it just used what I have and stripped/changed it to what you want to use:

Code: Select all

[{"id":"381fae0d.ab39b2","type":"http in","z":"d8373a16.1f28f8","name":"POST Imou-push","url":"/imou","method":"post","upload":false,"swaggerDoc":"","x":113.19999694824219,"y":570,"wires":[["e7c4ff2e.be703","488873e8.98214c"]]},{"id":"488873e8.98214c","type":"debug","z":"d8373a16.1f28f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":335.20006561279297,"y":537.7999038696289,"wires":[]},{"id":"e7c4ff2e.be703","type":"function","z":"d8373a16.1f28f8","name":"Process Post  request","func":"//  Build Domoticz MQTT message when motionAlarm\nmsgd = null;\nif (msg.payload.labelType == \"motionAlarm\" ) {\n\t// {\"command\": \"switchlight\", \"idx\": 2450, \"switchcmd\": \"On\" }\n\tmsgd = {};\n\tmsgd.payload = {};\n\tmsgd.payload.command = 'switchlight';\n\tmsgd.payload.idx = 236;\n\tmsgd.payload.switchcmd = 'On';\n}\n// Return OK for Web post request\nmsg.payload = \"ok\"; \nreturn [msg,msgd];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":359.19998931884766,"y":619.999979019165,"wires":[["488873e8.98214c","12ce5f7b.f239f1"],["54132562.63ea6c"]]},{"id":"12ce5f7b.f239f1","type":"http response","z":"d8373a16.1f28f8","name":"HTTP Answer to POST","statusCode":"","headers":{},"x":622.2000350952148,"y":601.3999423980713,"wires":[]},{"id":"54132562.63ea6c","type":"mqtt out","z":"d8373a16.1f28f8","name":"MQTT Domoticz/in","topic":"domoticz/in","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e0d1b59.fd93f48","x":606.200065612793,"y":642.7999668121338,"wires":[]},{"id":"e0d1b59.fd93f48","type":"mqtt-broker","name":"Localhost","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
It will send an Switch ON command via mqtt when it receives msg.payload.labelType == "motionAlarm". :)

Re: Camera motion detection to trigger device (IMOU)

Posted: Friday 28 April 2023 22:14
by QNTL
Hi,

Thanks for the help once more. I tried it very quick.

I've added the code, but it doesn't seem to work.
I get this:

Code: Select all

TypeError: Cannot read property 'payload' of null
I'll search for a solution and report back when I have one. If you know what is the issue, feel free to share it.
Strange that it works with your implementation.

Re: Camera motion detection to trigger device (IMOU)

Posted: Friday 28 April 2023 22:54
by jvdz
The error seems to indicate that msg,payload isn't defined which would be strange to me unless there are other packages for you than I get.
It Could be you need to test whether fieldname labelType is part of the payload content in the formula block:

Code: Select all

if (msg.payload.labelType !== undefined && msg.payload.labelType == "motionAlarm" ) {
... but you really will have to enable the debug bock and show the msg object content when the error occurs so we have the required info to debug. ;)

Re: Camera motion detection to trigger device (IMOU)

Posted: Saturday 29 April 2023 9:23
by QNTL
I've added the debug window.

Image

Whenever I don't press the 'function' node, it is fine. But when i double click on it, it shows this error code:

Image