Create a zone around a moving person

Hello everyone,

I have a use case, I don’t know how to set it up…

  • I would like to protect my bike from thieves with a tracker inside. It has already been successfully added to device trackers in home assistant, it appears on the maps and recognizes zones from hass
  • On the other side I have my phone that has the companion app and also appears in device trackers and the map of hass.

I would like to create an automation that automatically protects my bycicle when I leave it to park:

  1. My bycicle is parked and is not within my phone’s reach anymore (imagining there is a moving zone of 200m radius around my phone)

  2. If my bycicle is moving and my phone is not within reach I get a notification

  3. Else if my bicycle is moving and my phone is also moving with it then nothing happens

The question is, can we create a moving zone around a device tracker? that automatically updates when device moves?
Do I have to use the proximity integration?

Thanks in advance for any help !!

AFAIK there isn’t a way to create a mobile zone. But, you can use the distance function in a template trigger or sensor to monitor how far apart two GPS enabled device trackers, person entities, lat/long coords are. With this, you could save the location of the bike to helpers and use that location to know if the bike has been moved.

The following is untested

trigger:
  - platform: template
    id: park
    value_template: >
        {{ distance('person.gussir', 'device_tracker.bike') > .025 }}
  - platform: template
    id: bike moving
    value_template: >
        {% set p_lat = states('input_number.bike_latitude') | float(0) %}
        {% set p_long = states('input_number.bike_longitude') | float(0) %}
        {{ distance('device_tracker.bike', p_lat, p_long) > 0.025 and 
        distance('person.gussir', 'device_tracker.bike') > 0.025 }}
condition: []
action:
  - choose:
    - alias: "Save bike location to helpers when gussir leaves the bike"
      conditions:
        - condition: trigger
          id: park
      sequence:
        - variables:
            lat: "{{ state_attr('device_tracker.bike', 'latitude') }}"
            long: "{{ state_attr('device_tracker.bike', 'longitude') }}"
        - service: input_number.set_value
          target:
            entity_id: input_number.bike_latitude
          data:
            value: "{{ lat }}"
        - service: input_number.set_value
          target:
            entity_id: input_number.bike_longitude
          data:
            value: "{{ long }}"
        - service: notify.mobile_app_gussir
          data:
            title: Bike Parking Location
            message: >
              You left your bike at {{ lat ~', '~ long }} 
    - alias: "Notify if bike leaves location"
      conditions:
        - condition: trigger
          id: bike moving
      sequence:
        - service: notify.mobile_app_gussir
          data:
            title: Bike Alert
            message: Your bike is moving and you're not on it!

EDIT: Corrected issue in the “bike moving” template and a couple typos

2 Likes

Thanks, I’ll give it a shot !

You could make a custom component to create dynamic stationary zones for your bike.
Have a look at the custom component icloud3, which does this for an iphone tracker.

1 Like

Hey,
I’ve implemented this code but wasn’t yet able to test in real life.

I have some questions:

  • For the “park”: Can we add other condition to make sure that it’s the bycicle that left behind and not the phone? If my understanding is correct it can also be the other way around if my phone falls behind during transit for example, the distance will be increased and therefore trigger the “park” automation?

  • For the “bike moving”: At what moment the two variables p_lat and p_long are stored in HASSIO? I don’t really understand how with this aumation we can detect (and store the position) when my bike its being considered as parked.

For your info .025 is not recognized in the automation and must be change to 0.025
Thanks again for all this code !

Your understanding is correct. One method would be to set up a Proximity sensors for your bike and phone so you can take advantage of the dir_of_travel attribute. For this application it doesn’t realy matter what your reference zone is, but you will need to make sure that the dir_of_travel attribute is reliable in your location, especially when “stationary”.

Another option might be using the “detected_activity” sensor created by the companion app.

The values that become p_lat and p_long are stored in their respective helpers during the “park” actions; specifically the two input_number.set_value service calls.

Great thanks for the feedback!
Just one note for anyone who’s going to use the code again later:

Little error on input_number.bike_latitude and input_number.bike_longitude that lack the ‘’ and the
.025 thats also not recognize :slight_smile:
Have a good day !

Hello again, for your information, I thought of adding a new condition to trigger the Bike Alert automation:

I added the following condition to check that my mobilephone acitivity is not detected has being “on a bicycle”
(states('sensor.mobile_detected_activity') != "on_bicycle")
This might help in reducing the number of false positive I think :slight_smile:

That should probably always be false, because it is not spelled correctly. I’ll Google to check that Google didn’t make a mistake - but bicycle is spelled like that.

1 Like

Exact thanks for your help
I’ll correct the post