Tracking my movements using my iphone/ios/icould3 as I move from zone to zone

I have installed ios and icloud3…and put about 10 zones in my configuration.yaml file. I would like to trigger an automation to say “Harry is at Home Depot” or "Harry is leaving home depot on our echo dots. I plan to enter many more zones and I want the Echo Dot’s to say the correct zone.

For testing, I’m just trying to track me…and I am running home assistant on my iphone and have duckDNS installed. I know how to do the notify TTS part on the echos, but I am stumped on how to write one automation trigger that will work for multiple zones…unless I list each zone separately.

Hopefully, this makes sense…in fact I thought I saw someone post code to do this although I recall they were sending a text message somewhere. I can handle the echo dot TTS part, but I need help on the trigger(s).

I’ve got an automation that pretty much does this. It will require further tweaking on your part to get it exactly how you want it, but should get you most of the way I think.

automation:
  - alias: "Zone Change"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: 
          - device_tracker.<Your_Device_Tracker_Here>
          - device_tracker.<Optional_Second_Device_Tracker_Here_For_Other_Family_Member>
    condition:
      - condition: template
        value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
    action:
      - service: notify.mobile_app_sean_s_iphone_xs
        data_template:
          title: "Zone Update Alert"
          message: > 
              Update from {{ trigger.to_state.attributes.friendly_name }} - {{ trigger.from_state.state }} to {{ trigger.to_state.state }}

I would suggest using it like this first, so you can see exactly how it works and what you’d like to adjust before doing the TTS part.

A further thought…In the sample automation above, SeanM uses device_tracker.xxx entities. I would use the
sensor.xxx_zone_name1 entities for the trigger. The sensor.xxx_zone_name1 formats the zone name (home=Home, not_home=Away, work=Work, etc) where xxx is the devicename for your phone then you can easily use formatted to_state & from_states in the notification message.

See docs on special zone names for iCloud3 here

The messages passed to the notify service are already formatted.

data_template:
    title: "Zone Update Alert - {{sensor.badge.attributes.friendly_name }}"
    message: > 
        From zone {{trigger.from_state.xxx_zone_name1 }} to zone {{ trigger.to_state.xxx_zone_name1 }}
1 Like

removing post until I check one more thing…

I am unable to get your code to work!

states lists my device_tracker.d_iphone with all the fields appearing to be correct.

If I replace your From zone … line with

'Home Alarm Triggered {{now().strftime("%Y%m%d-%H%M%S")}}'

I get a notification on my phone.

It seems that if I replace the timer expression with any attribute (sensor) value from the device tracker I don’t get nothing.

Overview shows all of the fields with their friendly name zone: Home and last zone: not_home

I have tried the following (and many variations) and it doesn’t send anything even though, as I said, there is a badge for zone: HOME.

'Home Alarm Triggered {{sensor.d_iphone_zone}}'

Could you post a one line message: that will notify my ios device that the iphone is in xxxx zone or one that says the iphone has just left (last_zone).

The only “clue” I can find is that when I look at the list of entities in the configurator, I see

Undefined
(sensor.d_iphone_zone)

Any thoughts???

You need to use the device name for your phone. In an earlier post, you had d_ iphone, dougs_iphone, dgd_iphone,xxx_iphone, etc. They should all be the same name which can be found on the Settings page (device I’d) in the ios app and the device_tracker entities list in HA …

I am using the same device name. I’ve just changed it randomly when posting (for security). I just uninstalled ios and icloud3 and home assistant companion…and reinstalled them one at a time…so I have a clean install…and I changed all references to be identical to my device_tracker.myiphonename.

NOTE: looking at ios.conf, I see the device name is: Myiphonename…the first letter is capitalized which is actually the name I have given my iphone and the name that appears on the apple cloud. I don’t know why one is lower case and the other capitalized. I used the lower case version since that’s the one used in device_tracker.

NOTE2: I am using the officially posted version of your code, not the updated version you sent me.

NOTE3: in the icloud3 folder, I services.yaml is empty. I tried copying it, but the duplicate command: lines threw an error. If something has to be in this file, then maybe that’s the problem.

These are my automations and scripts for arriving home for you to use as working examples.

  • My iPhone name in Settings>About is ‘Gary-iPhone’.
  • The iosapp device id is ‘gary_iphone’.
  • The device_tracker entity that ties everything together is ‘device_tracker.gary_iphone’.
#--------------------------------------------------------------
#   Gary arrives home automation
#--------------------------------------------------------------
- alias: Gary Arrives Home
  id: gary_arrives_home
  trigger:
    - platform: state
      entity_id: sensor.gary_iphone_zone_name1
      to: 'Home'

    - platform: template
      value_template: '{{states.sensor.gary_iphone_distance.state | float <= 0.2}}'
      
  condition: 
    - condition: state
      entity_id: input_boolean.gary_driving_flag
      state: 'on'
      
  action:
    - service: script.gary_send_message
      data_template:
        title: 'Gary Arrives Home'
        message: 'Zone={{states.sensor.gary_iphone_zone_name1.state}}, 
                  Zone2={{states.sensor.gary_iphone_zone_name2.state}}, 
                  Zone={{states.sensor.gary_iphone_zone.state}},
                  Distance={{states.sensor.gary_iphone_distance.state}},
                  Trigger {{trigger.from_state.state}} to {{trigger.to_state.state}}'
            
    - service: script.gary_arrives_home
#-------------------------------------------------------------
#   Gary Scripts - Arrive Home
#-------------------------------------------------------------
gary_arrives_home:
  alias: 'Gary Arrives Home'
  sequence:
    #Open garage door if driving flag is on
    - service: script.open_garage_door 
    
    #Turn off 'Away' flags
    - service: input_boolean.turn_off
      entity_id: input_boolean.gary_driving_flag
    - service: input_boolean.turn_off
      entity_id: input_boolean.gary_far_away_flag
    
    #Change Gary badge to home
    - service: mqtt.publish
      data_template:
        topic: 'location/gary' 
        payload: 'home'
        
    #If already home, do not issue 'zone home' command
    - condition: template
      value_template: '{{states.device_tracker.gary_iphone.state != "home"}}'
    
    #Issue 'zone home' command
    - service: device_tracker.icloud_update
      data:
        account_name: gary_icloud
        device_name:  gary_iphone
        command:     'zone home'
#-------------------------------------------------------------
#   Gary Scripts - Send Message to Gary - Central Notify Command
#-------------------------------------------------------------
gary_send_message:
  alias: 'Send Message to Gary'
  sequence:
    - service: notify.ios_gary_iphone
      data_template:
        title: "{{ title }} (IOS)"
        message: "{{ message }}"
          
#    - service: notify.mobile_app_gary_iphone
#      data_template:
#        title: "{{ title }} (mobile_app)"
#        message: "{{ message }}"

I’ve included zone, zone_name1 & zone_name2 in the sample message for the Arrive Home automation so you can see how each would be configured. I also didn’t give you all the scripts but the one you can look at for the message is the ‘- service: script.gary_send_message’ line with the title & message. Note the placement of the single-quote marks (’) at the beginning and end of the message and the ‘.state’ at the end of the {{sensor.gary_iphone_zone_name1.state}}.

Hope this helps.

The ‘script.gary_arrives_home’ is a script to open my garage door, turn off some away flags and send some other messages. I’ve included them to give you another example.

I also have a services.yaml and it contains the descriptions of the services that are created by iclou3 for documenttion purposes.It is not used for anything at this time and can be ignored.

1 Like

The following automation works only when I trigger it from the overview panel in HA. It does not work when driving between zones…or leaving home…it also does not work when I do a one shot location send from HA Companion.

  • alias: “Zone Change”
    initial_state: ‘on’
    trigger:
    - platform: state
    entity_id:
    - device_tracker.doug_iphone
    condition:
    - condition: template
    value_template: ‘{{ trigger.from_state.state != trigger.to_state.state }}’
    action:
    • service: media_player.alexa_tts
      data_template:
      entity_id: media_player.douglas_s_echo_dot
      message: >
      ‘Doug has left {{states.sensor.doug_iphone_last_zone.state}} and is now at {{states.sensor.doug_iphone_zone.state}}’
    • service: notify.ios_doug_iphone
      data_template:
      message: >
      ‘Doug has left {{states.sensor.doug_iphone_last_zone.state}} and is now at {{states.sensor.doug_iphone_zone.state}}’

Perhaps I should be triggering on something else?? does the condition template look right?

Note: when I use the configurator and look at entities, the icloud3 entiities all say “unidentified” but then give what looks like valid field names. Is that the way it should be? If I look at states under development tools in HA, all the icloud3 states are listed as separate items with valid states.

In IOS companion, all location settings are on other than beacons. Home Assistant Companion always has access to location, background app refresh on, cellular data on.notifications all on.

I do see in the log that zone change occasionally is triggered. At one point (a month or so ago), As I drove around, I would see location change sent messages as I entered/left zones…I am not seeing that now. I have removed HA companion and restarted iphone…then reinstalled iphone companion.

Next step is to toss in the towel and do a complete reinstall of everything, essentially starting with a clean home assistant.

I don’t think nuking everything and starting over is the answer.

  1. Are you using the automation tool to create your automations or creating a .yaml file with the code in it? I use a yaml file (better options, can create one automation from another, easier editing, etc.). I tried the built-in tool when it first came out and didn’t like it.
  2. Start simple and build from there. Take the sample automations and scripts I use (in a previous post), change the gary_iphone to doug_iphone and get it working. Forget alexia for now until you can get a notification that you have changed zones. Start with the ‘doug_leaves_zone’
  3. Make sure the notifications are turned on in the IOS app. If not, turn them on for everything. If that dowsnt give you a notification, than it is not notifying HA so there is nothing for me to pick up that a zone has changed.
  4. Comment on your automation:
    4.1. I would use the sensor.doug_zone_name1 as the entity. That eliminates false device_tracker state notifications due to gps wandering and accuracy errors.
    4.1 You do not need the trigger.from and trigger.to condition. It will always be different. That is what triggered the automation in the first place.
    4.2 The log file has entries when automations are triggered and scripts are run. It also shows if a condition that stops a script is false.
  5. Are you in a ‘dead zone’ as far as cell coverage is concerned? If so, the IOS app will not be able to trigger a zone exit.
  6. Is your GPA accuracy poor? iCloud3 has a sensor.poll_count that I display on the screen. The format is #:#:#. The first # is the icloud locator count, the second # is the number of triggers and zone enter/exits that have been handled and the third # is the old location/poor gps accuracy count.

I was just out running around and all the notifications were displayed when I changed zones using the above automations & scripts so I know they work.For me at least.

  1. I am using the configuration addin…editing the yaml file directly.
  2. The actions work, so I only need to get the trigger side working…
  3. Notifications are all on in HA companion on my phone
  4. I will make those changes. When you say log file…there are many logs…which one should I be looking at?>
  5. no dead zone…good verizon coverage.
  6. I’ll have to look at that…I’ll put it back in lovelace so it’s easy to see

Thanks for helping…I think this will be a useful automation for others when I actually get it working.

question: should the send manual location once button in HA companion trigger the automation?

The full HA log file is ‘config/home-assistant.log’ or you can click the circled-i in the bottom-left of the developers tools, scroll to the bottom and click ‘Load Full Home Assistant Log’.

The IOS app Manual Trigger will send a ‘Manual’ trigger which will be picked up by iC3 as a location change. If the zone does not change, it will not trigger an automation.