{"id":1005,"date":"2022-05-06T08:31:05","date_gmt":"2022-05-06T08:31:05","guid":{"rendered":"https:\/\/www.creatingsmarthome.com\/?p=1005"},"modified":"2022-05-06T08:31:05","modified_gmt":"2022-05-06T08:31:05","slug":"home-assistant-using-history-stats-sensor-to-count-persons-during-night","status":"publish","type":"post","link":"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/05\/06\/home-assistant-using-history-stats-sensor-to-count-persons-during-night\/","title":{"rendered":"Home Assistant: Using history stats sensor to count persons during night"},"content":{"rendered":"\n<p>For a while I&#8217;ve been trying to figure out a proper way to create automation that <strong>counts all persons during a night and shows it on a display first thing in the morning<\/strong>. This way I can quickly see early in the morning if someone has been &#8216;visiting&#8217; my yard without a permission.<\/p>\n\n\n\n<p>This is done through two phases: Triggering the night mode and using history stats sensor to display the values<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"132\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-06-at-11.19.49.png\" alt=\"\" class=\"wp-image-1020\"\/><\/figure>\n\n\n\n<p>Of course for this automation a <strong>supported hardware is needed<\/strong>. I&#8217;m using <a rel=\"noreferrer noopener\" href=\"https:\/\/store.ui.com\/products\/uvc-g4-dome\" data-type=\"URL\" data-id=\"https:\/\/store.ui.com\/products\/uvc-g4-dome\" target=\"_blank\">Unifi G4 dome camera<\/a> and Unifi Protect software (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/01\/29\/review-ubiquiti-dream-machine-pro-udm-pro-one-year-review\/\" data-type=\"URL\" data-id=\"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/01\/29\/review-ubiquiti-dream-machine-pro-udm-pro-one-year-review\/\" target=\"_blank\">UDM Pro<\/a>) that supports object detection out of the box. This automation should also be usable also with another cameras (e.g. <a rel=\"noreferrer noopener\" href=\"https:\/\/frigate.video\" target=\"_blank\">Frigate<\/a> + <a rel=\"noreferrer noopener\" href=\"https:\/\/coral.ai\/products\/\" target=\"_blank\">Coral TPU<\/a>) with a minor changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic night mode trigger<\/h2>\n\n\n\n<p>First I wanted the night mode to be triggered dynamically. Meaning that there&#8217;s no specific time frame that the night mode is enabled or disabled. <\/p>\n\n\n\n<p>Enabling the night mode is now done by specific time at 21.00.. at least until I find a better trigger for it. <\/p>\n\n\n\n<p>Disabling the night mode is done dynamically. It is being disabled, if front door is opened between 6.00-8.00 (when I&#8217;m going to work) or latest when the clock reaches 8.00. <\/p>\n\n\n\n<p>So, for starters a input_boolean entity is needed to be created for night mode to be on or off:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input_boolean:\n  night_mode:\n    name: Night Mode<\/code><\/pre>\n\n\n\n<p>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<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input_datetime:\n  night_mode_last_trigger_on:\n    name: \"Night mode last triggered on\"\n    has_date: true\n    has_time: true\n  night_mode_last_trigger_off:\n    name: \"Night mode last triggered off\"\n    has_date: true\n    has_time: true<\/code><\/pre>\n\n\n\n<p>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&#8217;m currently using, so modify it on your needs.. basically <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>automation:\n  - id: 'set_night_mode_on'\n    alias: 'Utility: Mode: Night Mode: On'\n    description: 'Sets night mode on specific time 21.00'\n    trigger:\n    - platform: time\n      at: '21:00:00'\n    condition: &#91;]\n    action:\n    - service: input_boolean.turn_on\n      data: {}\n      target:\n        entity_id: input_boolean.night_mode\n    mode: single\n  - id: 'set_night_mode_off'\n    alias: 'Utility: Mode: Night Mode: Off'\n    description: 'Sets night mode off at 8.00 or when front door is opened between 6.00-8,00'\n    trigger:\n    - platform: time\n      at: 08:00:00\n    - type: opened\n      platform: device\n      device_id: 3640deeeabea43419c26c1e06aa3258f\n      entity_id: binary_sensor.door_sensor_front_door\n      domain: binary_sensor\n      id: trigger_front_door_open\n    condition:\n    - condition: or\n      conditions:\n      - condition: and\n        conditions:\n        - condition: trigger\n          id: trigger_front_door_open\n        - condition: time\n          after: 06:00:00\n          before: 08:00:00\n      - condition: not\n        conditions:\n        - condition: trigger\n          id: trigger_front_door_open\n    action:\n    - service: input_boolean.turn_off\n      data: {}\n      target:\n        entity_id: input_boolean.night_mode\n    mode: single<\/code><\/pre>\n\n\n\n<p>And one final thing to do is to update time helper values when the night mode is being changed<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>automation:\n  - alias: Night mode on last date and time\n    description: 'Sets night mode helper values'\n    trigger:\n      platform: state\n      entity_id: input_boolean.night_mode\n      from: 'off'\n      to: 'on'\n    action:\n      - service: input_datetime.set_datetime\n        data_template:\n          entity_id: input_datetime.night_mode_last_trigger_on\n          time: '{{ (as_timestamp(now()) | timestamp_custom(\"%H:%M:%S\", true)) }}'\n          date: '{{ (as_timestamp(now()) | timestamp_custom(\"%Y-%m-%d\", true)) }}'\n  - alias: Night mode off last date and time\n    trigger:\n      platform: state\n      entity_id: input_boolean.night_mode\n      from: 'on'\n      to: 'off'\n    action:\n      - service: input_datetime.set_datetime\n        data_template:\n          entity_id: input_datetime.night_mode_last_trigger_off\n          time: '{{ (as_timestamp(now()) | timestamp_custom(\"%H:%M:%S\", true)) }}'\n          date: '{{ (as_timestamp(now()) | timestamp_custom(\"%Y-%m-%d\", true)) }}'\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Counting persons<\/h2>\n\n\n\n<p>Now we need to use the history stats sensor to count all persons between time values stored in helpers <em>night_mode_last_trigger_on<\/em> and <em>night_mode_last_trigger_off<\/em>.<\/p>\n\n\n\n<p>Of course one exception, if night mode is still on, we should count the events to current timestamp instead of last_trigger_off value!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sensor:\n  - platform: history_stats\n    name: Security events during night mode\n    entity_id: sensor.front_yard_detected_object\n    state: person\n    type: count\n    start: \"{{ states.input_datetime.night_mode_last_trigger_on.attributes.timestamp }}\"\n    end: &gt;-\n      {% if is_state('input_boolean.night_mode', 'off') %}\n      {{ states.input_datetime.night_mode_last_trigger_off.attributes.timestamp }}\n      {% else %}\n      {{ as_timestamp(now()) }}\n      {% endif %}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion and what next&#8230;<\/h2>\n\n\n\n<p>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!<\/p>\n\n\n\n<p>To make upgrades for this, I still need to count also cars during night and use multiple cameras.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1107\" height=\"435\" src=\"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-06-at-10.37.40.png\" alt=\"\" class=\"wp-image-1013\"\/><figcaption>Person(s) and car(s) as seen through Unifi G4 Camera<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>For a while I&#8217;ve been trying to figure out a proper way to create automation that counts all persons during a night and shows it&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1016,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3,100,2],"tags":[165,199,196,7,198,197,8,61,62,89],"class_list":["post-1005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide","category-home-assistant","category-inspiration","tag-camera","tag-car-detection","tag-g4","tag-home-assistant","tag-person-detection","tag-security","tag-smart-home","tag-ubiquiti","tag-unifi","tag-unifi-protect","has-post-thumbnail-archive"],"acf":[],"featured_image_src":"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2022\/05\/night_mode_featured_image.png","author_info":{"display_name":"Toni","author_link":"https:\/\/www.creatingsmarthome.com\/index.php\/author\/topsy\/"},"_links":{"self":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/1005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/comments?post=1005"}],"version-history":[{"count":15,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/1005\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/1005\/revisions\/1026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/media\/1016"}],"wp:attachment":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/media?parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/categories?post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/tags?post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}