Automation triggers when Conditions aren't met

What am I doing wrong.
I want to trigger this when my own or my wife’s cell phone is left in the garage and the Hallway sensor triggers. But it seems to trigger although we are home.

- id: '1582752041728'
  alias: If Phone left at garage
  description: ''
  trigger:
  - entity_id: binary_sensor.sensor_hallen_motion
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
    - condition: zone
      entity_id: device_tracker.sm_g950f
      zone: zone.garaget
    - condition: zone
      entity_id: device_tracker.oneplus_a5000
      zone: zone.garaget
  action:
  - data:
      message: Mobilen är kvar i bilen
      title: Mobil i bilen!
    service: notify.mobile_app_oneplus_a5000

What the logg says:

image

The Zones

the log doesn’t say anything about device_tracker.oneplus_a5000.
and the funny thing is you’re sending a notification to it even if it’s in the garage :wink:

Max is the user wich uses image
Mårten is name-override for device_tracker.oneplus_500

Yes, I’m aware that I’m sending a message to the phone that is possibly in the garage, when I get this to work which it doesn’t now, a message will be sent to my wife’s phone (if she is at home) or if not the hallway lights will blink at me.

it’s a bit complicated - I don’t get what’s the difference between a user and a device tracker in your case (and it should be) and why both of them are persons in the logbook.
By the way, do you know why most of the times Marten and Max change status together but sometimes it does not happen?

By the way, have a look a this topic - maybe you have the same problem (username vs tracker id).

What type of device_tracker are those? To use the zone condition they must be GPS-based trackers.

It’s probably better to use this instead:

  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.sm_g950f
      state: Garaget
    - condition: state
      entity_id: device_tracker.oneplus_a5000
      state: Garaget

They are both the official Home Assistant App with it’s GPS location updates.

The persons all have their respective device_tracker listed under Configuration->Persons
So I guess that is why HA updates both the person and the tracker.

I’ve tried using the person.max instead of the device_tracker for location but that doesn’t seem to help. It still activates and says that it’s in the Zone Garaget but HA doesn’t say that I’m there.

Just look at the Log, Everyone is at home and still the OR-condition triggers?

When this happens, what does the states of the device tracker entities look like on the STATES tab of the Developer Tools page (including attributes)? Do they both have latitude, longitude & gps_accuracy attributes with reasonable looking values?

At least for diagnostic purposes, can you try changing to state conditions as I suggested above and let me know if that works any better?

So I tried changing to the State example above, didn’t change anything

My GPS shows me solely in my Home-zone and a gps_accuracy of 23.
I have longitude and latitude.

But I really don’t think that should matter at all. For that to have an effect the Zone should first change to Garage and then the condition would trigger.
Now it triggers even though the state is home… I’m beginning to suspect the conditions OR-clause is wrong
so maybe it’s like
phone is in zone garage
OR
nothing

and it’s always nothing so it goes through.
But since the logger doesn’t specify WHY something triggered it’s hard to know.

I find that hard to believe. Are you sure you reloaded and/or restarted after making that change?

Here’s the deal…

A zone condition is true if the entity (device_tracker or person, whichever you’re using with it) is considered in the zone. That is determined by looking at the location and size of the zone, as well as the location and gps_accuracy of the entity. If the circles defined by those values overlap, then the entity is considered in the zone.

However, when determining the state of an entity it’s possible for the entity to be “in” multiple zones (i.e., the entity’s “circle” is overlapping with more than one zone “circle.”) If this happens it needs to pick one of the zones which is the most appropriate. It does this by picking the zone whose center is closest to the entity’s GPS coordinates. For this the zone size/radius and the entity’s gps_accuracy are ignored. However, it’s then possible for the entity to be the same distance to multiple zones. (This can happen if two or more zones have the same location but different sizes.) If this happens then, of those zones it is closest to, it picks the one with the smallest radius.

So, what appears to be happening in your case is that the “location circle” (defined by the GPS coordinates and gps_accuracy) of one or both of your device_tracker entities is still overlapping zone.garaget even though they become closer to zone.home and therefore their state changes to home. So, although they are now home, they’re also still overlapping the garage zone so the zone conditions are also still true.

I’m pretty sure changing to the state conditions as I suggested will solve the problem. You have to make sure the automation is properly reloaded after you change it. You might want to restart HA, just to be sure.

1 Like

Yes, I did select “Reload Automations” under Server Controls after I changed the yaml file.
I believe that HA does that automatically when you change through the GUI.

Bullcrap on HA then If HA never tells me what conditions are true in the log for when it activates an automation how am I to figure out what is wrong.

It’s not worth using the HA App then. My wifes phone is currently on the kitchen table.
Google Maps says she’s at home (we share our coordinates).
But HA App says she is in the garage and has been for the last 16 minutes.

Can’t trust the app at all.

Is there a way to write HA code in python or some real language with proper encapsulation of statements instead of yaml?

It’s horrible that the indentation decides what goes together.

remove your conditions and call a python_script in your action section so you can use python to check conditions and be in full control of your automation.

Use the logbook.log service as one of the actions in the activating automation to establish what the conditions state is and why, or view the states section under Developer Tools to establish which condition(s) are the issue.

Do you have the YAML code for this?
Error while executing automation automation.if_phone_left_at_garage. Invalid data for call_service at pos 2: template value should be a string for dictionary value @ data[‘message’]

 data:
  message: {{ states.device_tracker.oneplus_a5000 }}
  entity_id: device_tracker.oneplus_a5000
  service: logbook.log

I don’t use it myself, but this thread has some.

Your error is related to using data when you need data_template I think.

1 Like

should be

data_template:
 message: "{{ states.device_tracker.oneplus_a5000 }}"
 entity_id: device_tracker.oneplus_a5000
 service: logbook.log

Thank’s,

all I get is

Logger: homeassistant.components.automation
Integration: Automation (documentation, issues)
First occured: 6:42:35 AM (1 occurences)
Last logged: 6:42:35 AM

Error while executing automation automation.if_phone_left_at_garage. Invalid data for call_service at pos 2: required key not provided @ data['name']

Is my notification code malformed somehow, it does work.

- id: '1582752041728'
  alias: If Phone left at garage
  description: ''
  trigger:
  - entity_id: binary_sensor.sensor_hallen_motion
    platform: state
    to: 'on'
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.max_oneplus5
      state: Garaget
    - condition: state
      entity_id: device_tracker.oneplus_a5000
      state: Garaget
    - condition: state
      entity_id: device_tracker.sm_g950f
      state: Garaget
  action:
  - data:
      message: Mobilen är kvar i bilen
      title: Mobil i bilen!
    service: notify.mobile_app_oneplus_a5000
  - data_template:
      message: "{{ states.device_tracker.oneplus_a5000 }}"
      entity_id: device_tracker.oneplus_a5000
    service: logbook.log

Looking at the services tab under Developer Tools in one of my HA instances, the logbook.log service requires a name parameter (not marked with optional):

I would try something like

  - service: logbook.log
    data_template:
      name: "OnePlus A5000 Device States"
      message: "{{ states.device_tracker.oneplus_a5000 }}"
      entity_id: device_tracker.oneplus_a5000
1 Like

AppDaemon

but it the TS cannot find out why the automation failed…