Hikvision - Line Crossing Trigger - Binary Sensor Activates but automation is not run

Hi all. I’d appreciate a second set of eyes on this. I have a Hikvision NVR system and have a line crossing motion trigger setup and integrated it into HA. HA receives reports of movement correctly but I just can’t get the automation to run. As it’s a binary sensor, I’m aware it is either off or on, even though the HA interface shows ‘clear’ or ‘detected’, but for the life of me, I just can’t seem to get it to then run my automation.

configuration.yaml

binary_sensor:
  platform: hikvision
  host: 192.168.***.***
  username: admin
  password: ********
  customize:
    binary_sensor.network_video_recorder_line_crossing_2:
      delay: 0

automation.yaml

- alias: CCTV Motion Detected
  trigger:
  - platform: state
    entity_id: binary_sensor.network_video_recorder_line_crossing_2
    from: 'off'
    to: 'on'
  condition:
    - condition: sun
      after: sunset
      after_offset: "-00:40:00"
    - condition: sun
      before: sunrise
      before_offset: "-0:30:00"
  action:
  - service: light.turn_on
    data:
      entity_id: group.outside
      brightness: 255
  - delay: '00:00:15'
  - service: light.turn_on
    data:
      entity_id: group.outside
      brightness: 255

(I have the light turn on twice because sometimes the hue lights don’t respond - though I’m sure there’s an easier FOR and LOOP test).

I’ve also tried:

    entity_id: binary_sensor.network_video_recorder_line_crossing_2
    to: 'on'
    entity_id: binary_sensor.network_video_recorder_line_crossing_2
    to: 'On'

Any ideas? As always your input is much appreciated.

You need to look at the entity in the states page to tell you what states are reported. It just might be that you really do need to use ‘detected’ as the state to trigger the automation.

check to make sure the state for the binary_sensor you’re using changes, too. I don’t see why it wouldn’t be since it’s showing up in the interface but just as a dummy check. :grinning:

also maybe the automation is actually triggering but the action you think should be happening isn’t being taken. check the log to see if the automation triggers.

Thanks for taking the time to look and respond to this. In states I have:

So I was assuming all lower case ‘on’ and ‘off’…

Nothing in the Logbook about the automation running…

ok - So I took out the time conditions and it runs - so it’s got to be something down to sunrise/sunset. I’ll continue playing. Thanks.

You need OR in the condition, example from the manual:

condition:
  condition: or
  conditions:
    - condition: state
      entity_id: 'device_tracker.paulus'
      state: 'home'
    - condition: numeric_state
      entity_id: 'sensor.temperature'
      below: '20'

:slight_smile:

You know I really thought I was getting to understand this programming… Thanks for your assistant. From what I understand then, triggers work on OR, so for each of my sensors I could have just one automation - would that be correct?

e.g.

  trigger:
  - platform: state
    entity_id: binary_sensor.network_video_recorder_line_crossing_2
    to: 'on'
  - platform: state
    entity_id: binary_sensor.network_video_recorder_line_crossing_1
    to: 'on'
  - platform: state
    entity_id: binary_sensor.network_video_recorder_motion_2
    to: 'on'

if either of these activated, then the automation would run?

Think so, never tried but seems correct :slight_smile:

Yes, the triggers are OR. So are the conditions unless specified AND.

This works perfect for me

  condition:
  - condition: or
    conditions:
      - condition: time
        before: '05:00:00'
      - condition: time
        after: '22:30:00'

With times that cross midnight, this works perfectly

But for yours this should work

  condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-00:40:00"
      - condition: sun
        before: sunrise
        before_offset: "-0:30:00"

Thanks everyone. Been testing with having it active after sunrise and before sunset and it’s coming on now as expected. Now to amend this the other way around and wait for this evening. Thanks for all your help. You’ve saved my sanity for another day.

do you have only one camera and 1 NVR? I have 4 camera and 1 NVR, you know how to get 4 line crossing from NVR?

why you put delay at 0?

I have 4 cameras - 1 NVR. The line recorder only triggers to ‘on’ for less than a second and I read somewhere that the default was 5 seconds in which it had to be at the particular state before notifying HA. Perhaps I misread…

Ah yes, well then you should put higher value then zero … no?

I’ve been working on it for a few days and my brain has turned into mush. At the moment, the sensors activate perfectly. I’ll fine tune it once it works after testing this evening, but at the moment, as the sensor is reacting as I would like, I’m avoiding changing it…