Location based automations - looking for ideas

Hi

I am looking for some ideas / input from you clever people, basically I have a working automation that notifies me of my wife’s location changes that currently has a condition that I am at home (as otherwise we are typically together) however I have a mild annoyance in that commonly when we either leave together or arrive home together I get the notification due to the iphones being logged as home at different times.

What’s the best or easiest way to clear this up, I wondered if I could make a binary sensor to state if we are close to each other and use that as a condition - thinking about it, it would be nice to have the notifications regardless of if I am home or not but just based on us not being together.

Any input / ideas greatly appreciated as always.

Has your wife consented to be tracked like this?

Yes of course, we use HA to track the whole family, my wife is a particularly nervous driver for example and likes the security of me knowing she has safely arrived at destination. In fact it was almost a demand from her in order to get her back driving again on her own after several years of her not driving.

You could use the wait for trigger in the automation.

So trigger when she arrives home, wait for trigger that is that you arrive home and have nothing happen if you also arrive home.
But the default action is to notify you after a few seconds (or perhaps minutes).

Yeah I thought about a time delay or wait but cant wrap my head round it, as at present the trigger is based on zone changes like this:

trigger:
  - platform: state
    entity_id: device_tracker.ej_iphone_app
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
  - condition: state
    state: home
    entity_id: person.ross

I guess I want to remove the condition of me being at home and have a condition of us not being in close proximity to each other as if I was at work for example I currently dont get the notifications she has arrived at zone as I am not at home.

Was thinking along the lines of a template sensor or boolean that represents if we are over a set distance from each other - currently use icloud3 and wondered if there was a way of harnessing the travel times or distances but at present these just appear to show distance to home - maybe template to difference the two distances but thats not the best as we could both be 10 miles away but in opposite directions.

If you just want the distance then there is the distance template.

{{ distance('person.ross', 'device_tracker.ej_iphone_app') }}
2 Likes

Perfect thank you - I thought I could work the rest out myself but on trying to make a template sensor based on above I am getting an error:

in sensors.yaml

- platform: template
  sensors:
    ross_and_ej_distance_apart:
      value_template: '{{ distance('person.ross', 'person.ej') | round (2, default=0) }}'

Error:

can not read an implicit mapping pair; a colon is missed at line 375, column 91:
     ... ej') | round (2, default=0) }}''
                             ^

What am i doing wrong here?

I don’t have “default” in my version of HA.

But: Templating - Home Assistant (home-assistant.io)

  • Filter round(precision, method, default) will convert the input to a number and round it to precision decimals. Round has four modes and the default mode (with no mode specified) will round-to-even. If the input value can’t be converted to a float, returns the default value, or if omitted the input value.
  • round(precision, "floor", default) will always round down to precision decimals
  • round(precision, "ceil", default) will always round up to precision decimals
  • round(1, "half", default) will always round to the nearest .5 value. precision should be 1 for this mode

Perhaps it needs the method also?

I assumed I needed default with most templates these days but interestingly all of these work in dev tools:

 {{ distance('person.ross', 'person.ej') | round (2, default=0) }}

{{ distance('person.ross', 'person.ej') | round(2, "half", default) }}

{{ distance('person.ross', 'person.ej') }}

Yet even if i remove the rounding and default I still get an error when trying to crate a template sensor:

- platform: template
  sensors:
    ross_and_ej_distance_apart:
      value_template: '{{ distance('person.ross', 'person.ej')}}'

Error:

can not read an implicit mapping pair; a colon is missed at line 375, column 66:
     ... e('person.ross', 'person.ej')}}'
                             ^

Now I see the issue.

You have used ' on both value template and entities. That won’t work since the first ' ends where person.ross starts.

You should have:

- platform: template
  sensors:
    ross_and_ej_distance_apart:
      value_template: "{{ distance('person.ross', 'person.ej') | round (2, default=0) }}"

" on one of them.

1 Like

Of course, what an idiot. I have lost count of the amount of times this has happened to me, as doing things in UI, Dev tools and yaml etc, they seem to have slightly different requirements.

Thank you so much for your help, greatly appreciated.

So just for continuity and in case it may help anyone else this is the final automation I have come up with with help from @Hellis81

trigger:
  - platform: state
    entity_id: person.ej
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
  - condition: numeric_state
    entity_id: sensor.ross_and_ej_distance_apart
    above: '1'
action:
  - service: notify.mobile_app_ross
    data_template:
      title: EJ Location update
      message: >-
        {{ now().strftime("%d%m%Y-%H%M%S") }} Location has changed from {{
        trigger.from_state.state }} to {{ trigger.to_state.state }}
      data:
        actions:
          - action: URI
            title: Show On Map
            uri: /test-dashboard/map
        push:
          sound: alarm.caf
mode: single