Understanding State Changes


I’m struggling to understand some state change transitions in a custom sensor I have made.

I have network and phone tracking setup using the HA ping tracker to detect a device on the network, as well as OwnTracks and HA App tracking. So each member of the household has three sources of location data. I combine these sources together using the ‘Persons’ section in the HA Config.

I then created a custom sensor using these sources in order to use custom icons and attribution dependant upon state eg.

     # Gails Location State
      friendly_name: "Gail"
      value_template: >-
        {% if states.person.gail.state == "home" %}
        {% elif states.person.gail.state == 'not_home' %} 
        {% else %}
          {{ states.person.gail.state }}
        {% endif %}
      icon_template: >-
        {% if states.sensor.location_state_gail.state == "Home" %}
        {% elif states.sensor.location_state_gail.state == 'Away' %} 
        {% else %}
        {% endif %}
        attribution: >-
          {% if states.device_tracker.gail.state == "home" %}
            Data from Network Tracker
          {% elif as_timestamp(states.device_tracker.gail_gailsphone.last_updated) > as_timestamp(states.device_tracker.a60pro.last_updated) %}
            Data from OwnTracks
          {% else %}
            Data from HA App
          {% endif %} 

This had all been working fine, apart from yesterday when I started seeing multiple state changes on one of these sensors. I have an automation which announces on Google Home devices who is home, so when for example location_state_gail changes to home the announcement automation runs.

Yesterday this announcement started running every 10 / 20 mins. Now the network ping tracker can be a bit flakey and fail to see a device when on the network, but this does not affect the state of the ‘person’ entity as it has multiple sources. However, when looking at the state history for the sensor, it has multiple ‘Changed to Home’ events listed in the history, but no other states listed…, so its changing from home to home…, which does not make sense to me. See below

Does anyone know what this could be caused by ?

I thought that if the state of a sensor was updated with the same state as its previous value, this was not a change ie. in order for the sensor to generate a ‘Changed to Home’ event, it had to be in a state other than ‘Home’ to do so ?

My automation also triggers on the transition from Away to Home, yet it still runs when no such transition has occurred (according to the state history)

- id: gail_comes_home
  alias: "Alert when Gail comes home"
  initial_state: true
    - platform: state
      entity_id: sensor.location_state_gail
      from: 'Away'
      to: 'Home'
    - service: script.turn_on
      entity_id: script.say_everywhere
          message: 'Gails home'