Trigger notification only when towards home

I created an automation that triggers a mobile notification when I leave Work Zone, this works great and is set as:

- alias: Notify travel from work
  trigger:
    - platform: state
      entity_id: device_tracker.iphone
      from: 'Work'
  action:
    service: notify.mobile_app_iphone
    data_template:
      title: "Home Assistant"
      message: "Leaving work"

But I would also like to add the proximity direction to it, to only trigger the notification if I’m towards Home.
I tried to set

  condition:
  - condition: template
    value_template: '{{ states.proximity.home.attributes.dir_of_travel == "towards" }}'

But this is not always correct… I noticed that my dir of travel can also be stationary when the state trigger leaving Work zone get’s activated.
So how can I keep track that I’m towards Home and keep into account that the I exited Work zone right before?

What do you mean by it being “not always correct”? How can you know if it’s correct? Will it be correct a certain amount of time after you leave work? Will it be correct if it’s stationary when you leave, and then changes to something else?

First you have to specify when and/or under what conditions it should be considered correct. Then a suggestion can be made as to how to proceed.

Good point… as soon if the dir of travel is towards and I left the work zone, I would like to trigger the notification.

I just wanted to point out that when I only set the trigger for leaving the work zone my dir of travel sometimes is stationary ( because I’m at a traffic light ).

So when dir of travel changes to towards, and you’ve left the work zone within the last century, …???

I think you need to think more about what the exact sequence(s) need(s) to be to send the notification. :slightly_smiling_face:

How about this? After leaving work, wait until dir of travel is not stationary. Then if it’s towards, then send the notification, otherwise abort. Will that work? Or could it be “away_from” after you leave the work zone (maybe, e.g., if you’re walking to the car in the wrong direction???)

Sorry, don’t mean to be facetious, but until you’ve thought about all the possibilities and defined exactly what should cause the notification to be sent it’s kind of hard to implement something that will do what you want.

No worries, I hope you don’t mind this discussion. Because this is the first time I want to dive a bit deeper in the automation possibilities so normal I did not yet think this through enough.

So in my mind… the work zone in home assistant is a circle and for my notification trigger I would like to send it as soon as I cross that circle. Timing would be in minutes I guess… so from 0 to 5minutes I left the circle.
Then the direction is important. I only want to actually send the trigger if it is determined that I’m toward my Home zone. So not stationary or unknown or away ( so none of the other directions ).

Is this more in what is possible?

Not sure that will handle all the possible cases the way you want, but it sounds like a start.

Try:

- alias: Notify travel from work
  trigger:
    - platform: state
      entity_id: device_tracker.iphone
      from: 'Work'
  action:
    - wait_template: >
        {{ is_state_attr('proximity.home', 'dir_of_travel', 'towards') }}
      timeout: '00:05:00'
      continue_on_timeout: false
    - service: notify.mobile_app_iphone
      data:
        title: "Home Assistant"
        message: "Leaving work"

So if dir of travel is towards when you leave work, or changes to that within 5 minutes, it will send the notification. Otherwise it will not.

2 Likes

Well @pnbruckner that sounds like a very good start indeed! Thx for the details!! I will try this.

1 Like

Just an FYI. If you were to leave the work zone, while dir of travel was not towards, and while it stayed that way and before the 5 minutes were up, you re-entered the work zone and left it again, the wait_template would be aborted and the next step would run, meaning it would send the notification. Not a likely scenario, but possible, especially if you “hover around” the border of the zone. This is just a side effect of the way automations work. If that becomes a problem there is a solution – move the action steps into a script, and then in the automation action “turn off” the script then run it. Let me know if/when you might need that.

Ok! But you are right, possibly not something that would happen often… so let’s first try it with your current suggestion.