Issue with smartphone tracking, presence detection and Life360, and more specifically with offline devices

Hello Wizards!

It has been just a few weeks since I started playing around with my home automation software, and I am already stuck in a trap because of my lack of knowledge around the libraries of this platform. :slight_smile:

I started with a raspberry PI 4 but I realized very quickly that this environment wouldn’t be able to scale to the level that I was expecting and this was the reason why I decided to migrate my configurations to a docker container in my QNAP NAS server.

The issue that I am facing is related to smartphone tracking, presence detection and Life360. I started using Life360 to help me track my family smartphones but I realized that the quality of the sensor is questionable. During my research, I found an article from Phil Hawthorne where he explains how to enhance the HA presence detection mechanism through a state machine.

My implementation is very similar to what he suggested initially but I decided to introduce two additional sensors to help me filter part of the noise associated with Life360 and HA Companion App. I don’t know if there is a problem with my implementation but I noticed that both platforms don’t deal well with offline devices (smartphones) and weak connections.

Below is one of my routines from the automations.yaml file and how I am dealing with the state changes:

- alias: Update  Device Tracker Status
  id: update_device_tracker_status
  initial_state: on
  trigger:
    - platform: state
      entity_id: device_tracker.life360
  action:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.status_dropdown
        option: >
          {% if is_state('device_tracker.life360', 'Home') %}
            {% if (as_timestamp(now())-as_timestamp(states.device_tracker.life360.last_changed)) < 300 %}
              Home
            {% else %}
              Offline
            {% endif %}
          {% else %}
            {% if is_state('device_tracker.life360', 'Away') %}
              {% if (as_timestamp(now())-as_timestamp(states.device_tracker.life360.last_changed)) < 300 %}
                Away
              {% else %}
                Offline
              {% endif %}
            {% else %}
              {% if is_state('device_tracker.life360', 'Work') %}
                {% if (as_timestamp(now())-as_timestamp(states.device_tracker.life360.last_changed)) < 300 %}
                  Work
                {% else %}
                  Offline
                {% endif %}
              {% else %}
                {% if is_state('device_tracker.life360', 'moving') %}
                  Moving
                {% else %}
                  {% if is_state('device_tracker.life360', 'driving') %}
                    Driving
                  {% else %}
                    Unknown
                  {% endif %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %} 

But when I start my HA instance, I am getting the following error message:

2020-09-24 18:20:42 ERROR (MainThread) [homeassistant.components.automation.update_device_tracker_status] Update Device Tracker Status: Error executing script. Service not found for call_service at pos 1: Unable to find service input_select/select_option
2020-09-24 18:20:42 ERROR (MainThread) [homeassistant.components.automation.update_device_tracker_status] While executing automation automation.update_device_tracker_status
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 426, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 413, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1265, in async_call
    raise ServiceNotFound(domain, service) from None
homeassistant.exceptions.ServiceNotFound: Unable to find service input_select/select_option

Does anyone have any idea about what may be going wrong here?

What version of HA are you running? Did you create the input_select.status_dropdown? If you go to Developer Tools -> Services, do you see the service input_select.select_option there?

Home Assistant 0.115.2.

I checked Developer Tools -> Services and I did not find the input_select object in the dropdown. I created the file manually but it does NOT seem to be working properly. What am I missing here?

So do you see the input_select.status_dropdown under Developer Tools -> States? Did try to restart already?

Yes, I tried to restart a few times before but the only way that seems to be working is when I have the input_selects inside of my configuraiton.yaml. Initially, I was using a file called input_select.yaml.

Everything seems to be working now but the algorithm that I created above to manage the states is not working. I don’t know what is the return of is_state(‘device_tracker.life360’, ???), but my algorithm is returning “Unknown” in 99% of the cases.

Do you see anything wrong with my code? Thanks.

what are the different states for device_tracker.life360? are you sure that they are in capital letters? you can check under Developer Tools -> States

Hi,

I created an automation as follows. I hope it works for you.

alias: Zone Notification
description: ''
trigger:
  - platform: state
    entity_id: person.ahmet
    attribute: gps_accuracy
  - platform: state
    entity_id: person.mehmet
    attribute: gps_accuracy
condition:
  - condition: template
    value_template: >-
      '{{ trigger.from_state and trigger.to_state and trigger.from_state.state
      != trigger.to_state.state }}'
action:
  - service: >-
      notify.{% if trigger.to_state.attributes.friendly_name == 'Mehmet' %}mehmet_phone {% elif
      trigger.to_state.attributes.friendly_name == 'Ahmet'
      %}ahmet_phone{% endif %}
    data:
      title: '{{ trigger.from_state.attributes.friendly_name }} konum değiştirdi'
      message: >-
        {% if trigger.to_state.state != 'not_home' %}{{ trigger.to_state.state
        }}; {% endif %} {% if trigger.to_state.attributes.friendly_name ==
        'Ahmet' %}{{ states('sensor.ahmet_phone_geocoded_location') }} {%
        elif trigger.to_state.attributes.friendly_name == 'Mehmet'
        %}{{ states('sensor.mehmet_phone_geocoded_location') }} {% endif %}
mode: single