Device tracker automation

Hi Guys!

I’m new in HA and I want to set an automation, what is simple I think, but I don’t know and can’t find perfect sample for me.
My device tracking is works well.

This is in my configuration.yaml:

device_tracker:
  - platform: xiaomi
    host: 192.168.31.1
    password: **********

This is in my known_devices.yaml:

huawei_mate_8:
  hide_if_away: false
  mac: **:**:**:**:**:**:**:**
  name: HUAWEI Mate 8
  picture: https://home-assistant.io/images/favicon-192x192.png
  gravatar: ******@gmail.com
  track: true
  vendor: HUAWEI TECHNOLOGIES CO.,LTD

It works well. But I don’t know how can i set the trigger.
Action is when my device arrived HOME, and presence switch home frome away, than my Google home (set volume 4, than) say Welcome home Michael. Action is works fine when trigger is a time or something simple. But the trigger bad. What is wrong?

This is my automation in configuration.yaml:

automation Welcome:
  alias: Michael
  trigger:
    platform: state
    entity_id: known_devices.huawei_mate_8
    state: home
  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room_home
        volume_level: "0.4"
    - service: tts.google_say
      data:
        entity_id: media_player.living_room_home
        language: en
        message: Welcome home Michael.

Switch the trigger section up like so:

trigger:
  platform: zone
  event: enter
  zone: zone.home
  entity_id: device_tracker.huawei_mate_8

See if that works.

1 Like

WOW, Thanks! I will try it!

your state needs quotes

  trigger:
    platform: state
    entity_id: device_tracker.ryans_iphone_app
    state: 'home'

I’m not sure you would want to use “state” as the platform on this automation anyway. Once he is ‘home’ then going by state it may cause the automation to infinitely loop until he leaves that state. I could be wrong but going with the zone platform and event being enter it should only trigger one time when he enters the home zone.

@oembob

I am no expert and can see where either solution would work. Not sure if one is better than the other or not.

I was only providing the quote suggestion based on the notes here.

@oembob This zone thing is not working, but (@rabittn solution) state is working. Are you sure this will be create an infinitely loop?
I need a solution for this situation with device tracker.

I need when switch to home from away. This switch is one time… Can I do it?

I have many automations that use from and to states without an infinite loop.
When coming home.

  trigger:
    platform: state
    entity_id: device_tracker.ryans_iphone_app
    from: 'not_home'
    to: 'home'

When Leaving the house

  trigger:
    platform: state
    entity_id: device_tracker.ryans_iphone_app
    from: 'home'
    to: 'not_home'

No I’m not sure that it will happen, which is why I said “may”. If the other solution works, then stick with it.

I’ve just found it helps to consider any coding as literal as possible since that is usually how the system will read it.

The difference here is you have the

from: 'home' 
to: 'not_home'

entries in yours. Which means it is looking for a state change. The OPs code does not have that so it is just looking for a state of “home” to activate it.

I only said it “may” cause an infinite loop of activations because I wasn’t sure if it would or wouldn’t. I just try to keep code as specific as possible to avoid issues like that.

For reference, here is one of my working automations using the zone platform instead of state:

alias: 'Chrissy Arrive Work'
trigger:
  platform: zone
  event: enter
  zone: zone.barclaycard
  entity_id: device_tracker.iphoneha
action:
  service: notify.HomeAssistantFE
  data:
    message: 'Chrissy has arrived at work'
1 Like

this might be the key for why its not working. Looks like zone enter only works for GPS enabled trackers.

@oembob Oh, OK. Sorry, my english is soooo weak :slight_smile:
It is working just fine, I think “from to” is a good solution. Nothing repeat.

  trigger:
    platform: state
    entity_id: device_tracker.huawei_mate_8
    from: 'away'
    to: 'home'

And now I have a new question, because you are so smart and helpful guys :wink:

What then, when I want to run this automation if “from away to home” AFTER SUNSET.

Can I use more platform in the trigger? Or how can I set the logic “AND”?

  • platform: sun
    event: sunset

Yes it is possible to have multiple triggers. See here:

If you only want this action to fire after sunset you want to add a condition.

Trigger: If a device comes home

Condition: it is after sunset.

action: turn on the light

  condition:
     - condition: state
       entity_id: sun.sun
       state: 'below_horizon'
1 Like

GREAT! Thanks guys!
And the last one :smiley:

I live with my partner. What I have to do when I want an action only happen when both of us phones left the house?

I have my family device tracker entities all in a group called “family”. I then use the state condition. Here is my example:

alias: 'Motion when nobody home'
trigger:
  platform: state
  entity_id: binary_sensor.living_room_motion_motion
  state: 'on'  
condition:
  condition: state
  entity_id: group.family
  state: 'not_home'
action:
  service: notify.HomeAssistantFE
  data:
    message: 'Motion detected in the Living Room'

This is great, but this group thing is a mistery for me a while.
How do you create a group? In groups.yaml file?
How does a group look like?

Groups are pretty easy. Yeah, you use the groups.yaml file. More info with examples here:

OK, thanks guys! You are very helpful and patient with me. I’m so grateful.

1 Like