Toggling motion detection on a camera via a lovelace switch

Hi,

Sorry if this is a n00b question; i’ve tried searching around but haven’t been able to find a solution to this particular use case.

We have a couple of Blink cameras around the home but the way we’ve configured the automation is that the system is typically enabled but then we just turn on and off the motion detection as required.

I’ve got it automated so it automatically enables motion detection on all cameras when we leave and turns them off when we return but there are some occasions when we want to do it manually, and it would be far easier to be able to do it via Home Assistant instead of the blink app.

So far as I can see it requires two distinct service calls - one to enable the motion detection and another to disable it. Could someone please help me with how we’d go about configuring such a setup in Home Assistant?

Ideally i’d like a switch on one of the lovelace pages which can be turned on and off and then have that drive the appropriate service calls.

Grateful for any help given.

Pete

Here is a command line switch I use to toggle a Foscam camera motion detection to enabled/disabled:

- platform: command_line
    switches:
      foscam_computer_cam_motion:
        friendly_name: 'Computer Cam Motion'
        command_on: 'curl -k "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&usr=user_name&pwd=pass_word"'
        command_off: 'curl -k "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=user_name&pwd=pass_word"'
        command_state: 'curl -k --silent "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=user_name&pwd=pass_word" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
        value_template: '{{ value == "1" }}'

You could modify it to use the commands for your camera and then set the state of the switch to display accordingly.

It’s a good suggestion but i’m not sure that the Blink cam’s i’ve got support using curl commands to modify their configuration. :frowning:

Nice to know it’s possible with Foscams though as i’m looking at some of those for the future.

Foscam’s are my go to camera’s. They are relatively inexpensive and they work really well.

But back to your issue…

I think I’m confused by this:

Doesn’t this mean that you have it set up in a HA automation already? or is that automation not in HA but in the Blink App?

I have some automations working with it - but I have a need to be able to also turn on and off motion detection manually from the HA interface.

I could just do it with two discrete buttons on a page which then call the services but i’d much rather use a regular toggle switch if there’s a way to achieve that goal.

OK, so you have a way in HA right now to call a service to turn on/off motion detection on the camera?

If that’s correct then you can create a template switch to run services based on the on/off switch action.

That looks like exactly what i’m after. I’ll take a look into it and report back.

Thanks finity. :slight_smile:

Hmmm, I think i’m heading in the right direction, but not quite there.

This is the code i’ve put into the yaml file:

  - platform: template
    switches:
      blinkbedroommotiontoggle:
        value_template: "{{ is_state_attr('camera.blink_bedroom', 'motion_enabled', 'false') }}"
        turn_on:
          service: camera.enable_motion_detection
          data:
            entity_id: camera.blink_bedroom
        turn_off:
          service: camera.disable_motion_detection
          data:
            entity_id: camera.blink_bedroom

The issue I have now is that the switch doesn’t seem to be aware of the current state of the motion detection. If it’s enabled and I reboot the Pi then when it comes back online the switch still shows it as ‘off’. Also, clicking on the switch causes it to briefly turn on before then switching off again.

Does the value_template you created show the correct change in state in the template editor when you operate the template switch?

At first it wasn’t because it turns out that the value false needs to be without quotes.

Here is what the camera entity looks like in the state view.

And this is what the template section shows when I paste the template value:

blink_camera2

The issue still exists although it’s now the other way round. The switch shows as being turned on when motion detection is disabled and when I click the switch it turns off for a few seconds and then turns itself back on.

1 Like

I’m not entirely sure that’s correct. But…

When you use the latest posted value_template in the template editor do you see the state switch from True to False & back again as you toggle the motion enabled?

remember tho that you’ll have refresh the template editor when you change the motion enabled to be able to see the updated state value. It doesn’t update in real time - only when it’s modified or refreshed.

I only figured out that the quotes weren’t required because when the motion_enabled attribute showed as false in the state view the template was actually reporting False which didn’t seem right. Removed the quotes and sure enough it then reported as True.

It doesnt’ seem that invoking the switch has any effect on the state.

However, if I use the Blink App on my mobile to turn the motion detection on then I see the change in the template. The evaluation of the template value changes from True to False.

Aaah, I think I know the issue.

The turn_on and turn_off makes calls to actions - not services. I’ve been trying to call a service directly.

So I think I need to define two actions and then update the config to use those. I’ll give it a try from home tonight and let you know how it goes.

You can call services directly in the turn_on/off portion of the switch. In fact, that is exactly what you are doing in the code. You call a service (“camera.enable_motion_detection” is a service), give it an entity_id (the “camera.blink_bedroom”) and then the service is performed on that entity.

So the following are actions but they are actions that call a service on your camera:

turn_on:
  service: camera.enable_motion_detection
  data:
    entity_id: camera.blink_bedroom
turn_off:
  service: camera.disable_motion_detection
  data:
    entity_id: camera.blink_bedroom

I’m pretty sure that part should work.

try the following in the value_template and see if it works:

"{{ states.camera.blink_bedroom.attributes.motion_enabled == false }}"

Sorry for the delay - it’s been a bit of a manic weekend.

Ok, so the above value_template returns True when I test it in the Template section of the developer tools.

However, this also means that when I use the switch in Lovelace it’s turned ‘on’ when the motion detection is turned off. I presume I need to invert the value template so that we test for == true instead and then it should work the correct way round (off when Motion Detection is disabled and on when Motion Detection is enabled?).

However, it’s still doing the ‘thing’ where when you click the switch it turns ‘off’ and then after a few seconds it just goes back to the ‘on’ state again. And nothing happens regarding the motion detection.

can you post the automations you use to control the camera’s motion detection when you leave and return, please?

You said those automations work correctly, right?

Yes, they work fine. We don’t actually change the motion detection setting on arriving home but just disarm the entire system. This probably needs changing because what we actually want is for the motion sensing to still work in some rooms and it’s necessary for the system to be armed for it to work.

The automation when we leave arms the system and also enabled motion detection on the two cameras we have and this seems to work just fine.

  # When everyone leaves home arm Blink and make sure motion sensing is enabled on all cameras
  - alias: Arm Blink when everyone leaves home
    trigger:
      platform: state
      entity_id: group.all_devices
      to: 'not_home'
    action:
      - service: alarm_control_panel.alarm_arm_away
        entity_id: alarm_control_panel.blink_home
      - service: camera.enable_motion_detection
        entity_id: camera.blink_bedroom
      - delay: '00:00:05'
      - service: camera.enable_motion_detection
        entity_id: camera.blink_landing

  # Disarm Blink when anyone comes home
  - alias: Disarm Blink when everyone arrives home
    trigger:
      platform: state
      entity_id: group.all_devices
      to: 'home'
    action:
      service: alarm_control_panel.alarm_disarm

1 Like

Just one more thing I’d like you to try just to make everything is working the way I think it’s supposed to.

go to the developer tools section in HA and select the services tab.

then from the drop down select the service for whichever condition the camera motion detection is NOT currently in (if it’s enabled select the disable service, etc).

than enter the entity_id of the camera (camera.blink_bedroom) and click to run the service.

Check that the motion detection has switched states.

Yes, that works fine. Tested with the system armed but no motion detection enabled.

Made the call to the service camera.enable_motion_detection with the entity camera.blink_bedroom.

When I refreshed the Blink App (I have to drag down to refresh the UI) then it shows as motion detection enabled.

I did the opposite Service call and that also worked.

Ok…I think I’m just stupid…:crazy_face: What’s worse is that I think you pointed out the problem a couple of posts up.

I don’t know why I got tunnel vision and used your initial test for the motion enabled as false from post #8 and then continued to insist on using the motion enabled as false for the state value of the switch. Then when you said you need to test for true instead of false I completely blew right past it.

so, yes try the test for true in your value_template and I think it should work.

"{{ states.camera.blink_bedroom.attributes.motion_enabled == true }}"

and, in fact, tho i’m not 100% sure about this, because the attribute returns true/false already you might even be able to eliminate the test completely and just go with:

"{{ states.camera.blink_bedroom.attributes.motion_enabled }}"