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

2 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

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.