For condition in automation.yaml not working

I hope someone can help me.
I have setup a proximity configuration and automation to detect when I am moving away a certain distance from home or coming towards home.
What doesn’t work in the automation in automation.yaml, is the ‘for: 1 minutes’ condition. When I remove that time condition the automation works brilliant.
Also when I test the conditions in the GUI it tests ‘conditions are met’, but the action is never executed as long as the time condition is present.

(The idea behind it is clear I think, so I can control for example heating etc when leaving home or arriving home. My HA is accessible remotely from my smartphone via a VPN solution of course otherwise HA will never get any location data)

This is the part in my automation.yaml:

- id: maurice_coming_home_5km
  alias: Maurice is coming home
  trigger:
  - platform: numeric_state
    entity_id: proximity.home
    below: 5
  condition:
    - condition: state
      entity_id: proximity.home
      attribute: dir_of_travel
      state: towards
      for:
        minutes: 1
  action:
  - service: notify.mobile_app_maurice_iphone
    data:
      message: 'Maurice is coming home closer than 5km'
  mode: single

Direction of travel can be pretty volatile, especially in urban or suburban areas with traffic stops, oneway streets, etc. Have you tried a shorter duration on the condition or a different distance on the proximity? It could just be coincidental that at the moment you cross the 5km boundary you had just been stopped at a stop sign and the dir_of_travel was stationary 30 seconds ago.

I live in the ‘wide open’ so when I travel it will always be longer trips without traffic lights etc. But I will try to do some other tests today.
As mentioned, it works if I take the time condition out so it has to do something with how HA implements this time measurement.

I suspect that you can’t use for: at the same time as attribute: in a state condition — it’s mentioned in passing in the state trigger documentation:

Despite the “HOLDING A STATE OR ATTRIBUTE” heading, the State object only records two times:

  • last_changed when the state last changed
  • last_updated when the state and/or attributes last changed

So if your proximity state (the distance) changes, both of these timestamps change and there’s no way for the condition to establish how long the dir_of_travel has been in a certain state.

The solution is to create a template sensor whose state is the dir_of_travel attribute:

template:
  - sensor:
      - name: Maurice direction of travel
        state: "{{ state_attr('proximity.home', 'dir_of_travel') }}"

then use that sensor in the condition.

1 Like

Thanks. Let me try that out the coming weekend. I will come back on the results.

Thank you @Troon . That was indeed the right solution and while I was thinking about it your explanation makes perfectly sense.
Did create the sensor and adjusted the condition.
A minor side effect from using an iPhone as tracker is that sensor updates are not on predictable times so HA doesn’t execute the action always at the same distance from home. But it works well enough now to bring this further.

1 Like