Howto use multiple device-tracker to turn off item?

Hi, im struggling with a automation.
what i want to do is

there are two user in this house, i want to turn on webcams when both are away.
the webcams are plugged in with smart plugs.

etc.
user1 and 2 “home” = webcam off
user1 “home” but user 2 “not_home” = webcam off
user1 and 2 “not_home” = webcam on

This can be accomplished with two automation pretty easily.

  - alias: "Camera On When leaving"
    trigger:
      - platform: zone
        entity_id: device_tracker.person1
        zone: zone.home
        event: 'leave'
      - platform: zone
        entity_id: device_tracker.person2
        zone: zone.home
        event: 'leave'
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: "{{ states('device_tracker.person') != 'home' }}"
        - condition: template
          value_template: "{{ states('device_tracker.person') != 'home' }}"
    action:
      - service: switch.turn_on 
        entity_id: webcam_1

The above triggers each time someone leaves but only executes the action if both conditions are met (no one is home). The zone trigger only works if you are using a gps based tracker, it can be easily adjusted if you only have local device tracking.

  - alias: "Camera Off When Home"
    trigger:
      - platform: zone
        entity_id: device_tracker.person1
        zone: zone.home
        event: 'enter'
      - platform: zone
        entity_id: device_tracker.person2
        zone: zone.home
        event: 'enter'
    action:
      - service: switch.turn_off
        entity_id: webcam_1

This automation triggers anytime someone comes home. You may want to add a condition to this automation for the camera to already be off. Would need to know the camera entity_id and ‘off’ state to do this though. You will also need to adjust the service and entity_ids listed to match your camera and device trackers.

1 Like

hmm… i got this error when trying to add this automation

Unsupported condition: and
{
“condition”: “and”,
“conditions”: [
{
“condition”: “template”,
“value_template”: “{{ states(‘device_tracker.galaxy_s8’) != ‘home’ }}”
},
{
“condition”: “template”,
“value_template”: “{{ states(‘device_tracker.84_0d_8e_92_18_1f’) != ‘home’ }}”
}
]
}

Can you post your full config here for a review? See the blue box at the top of the post to maintain proper formatting.

here

- id: '1548185863413'
  alias: Camera On When leaving
  trigger:
  - entity_id: device_tracker.galaxy_s8
    event: leave
    platform: zone
    zone: zone.home
  - entity_id: device_tracker.84_0d_8e_92_XX_XX
    event: leave
    platform: zone
    zone: zone.home
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: '{{ states(''device_tracker.galaxy_s8'') != ''home'' }}'
    - condition: template
      value_template: '{{ states(''device_tracker.84_0d_8e_92_XX_XX') != ''home''
        }}'
  action:
  - data:
      entity_id: switch.74607070dc4f22fb6002
    service: switch.turn_on
  - data:
      entity_id: switch.74607070dc4f22fb6254
    service: switch.turn_on
- id: '1548186234308'
  alias: Camera Off When Home
  trigger:
  - entity_id: device_tracker.galaxy_s8
    event: enter
    platform: zone
    zone: zone.home
  - entity_id: device_tracker.84_0d_8e_92_XX_XX
    event: enter
    platform: zone
    zone: zone.home
  condition: []
  action:
  - data:
      entity_id: switch.74607070dc4f22fb6002
    service: switch.turn_off
  - data:
      entity_id: switch.74607070dc4f22fb6254
    service: switch.turn_off

I think the issue has to do with the quotes in your conditions.

Place “quotes” around the entire value template. Use single ‘quotes’ inside those quotes. Note that you have multiple single ’ ’ quotes ’ ’ around home and device_tracker.XXXXXXXX.

Wouldn’t it be easier just to use a group? And if those are the only two device_trackers, there’s already a group that could be used - group.all_devices.

Basically, the automation trigger would be the group. A group of device_trackers, by default, is home when any one of the device_trackers is home, and not_home when all of the device_trackers are not home. And it could be done with one automation with a service_template.

Something like:

- alias: Camera control
  trigger:
    platform: state
    entity_id: group.my_devices
  action:
    service_template: >
      {% if trigger.to_state.state == 'home' %}
        switch.turn_off
      {% else %}
        switch.turn_on
      {% endif %}
    entity_id:
      - switch.74607070dc4f22fb6002
      - switch.74607070dc4f22fb6254

EDIT: Oops, just noticed a mistake in the service template. Corrected.

1 Like

Seconding the group suggestion. They work great. The status of the members in the group are treated as a logical OR. So as long as one person in the group is ‘home’, then the group status is home. You can also use groups to define what you want to turn on/off.

wow, thanks guys, it was much easier to use group,
everything is working as it should now.

i need to read more about the creating of templates

Greetings all!

So I am new to Home Assistant and I am trying to configure device detection to trigger a switch on/off to change the state of Motion Detection in MotionEyeOS.

So far I have a working button; from ‘Configuration.yaml’:

# Motioneye Motion Detect on/off
switch:
  - platform: command_line
    switches:
      motion_detect_toggle:
        command_on: "/usr/bin/curl http://192.168.1.108:7999/0/detection/start"
        command_off: "/usr/bin/curl http://192.168.1.108:7999/0/detection/pause"
        command_state: "/usr/bin/curl http://192.168.1.108:7999/0/detection/status"
        value_template: "{{ value | regex_search('ACTIVE', ignorecase=True) }}"

Toggling this button works manually to change Motion Detection state, and device detection is working fine.

I have set up a group with my Girlfriend’s phone and my phone and put this into the ‘Automations.yaml’:

- alias: Motion Detection
  trigger:
    platform: state
    entity_id: group.awesome_people
  action:
    service_template: >
      {% if trigger.to_state.state == 'home' %}
        switch.turn_off
      {% else %}
        switch.turn_on
      {% endif %}
    entity_id:
      - switch.motion_detect_toggle

It will not change the button state or the Motion Detection state. Can anyone help???

1 Like