Home Assistant: Using history stats sensor to count persons during night

For a while I’ve been trying to figure out a proper way to create automation that counts all persons during a night and shows it on a display first thing in the morning. This way I can quickly see early in the morning if someone has been ‘visiting’ my yard without a permission.

This is done through two phases: Triggering the night mode and using history stats sensor to display the values

Of course for this automation a supported hardware is needed. I’m using Unifi G4 dome camera and Unifi Protect software (UDM Pro) that supports object detection out of the box. This automation should also be usable also with another cameras (e.g. Frigate + Coral TPU) with a minor changes.

Dynamic night mode trigger

First I wanted the night mode to be triggered dynamically. Meaning that there’s no specific time frame that the night mode is enabled or disabled.

Enabling the night mode is now done by specific time at 21.00.. at least until I find a better trigger for it.

Disabling the night mode is done dynamically. It is being disabled, if front door is opened between 6.00-8.00 (when I’m going to work) or latest when the clock reaches 8.00.

So, for starters a input_boolean entity is needed to be created for night mode to be on or off:

input_boolean:
  night_mode:
    name: Night Mode

Along with that, a couple of helpers are needed to get trigger times for future purposes that we are using with the history_stats sensor

input_datetime:
  night_mode_last_trigger_on:
    name: "Night mode last triggered on"
    has_date: true
    has_time: true
  night_mode_last_trigger_off:
    name: "Night mode last triggered off"
    has_date: true
    has_time: true

Ok, now the input switch for the night mode and helpers are set up. Next thing is to trigger the night mode on and off through pre-defined conditions. This is what I’m currently using, so modify it on your needs.. basically

automation:
  - id: 'set_night_mode_on'
    alias: 'Utility: Mode: Night Mode: On'
    description: 'Sets night mode on specific time 21.00'
    trigger:
    - platform: time
      at: '21:00:00'
    condition: []
    action:
    - service: input_boolean.turn_on
      data: {}
      target:
        entity_id: input_boolean.night_mode
    mode: single
  - id: 'set_night_mode_off'
    alias: 'Utility: Mode: Night Mode: Off'
    description: 'Sets night mode off at 8.00 or when front door is opened between 6.00-8,00'
    trigger:
    - platform: time
      at: 08:00:00
    - type: opened
      platform: device
      device_id: 3640deeeabea43419c26c1e06aa3258f
      entity_id: binary_sensor.door_sensor_front_door
      domain: binary_sensor
      id: trigger_front_door_open
    condition:
    - condition: or
      conditions:
      - condition: and
        conditions:
        - condition: trigger
          id: trigger_front_door_open
        - condition: time
          after: 06:00:00
          before: 08:00:00
      - condition: not
        conditions:
        - condition: trigger
          id: trigger_front_door_open
    action:
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.night_mode
    mode: single

And one final thing to do is to update time helper values when the night mode is being changed

automation:
  - alias: Night mode on last date and time
    description: 'Sets night mode helper values'
    trigger:
      platform: state
      entity_id: input_boolean.night_mode
      from: 'off'
      to: 'on'
    action:
      - service: input_datetime.set_datetime
        data_template:
          entity_id: input_datetime.night_mode_last_trigger_on
          time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
          date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
  - alias: Night mode off last date and time
    trigger:
      platform: state
      entity_id: input_boolean.night_mode
      from: 'on'
      to: 'off'
    action:
      - service: input_datetime.set_datetime
        data_template:
          entity_id: input_datetime.night_mode_last_trigger_off
          time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
          date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'

Counting persons

Now we need to use the history stats sensor to count all persons between time values stored in helpers night_mode_last_trigger_on and night_mode_last_trigger_off.

Of course one exception, if night mode is still on, we should count the events to current timestamp instead of last_trigger_off value!

sensor:
  - platform: history_stats
    name: Security events during night mode
    entity_id: sensor.front_yard_detected_object
    state: person
    type: count
    start: "{{ states.input_datetime.night_mode_last_trigger_on.attributes.timestamp }}"
    end: >-
      {% if is_state('input_boolean.night_mode', 'off') %}
      {{ states.input_datetime.night_mode_last_trigger_off.attributes.timestamp }}
      {% else %}
      {{ as_timestamp(now()) }}
      {% endif %}

Conclusion and what next…

So far so good! Now I can easily check number of unwanted visitors on the front yard once I wake up or even if the night mode is still on!

To make upgrades for this, I still need to count also cars during night and use multiple cameras.

Person(s) and car(s) as seen through Unifi G4 Camera

Leave a Reply

Your email address will not be published.