Lorex Motion Triggers (and likely other PVR Systems)

Hey Everyone,

Hoping to give back to the community any way I can, so I thought I’d share my solution to triggering automations whenever my Lorex security system detects motion. I suspect this would work with a number of other security systems as well.

Background:
I bought this high quality Lorex system a while ago. Great product, but even with all it’s “smart” motion detection where it’s only supposed to report cars or people, I would still get a lot of false positives. Mostly shadows. Furthermore, their were no webhooks that would allow me to trigger an event on HA to do things like… Turn my porch lights on when some is checking my car door to see if it’s unlocked.

My solution:
Here is the short answer. If this generates any interest, I’m happy to get into greater detail.

One feature Lorex does have is FTP. I started by setting up an FTP server using a Home Assistant Community Add-on “FTP”. I then set up Lorex to send snapshots to the FTP whenever motion was detected.

Once files were getting dropped in the FTP, I set up a Folder Watcher pointing to the local FTP folder. With this in place, I was able to add an event trigger to any automation.

I use this trigger to send notifications to my phone. Using Automation Trigger Variables I have it send the snapshot to my phone along with an option to snooze for 8 hours if I’m getting too many notifications.

Related Features:

  • Linked porch camera to a PIR sensor at the front door. I only get notifications when both motion and the PIR are triggered. I get hardly any false positives on this camera.
  • Added an option on the notification to save the snapshot to the server. I purge the snapshots every few hours to save space. This allows me to keep anything interesting.
  • Turn on outside lights when motion is detected after sunset

Future Related Features:

  • Add action to send Snapshot to TV

Models
PVR: N841A8
Cameras: E891AB

3 Likes

Interesting approach, but seems a little overkill, couldn’t you have used either Line Crossing or Intrusion Detection Zone’s to accomplish this at the Camera level?

I wish. I went through every inch of the software (desktop and webUI version) at the camera and PVR level. While it does have zone detection, there is no way to trigger Home Assistant. I’d agree it’s overkill if there were any other way. I’d love for someone to prove me wrong with a hidden API or somthing.

“Interesting approach” I get that a lot lol. But when it works it’s not wrong! :smiley:

1 Like

What’s the model of the PVR and Cameras?

PVR: N841A8
Cameras: E891AB

VERY interested in what you are doing! In my book, your approach is not “interesting”, it’s genius! And thanks for taking the time to share it.

I’m trying to achieve the exact same thing than you. I have N842 NVR and E892 cameras, so it’s pretty close to yours. In my case, Home Assistant is running in a docker on my Synology NAS. AFAIK, you can’t have add-ons when running in Docker, but it’s easy to use the NAS with FTP.

I’m curious to know how you set up the file trigger. Could you share your config?

Thanks, I appreciate the comment. I’m not familiar with Synology NAS. I didn’t know you run docker on something like that. Very cool. If nothing else, I’m sure there are plenty of docker FTP servers.

I’m using Folder Watcher (looks like I originally said File watcher. Sorry for the confusion) for the trigger. Fairly simple to use. Just had to had to add this in the configuration.yaml

folder_watcher:
  - folder: /media/192.168.0.48/
    patterns:
      - '*.jpg'

I can’t remember if it was me or Lorex that decided to use the IP as the folder name. Ether way, that’s the how it is. Lorex will create subfolders with the date and drop .jpg s in there. You can also do video clips, but that’s not what I wanted. Once a file is dropped in the 192.168.0.48 directory, my automation is triggered (fairly quickly) using an event trigger. Lorex decides the filename as well. Latest example is “N841A8_ch2_20220518094521_E.jpg”. As you can see, it gives me the channel. This helped me trigger different things once I parsed it with some formatting.

{{ trigger.event.data.file[9:10] }}

This returns me “2” when the filename is “N841A8_ch2_20220518094521_E.jpg”
It’s too bad the filename does not indicate zones. Otherwise I could even cooler automations.

Here is my automation for this trigger. You’ll see I use some helpers to snooze notifications and only give me a max of one notification every 60 seconds. There is a lot more too it in the script I run, but it’s pretty wild in there. Let me know if you have any questions. Let me know how you make out!

alias: 2 Motion Lorex FTP Trigger
description: ''
trigger:
  - platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
condition:
  - condition: template
    value_template: >-
      {{
      (states('input_number.ch'+trigger.event.data.file[9:10]+'snooze')|float)
      <= as_timestamp(now()) }}
  - condition: template
    value_template: >-
      {{
      (states('input_number.ch'+trigger.event.data.file[9:10]+'lastmotion')|float)
      <= as_timestamp(now()) }}
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.armed
            state: 'on'
          - condition: template
            value_template: '{{ trigger.event.data.file[9:10]|int == 1 }}'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ "ch1" not in trigger.event.data.file }}'
        sequence:
          - service: script.turn_on
            target:
              entity_id: script.motionalert
            data:
              variables:
                channel: '{{ trigger.event.data.file[9:10] }}'
                filepath: >-
                  {{ trigger.event.data.path|replace('/media/', '/media/local/')
                  }}
      - conditions:
          - condition: state
            entity_id: binary_sensor.front_door_motion
            state: 'off'
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: binary_sensor.front_door_motion
                from: 'off'
                to: 'on'
            continue_on_timeout: false
            timeout: '00:00:10'
          - service: script.turn_on
            target:
              entity_id: script.motionalert
            data:
              variables:
                channel: '{{ trigger.event.data.file[9:10] }}'
                filepath: >-
                  {{ trigger.event.data.path|replace('/media/', '/media/local/')
                  }}
      - conditions:
          - condition: state
            entity_id: binary_sensor.front_door_motion
            state: 'on'
        sequence:
          - service: script.turn_on
            target:
              entity_id: script.motionalert
            data:
              variables:
                channel: '{{ trigger.event.data.file[9:10] }}'
                filepath: >-
                  {{ trigger.event.data.path|replace('/media/', '/media/local/')
                  }}
    default: []
mode: parallel
max: 10
1 Like

Wow! Thank you so much for all the detailed info!
I think I figured out the home assistant part. But I can’t make the lorex box upload snapshots to FTP. FTP is configured and Lorex says “OK” when testing the settings. But I still can’t see file uploaded when I trigger a motion on the cameras. I guess this is an issue that is better adressed on the Lorex side, I just wished the community on Lorex was as strong as on Home Assistant! :smiley:

Yeah, Lorex software is the weak link here for sure. But to be fair, they never said anything about being very customizable. They’re just selling the ready to go, out of box experience. I think they have APIs for commercial grade stuff though.

I’ll PM you some screenshots of my Lorex settings to keep this HA related.

What a creative solution. Thanks for sharing, that can definitely do the job. And the compound state of two events positive to become an event, that’s great!

Great work !!

My requirement is also pretty much similar .

I am at the early stage where I am exploring the available hardware options .

I have lights and switches fitted . I am planning to retrofit the switch module to make smart . Then looking for some motion sensor camera .

Like you have mentioned, my plan is to trigger the lights on if there is any motion detected by the camera .

I am planning for the Tuya smart module with Lorex PoE camera .

I am very new and in the learning phase .Need the expert advice on the hardware such as Switch Module and Camera .

Regards
Milan

Hi Milan,

Thank you! I’m happy to help as time permits. I’m not clear on what stage your at with this. Just let me know what you need help with. For the switch Module, this should be all set up with your Tuya integration. For the Camera, have you found the settings? Should be a web portal if you navigate to the camera’s IP.

Thanks Jscarlett5,

I am still exploring the available hardware options for best integration. I have shortlisted the Tuya and Lorex but not yet purchased. I am looking for suggestion and expert advise if these combination are great or is there any thing else I should be looking into.

Hello @jscarlett5, I’m fairly new to Home Assistant, but I am very experienced with integrating various technologies and have found your posts in this thread to be very inspiring. I have recently finished integrating most of my other devices and I’m now trying to integrate my Lorex cameras. I have a Lorex NVR Model N864A and (currently) six Model E841CA cameras.

I’ve seen several posts about how to configure Lorex cameras using the HASS Generic Camera integration. Providing the Stream Source URL was easy. However, the usual Lorex snapshot URL (that works fine in a web browser) doesn’t work for the Still Image URL because the HTTP response from that URL is not an image file.

My first workaround for this limitation, was to place static snapshots from each of my cameras in a subfolder of the /config/www folder so I could refer to them using the HASS internal HTTP server with static URL’s for those JPG files.

After seeing your posts in this thread, I am now trying to use the Lorex FTP action to put the most recent snapshot from each camera into the HASS /media folder and then use a Folder Watcher action to invoke a shell script that moves and replaces those JPG files to the appropriate /config/www folder. My first goal is just to have the most recent camera still image from the motion, rather than an unchanging static image. Later I plan to save some number of the most recent motion alerts.

Anyway, here is a summary of my current solution:

Camera snapshots get FTP’d by the NVR to the HASS Media folder as:

/media/192.168.3.169/yyyy-mm-dd/N864A6_chx_yyyymmddhhmmss_E.jpg

I have defined a Shell Command in configuration.yaml that will move the file:

shell_command:
move_camera_snapshot: ‘mv -f {{ filepath }} /config/www/cameras/latest/N864A6_ch{{ channel }}.jpg’

The camera snapshots get moved to the www folder by the Shell Command from the Folder Watcher action waiting for new files to be created:

action: shell_command.move_camera_snapshot
data:
filepath: “{{ trigger.to_state.attributes.path }}”
channel: “{{ trigger.to_state.attributes.file[9:10] }}”

Now the the most recent snapshot for a given channel (i.e. Still Image URL) can be found at:

http://hass.local:8123/local/cameras/latest/channelx.jpg

This plan mostly works, but my problem is getting the Lorex DVR to reliably FTP all of the motion snapshots for all of the cameras. I have never been able to get all six of my cameras to FTP their snapshots for every detected motion. My attempts to make the camera settings consistent with each other seems to change things, but doesn’t get my desired results.

Every time I think I have my NVR and all of my cameras settings correctly configured for FTP, “some” of the channels don’t work and it’s not always the same channels. It’s interesting that I still get motion notifications for all channels from the Lorex iPhone App, but the Folder Watcher actions only get triggered for some of them. Looking at the HASS Logbook, it doesn’t appear that the files are being sent by the Lorex FTP.

You are very correct that the Lorex software is a weak link. It does seem that the issue could be with my Lorex setup, but I’m not sure whether I’m battling Lorex bugs or just not applying the settings correctly. I have been trying a lot of different approaches for changing the settings. I have tried setting them directly via an HDMI display and mouse, via the NVR port 80 HTTP server and also via the iPhone App. When changing settings, I have been tapping the Apply button after almost every change thinking that will help, but for all I know, that may have a negative side effect.

If anyone has very reliable experience with Lorex FTP settings for multiple cameras, I would be very interested in understanding your setup. Thanks.

Very sorry for the delay. I meant to get back to you sooner. At the same time, I don’t have a lot to offer you. Lorex’s FTP setup is just extremely fickle. I changed a setting one day and snapshots stoped sending entirly. Took me hours to sort it out.

If some cams are working fine and others are not, I’ll suggest the same thing Lorex will tell you. Factory reset the camers and try again. There is likely one check box or option selected that is breaking the FTP for that camera. For me, it was the Encode mode. I changed it to H.265 thinking I might get better quality or streaming speed. Instead, it disabled the IVS and Smart Plan options. Even switching back to H.264 wouldn’t re-enable. A factory resent was needed (for the camera only). IVS is not the only place to create snapshots, but it was the only remotely reliable option I found for my devices.

Happy to share any of my settings if it would help, but I’m guessing our interfaces may be a bit different.