Automation runs when it shouldn't

I don’t understand why this runs every time I walk away from my car.

The conditions should limit it.

id: '1627374681490'
alias: Andreas high accuracy walking home
description: ''
trigger:
  - platform: numeric_state
    entity_id: proximity.home
    below: '200'
  - platform: state
    entity_id: sensor.andreas_bluetooth_connection
    attribute: connected_paired_devices
condition:
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: state
            entity_id: sensor.wifi_connection
            state: <not connected>
          - condition: numeric_state
            entity_id: proximity.home
            below: '200'
      - condition: and
        conditions:
          - condition: template
            value_template: >-
              {{ '4E:C3:9E:5B:9C:86' in
              trigger.from_state.attributes.connected_paired_devices }}
          - condition: template
            value_template: >-
              {{ not '4E:C3:9E:5B:9C:86' in
              trigger.to_state.attributes.connected_paired_devices }}
          - condition: numeric_state
            entity_id: proximity.home
            below: '200'
          - condition: state
            entity_id: sensor.wifi_connection
            state: <not connected>
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: notify.mobile_app_andreas
    data:
      message: command_high_accuracy_mode
      title: turn_on
mode: single

proximity.home is currently about 48200
sensor.wifi_connection is currently <not connected>
sensor.andreas_bluetooth_connection does change when I walk away from the car and the mac address is the one I want it to trigger on.

Since the proximity is condition then it shouldn’t trigger, or is there something wrong with this condition?

This is the trace I get (I have never read one of these before).

Executed: 2 August 2021, 6:32:57
Result:

result: true

## conditions/0

Executed: 2 August 2021, 6:32:57
Result:

result: true

## conditions/0/conditions/0

Executed: 2 August 2021, 6:32:57
Result:

result: true

## conditions/0/conditions/0/entity_id/0

Executed: 2 August 2021, 6:32:57
Result:

result: true state: <not connected> wanted_state: <not connected>

## conditions/0/conditions/1

Executed: 2 August 2021, 6:32:57
Result:

result: true

## conditions/0/conditions/1/entity_id/0

Executed: 2 August 2021, 6:32:57
Result:

result: true state: 0

What does the graphical trace look like?

Your proximity sensor is ORed with each of all the other sensor conditions. So it simplifies to this:

condition:
  - condition: or
      - condition: numeric_state
        entity_id: proximity.home
        below: '200'
      - condition: state
        entity_id: sensor.wifi_connection
        state: <not connected>
      - condition: and
        conditions:
          - condition: template
            value_template: >-
              {{ '4E:C3:9E:5B:9C:86' in
              trigger.from_state.attributes.connected_paired_devices }}
          - condition: template
            value_template: >-
              {{ not '4E:C3:9E:5B:9C:86' in
              trigger.to_state.attributes.connected_paired_devices }}
          - condition: state
            entity_id: sensor.wifi_connection
            state: <not connected>

Which may be simpler to trace.

I don’t think that will be correct.

I want the proximity and wifi connection to be “and”.

I do not want this to run when I walk away from my car and I’m more than 200 m from home. (and that is what it’s doing currently)

With your condition:

condition:
  - condition: or
      - condition: numeric_state
        entity_id: proximity.home
        below: '200'
      - condition: state
        entity_id: sensor.wifi_connection
        state: <not connected>

if either one of these is true then it runs? And since wifi connection is not connected at the time I leave my car usually (only if I park right outside, which could happen but is unusual) then that will be true at 99% of the times.

I want it to trigger when I park at my normal parking spot (less than 200 m from home) or when I disconnect from car BT.
But conditions is less than 200 m and not connected to wifi (not when I park right outside).
The reason is that this turns on the high accuracy mode to follow me home and then I have an automation to turn it off when I connect to home wifi.

If it would trigger when I park right outside the house then it might turn on the high accuracy mode after I connect to home wifi and thus it will “never” turn off.

While checking I found it is actually worse than what I originally posted.

A =

          - condition: state
            entity_id: sensor.wifi_connection
            state: <not connected>

B =

          - condition: numeric_state
            entity_id: proximity.home
            below: '200'

C =

          - condition: template
            value_template: >-
              {{ '4E:C3:9E:5B:9C:86' in
              trigger.from_state.attributes.connected_paired_devices }}

D =

          - condition: template
            value_template: >-
              {{ not '4E:C3:9E:5B:9C:86' in
              trigger.to_state.attributes.connected_paired_devices }}

You have:

(A && B) || (C && D && A && B)

This is equivalent to:

(A && B)

Your extra C and D conditions do nothing. Check it yourself: Boolean Algebra Calculator - Online Boole Logic Expression Simplifier

Your conditions simplify to just this:

conditions:
  - condition: state
    entity_id: sensor.wifi_connection
    state: <not connected>
  - condition: numeric_state
    entity_id: proximity.home
    below: '200'

Double checked on another website:

Screenshot 2021-08-02 at 16-37-03 Boolean Algebra Solver - Boolean Expression Calculator

Hmm… either way it’s still running when it shouldn’t.

It ran this morning when I got to work, 48000 m from home when not connected to wifi.

Check the state of sensor.wifi_connection and proximity.home at the time the automation ran (use the history page). Time according to the trace was 2 August 2021, 6:32:57

As both those two conditions would have to have been true (the others dont matter) for the automation to run.

Between the left point and the red marker there is no updates of the values.
Red marker is at 06:32:58
So proximity.home was 0 at the time even though high accuracy mode was enabled on the phone during the whole drive.
That explains why the automation ran. Now I have another problem to look in too.

1 Like