Best way to turn security camera on when I leave the home and turn off when I arrive (before motion detection detects me)?

I have HA OS on a Raspberry Pi Zero 2 W as well as a Tapo C210 indoor security camera (not set up or connected yet).

It is in my home office. The security camera will be set to detect motion and send an alert to my phone as well as record the motion to the SD card on the pi Zero 2 W.

I want to turn the security camera on when I leave the house (not leave the office, the house), and turn it off when I return (ideally before I am detected by the camera, recorded and an alert sent).

Because I live in the house with other people I cannot set up a motion detector or front door camera etc to detect me leaving/arriving that way.

The two ideas I have are:

1). Set up a presence/wifi detector for my smartphone’s wifi connection and when the phone disconnects from wifi (which will be outside my small home) it turns on a Tapo P100 smart wifi socket (which the Tapo C210 will be plugged into) giving power to the C210 and turning it on. When I return to the wifi network, the P100 socket will turn off, preventing me being detected on the C210’s motion detector. Will this work?

  1. Buy a smart button which turns the P100 socket on when pressed and then I can use the smartphone app to turn the P100 socket off when I get my keys out to enter the front of my house. Kind of a hassle and might forget.

Any better ways?

Basically you need to come up with ways to detect if you are in the office or not. Pir sensors in the office, computer power sensor to determine if it is on or off, lights on/off, bluetooth beacon and your phone tracking it, wifi sensor.

To minimize false positivises that smart button is best one. Press it when leaving the office and maybe have 5 to 10 minutes delay before turning on the camera.

Turning the camera off should not be just a button for anyone to press. I suppose you have a door?
I would keep camera on as long as you phone is in the area and while that is true, door is opened.
Then if you didnt enter the office the recording is on. Alltough anyone could enter while you are in the house, so propably not the best. Partly the idea is to have two conditions for shutting down the camera.

I am not worried about other people living in the house going into the office during the time I arrive home and going to the office (would be too risky and they likely won’t ever want to enter the office). BTW, by home office I meant my desk in my bedroom.

So pretty much, every time I return home I will go directly to my bedroom/home office, leaving 20 seconds for someone to go into the room (unlikely).

I am only concerned about them seeing me leave the house and being away from the house for 1+ hours, then going in there… and I a concerned about home invaders etc. when I am not home.

I’m a Pi 4 with a Tapo C220 (almost identical). Here is my yaml for the scripts, switch and automations. This should be easy enough for you to edit. The C220 can move so the switch also includes a section to have it face the wall to not creep people out when it is β€˜off’ (in privacy mode).

  • I am using a contact sensor on the door to trigger turning it on
  • I am using my location, the door closing and a group containing all my internal motion sensors to decide whether to turn it off (or not).
  • I am using the Tapo camera-control HACS integration.
  • I’ve left in all my debug logs as it took ages to get this right. So, whilst it is rock solid for me, you might need to play a bit to get it to work for you.
logbook card
- type: logbook
  title: "All"
  hours_to_show: 24
  entities:
    - automation.tapo_on_off
    - script.tapo_move_to_wall
    - script.tapo_move_to_door
    - switch.tapo_on_off
relevant parts of my package
input_select: #-----------------------------------------------------------------

  tapo_last_move:
    name: "Tapo's last move was to"
    icon: mdi:directions
    options:
      - "door"
      - "wall"


script: #-----------------------------------------------------------------------

  tapo_notify_snapshot:
    alias: "Tapo: Notify front camera status"
    mode: queued
    icon: mdi:panorama
    sequence:
      - if:
          - condition: state
            entity_id: switch.tapo_door_camera
            state: 'on'
        then:
          - service: camera.snapshot
            entity_id: camera.tapo_c220_hd_stream
            data:
              filename: "/snapshots/movement.jpg"
          - delay: 5 # secs
          #- service: script.notify with message "Camera should be facing {{ states('input_select.tapo_last_move') }}" and  attachment: "/snapshots/movement.jpg"
        else:
          #- service: script.notify with message "camera off"


  tapo_move_to_door:
    alias: "Tapo: Point camera at door"
    icon: mdi:door
    mode: queued
    sequence:
      - if:
          - condition: state
            entity_id: switch.tapo_door_camera
            state: 'on'
        then:
          - alias: "Wait until preset is available"
            wait_template: "{{ is_state('select.tapo_c220_move_to_preset', 'unknown') }}"
            timeout: "00:00:30"
            continue_on_timeout: true
          - if:
              - "{{ wait.completed }}"
            then:
              # remember move
              - service: input_select.select_option
                target:
                  entity_id: input_select.tapo_last_move
                data:
                  option: door
              # move to preset
              - service: select.select_option
                continue_on_error: true
                target:
                  entity_id: select.tapo_c220_move_to_preset
                data:
                  option: door
              # wait
              - delay: 5
              # notify
              - service: script.tapo_notify_snapshot
            else:
              # - service: script.notify with message "Unable to move camera to door - 'select' not available"
        else:
          # - service: script.notify with message "Unable to move to door - camera is off."

  tapo_move_to_wall:
    alias: "Tapo: Point camera at wall"
    icon: mdi:wall
    mode: queued
    sequence:
      - if:
          - condition: state
            entity_id: switch.tapo_door_camera
            state: 'on'
        then:
          - alias: "Wait until preset is available"
            wait_template: "{{ is_state('select.tapo_c220_move_to_preset', 'unknown') }}"
            timeout: "00:00:30"
            continue_on_timeout: true
          - if:
              - "{{ wait.completed }}"
            then:
              # remember move
              - service: input_select.select_option
                target:
                  entity_id: input_select.tapo_last_move
                data:
                  option: wall
              # move to preset
              - service: select.select_option
                continue_on_error: true
                target:
                  entity_id: select.tapo_c220_move_to_preset
                data:
                  option: wall
              # wait
              - delay: 5
              # notify
              - service: script.tapo_notify_snapshot
            else:
              # - service: script.notify with message "Unable to move camera to wall - 'select' not available"
        else:
          # - service: script.notify with message: "Unable to move to wall - camera is off."


switch: #β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”

- platform: template
  switches:
    tapo_door_camera:
      friendly_name: "Tapo door camera"
      value_template: "{{ is_state('switch.tapo_c220_privacy', 'off') }}"
      availability_template: "{{ has_value('switch.tapo_c220_privacy') }}"
      turn_on: #--------------------------------------------------
        # privacy
        - service: switch.turn_off
          target:
            entity_id: switch.tapo_c220_privacy
        # status LED
        - service: switch.turn_on
          target:
            entity_id: switch.tapo_c220_indicator_led
      turn_off: #-------------------------------------------------
        # status LED
        - service: switch.turn_off
          target:
            entity_id: switch.tapo_c220_indicator_led
        # privacy
        - service: switch.turn_on
          target:
            entity_id: switch.tapo_c220_privacy
      icon_template: >-
        {% if is_state('switch.tapo_c220_privacy', 'off') %}  mdi:cctv
        {% else %}  mdi:cctv-off
        {% endif %}


automation: #-------------------------------------------------------------------
- alias: "Tapo: Camera control"
  id: tapo_on_off
  mode: queued
  initial_state: true
  trigger:
    - platform: numeric_state
      entity_id: zone.home
      below: 1
      id: away
    - platform: numeric_state
      entity_id: zone.home
      above: 0
      id: home
    - platform: state
      entity_id: binary_sensor.front_door_contact
      to: "on"
      id: open
    - platform: state
      entity_id: binary_sensor.front_door_contact
      to: "off"
      id: closed
  action:
    - choose:
        # away -----------------------------------------------------------------
        - conditions:
            - "{{ trigger.id == 'away'}}"
          sequence:
            # notifications on
            - service: switch.turn_on
              target:
                entity_id: switch.tapo_c220_notifications
            # tamper protection on
            - service: select.select_option
              target:
                entity_id: select.tapo_c220_tamper_detection
              data:
                option: normal
        # home -----------------------------------------------------------------
        - conditions:
            - "{{ trigger.id == 'home'}}"
          sequence:
            # notifications off
            - service: switch.turn_off
              target:
                entity_id: switch.tapo_c220_notifications
            # tamper protection off
            - service: select.select_option
              target:
                entity_id: select.tapo_c220_tamper_detection
              data:
                option: "off"
        # door open ------------------------------------------------------------
        - conditions:
            - "{{ trigger.id == 'open'}}"
            - "{{ states('switch.tapo_door_camera') == 'off' }}"
          sequence:
            # try 3 times to turn camera on
            - repeat:
                while: "{{ is_state('switch.tapo_door_camera', 'off') and repeat.index <= 3 }}"
                sequence:
                  # switch on camera
                  - service: switch.turn_on
                    target:
                      entity_id: switch.tapo_door_camera
                  # wait for wake-up if off for a while
                  - wait_template: "{{ is_state('switch.tapo_door_camera', 'on') }}"
                    timeout: "00:00:10"
                    continue_on_timeout: true
                  - if:
                      - "{{ wait.completed }}"
                    then:
                      # debug log
                      - service: logbook.log
                        data:
                          name: "Debug:"
                          message: "Camera on after attempt #{{ repeat.index }}"
                          entity_id: automation.tapo_on_off
                      # move camera to door (wait to complete)
                      - service: script.tapo_move_to_door
                    else:
                      # debug log
                      - service: logbook.log
                        data:
                          name: "Debug:"
                          message: "Try #{{ repeat.index }} didn't turn camera on."
                          entity_id: automation.tapo_on_off
                      - if:
                          - "{{ repeat.index == '3' }}"
                        then:
                          # notify with message "Failed to turn on."
        # door closed ----------------------------------------------------------
        - conditions:
            - "{{ trigger.id == 'closed'}}"
            - "{{ states('switch.tapo_door_camera') == 'on' }}"
            - "{{ states('zone.home') != '0' }}"
          sequence:
            # wait for hallway motion sensor to timeout
            - delay: 10 # secs
            # debug log
            - service: logbook.log
              data:
                name: "Debug:"
                message: "Door closed - waiting..."
                entity_id: automation.tapo_on_off
            # wait for indication of movement
            - wait_template: >
                {{
                  is_state('binary_sensor.indoor_movement', 'on')
                  or
                  is_state('binary_sensor.front_door_contact', 'on')
                  or
                  is_state('zone.home', '0')
                }}
              timeout: "00:05:00"
              continue_on_timeout: true
            - choose:
                # Movement detected & home ------------------------------
                - conditions:
                    - "{{ wait.completed }}"
                    - "{{ states('binary_sensor.indoor_movement') == 'on' }}"
                    - "{{ states('zone.home') != '0' }}"
                  sequence:
                    # debug log
                    - service: logbook.log
                      data:
                        name: "Debug:"
                        message: "Inside movement (still 'home')"
                        entity_id: automation.tapo_on_off
                    # move to wall
                    - service: script.tapo_move_to_wall
                    # debug log
                    - service: logbook.log
                      data:
                        name: "Debug:"
                        message: "Switching off camera - finished."
                        entity_id: automation.tapo_on_off
                    # switch off camera
                    - service: switch.turn_off
                      target:
                        entity_id: switch.tapo_door_camera
                # Door re-opened (still 'home') ----------------------------
                - conditions:
                    - "{{ wait.completed }}"
                    - "{{ states('binary_sensor.front_door_contact') == 'on' }}"
                    - "{{ states('zone.home') != '0' }}"
                  sequence:
                    # debug log
                    - service: logbook.log
                      data:
                        name: "Debug:"
                        message: "Door re-opened (still 'home') - finished."
                        entity_id: automation.tapo_on_off
                # now 'away' --------------------------------------------
                - conditions:
                    - "{{ wait.completed }}"
                    - "{{ states('zone.home') == '0' }}"
                  sequence:
                    # debug log
                    - service: logbook.log
                      data:
                        name: "Debug:"
                        message: "Now 'away' - finished."
                        entity_id: automation.tapo_on_off
                # wait timed out -----------------------------------------------
                - conditions:
                    - "{{ not wait.completed }}"
                  sequence:
                    # debug log
                    - service: logbook.log
                      data:
                        name: "Debug:"
                        message: "Wait timed out (outside but nearby) - finished."
                        entity_id: automation.tapo_on_off

- alias: "Tapo: Notify when turning on/off"
  id: tapo_notify
  initial_state: true
  mode: queued # in case door open➜close quickly
  trigger:
    - platform: state
      entity_id: switch.tapo_door_camera
  action:
    # - service: notify with message: "{{ states('switch.tapo_door_camera') }}"


I hashed out the β€˜notify’ sections as mine reference a script which would not help you - just put in your own notify service there using the message that I have written (or your own version of it).

Good luck!

[edited for typos and clarity. Added the input_select to notify last position moved to]

2 Likes

I got HA OS up and running on the Pi Zero 2 W but the connection in my web browser keeps failing. Probably due to the Zero 2 W not have much RAM? I do not even have any devices added yet.

Perhaps I should get Raspberry Pi 5 with 4GB RAM?

I’m on a Pi 4 with 2GB RAM and it works great (no video processing though).

I have a friend on a PI-5 who says its quite snappy, so I guess that is more future-proof.

1 Like

A simple solution is to use the proximity integration. It tracks how close or far you are from the zone and you can define automations based upon that distance. So, if you more than 1000 feet from your zone and approaching, you can turn your camera off before you ever enter the house.

1 Like

Does Proximity work due to my phone (will always be with me when I leave the house) having WIFI turned on? I am not sure how quickly my phone would reconnect to the WIFI when I am near home (my WIFI signal barely leaves the house).

Hi @terryb1

How about a simple Bluetooth detection of your phone? What make is it, which OS is it running?

I use my Apple Watch for exactly this purpose, to deactivate/activate my alarm system whenever I approach my front door, or leave the house. You could install our Theengs Gateway (HA Add-on) along on the Pi Zero, to have it auto-discover your phone as a device_tracker, and with the the scan time setting make it as responsive as you need it for your purpose, without any possible WiFi or other cloud service lag.

No, not exactly. It uses whatever method your device tracker uses (easiest is the HA companion app installed on your phone exposing location).

Is this add-on the same or better?

So I need to allow location services for HA iOS app? Couldn’t that be used to track me anywhere I go?

No idea to be honest, as I’ve never tried that one. I suppose you could try all the different options which implement a similar local blueooth presence detection, and see which one works best :wink:

So I take it you have an iPhone. Have a look at how your iPhone would appear with its presence device_tracker and the unlocked state - although there is an issue with the correct unlocked state for some users, which we haven’t tracked down to which settings difference is causing this for some.

1 Like

If I understand correctly, I could have location services enabled for that app, only when the app is open? So when I get home I unlock my phone and it tells HA OS to turn off the security camera?

Now I think of it, could I make a iOS shortcut using the Shortcuts app/feature and HA iOS app, and add it to my phone’s home screen and just press that button when I get home?

Which app are you talking about now, I don’t know if that works for the HA iOS app, to only allow for location services when running in the foreground, but even then I would expect quite a lag in the actual location being broadcast through the cloud to your HA instance.

With the Theengs Gateway Add-on running on your RPi, the bluetooth presence of your iPhone is detecting HOME as soon as you come into bluetooth range of your home, if and how you then implement to also require the unlocked state to be unlocked (just be unlocking the iPhone, there is no separate app required on the iPhone for this) to disable your camera is totally up to your automation - and the unlocked state being correctly recognised on your iPhone.

The only problem I could see you running the Theengs Gateway Add-on is that your stated the even the Web connection to HA running on your Pi Zero is already flakey.

1 Like

This is probably the simplest solution.

Thanks! I will try this and report back. It will kind of suck having Bluetooth on all the time, but it is a simple solution (if it works with a Pi Zero 2 W).

Alternatively, and assuming that you also have your keys with you whenever you leave the house, is a small cheap simple bluetooth beacon tag on your key chain. Something I also used before switching over to the Apple Watch.

Something like this?