Home/Away Stats Tracking

I was trying to set up a quarantine meter that I saw on Reddit. It seemed to work but the number of hours away from home seemed way off. So tracking it down it sees that my home/away stats are off. There are 336 hours in 14 days. but some reason my tracking sensors are always 90 or so hours off. Not sure why they are missing. To determine home/away I am just using bluetooth connection to my RPi. there is one corner of my apartment where BT does drop out (another issue for another topic), but it should set me to not_home. Which still should add into the 336 hours. Current values show away_stats=19.32 and home_stats=221.55. total of 240.85 with a missing 95.15 hours. Was I abducted by aliens during this time?? Where did this time go?

I am using these as the sensor for testing. Is there another state for a person entity besides not_home and home?? Figure I have something wrong with my person entity which is creating bad values in the final calculations.

- platform: history_stats
  name: away_stats
  entity_id: person.me
  state: "not_home"
  duration:
    days: 14
  end: "{{ now() }}"

- platform: history_stats
  name: home_stats
  entity_id: person.me
  state: "home"
  duration:
    days: 14
  end: "{{ now() }}"

And this is the full sensors for the quarantine meter. Mainly for fun but also to understand how HA is working.

- platform: template
  sensors:
    hours_outside_per_day_me:
      friendly_name: Hours Outside
      value_template: >
        {% set d, h, m = state_attr("sensor.quarantine_meter_me", "value").split() %}
        {% set d, h, m = d[:-1] | int, h[:-1] | int, m[:-1] | int %}
        {% set hours_outside = ((14 - d) * 86400 - h * 3600 - m * 60) / 3600 %}
        {{ (hours_outside / 14) | round(1) }}
  
- platform: history_stats
  name: Quarantine meter Me
  entity_id: person.me
  state: "home"
  type: ratio
  duration:
    days: 14
  end: "{{ now() }}"

- platform: history_stats
  name: Quarantine meter times left
  entity_id: person.me
  state: "home"
  type: count
  duration:
    days: 14
  end: "{{ now() }}"

How many days do you have the recorder set up to retain?

There’s this note in the History Stats sensor docs:

If the duration exceeds the number of days of history stored by the recorder component ( purge_keep_days ), the history statistics sensor will not have all the information it needs to look at the entire duration. For example, if purge_keep_days is set to 7, a history statistics sensor with a duration of 30 days will only report a value based on the last 7 days of history.

Going on your 240.85 hours. I’m guessing you only retain 10 days.

Hmm I never set the recorder timeframe. is that set per sensor? or as a standard default for what is stored in the DB? Sorry, still new to HA but that sounds like the problem. I would need to add something like this to my confg yaml? or would I need to adjust the sensor in my sensors.yaml. Worth noting that I have sensor: !include includes/sensors.yaml in my config yaml already.

sensor:
  - platform: history_stats
    name: Quarantine meter me
	entity_id: person.me
    state: "home"
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
	duration:
	  days: 14

It’s a global setting for all states and events.

The default is 10 days :slight_smile:

All you need is this in your configuration.yaml file:

recorder:
  purge_keep_days: 14

Note, that as your system grows you might like to start excluding things that you have no interest in keeping historical data for. This reduces the size of the database and reduces the number of writes to the database. Which is especially important if using an SD card with finite write endurance.

Ok great. that will probably do it then. I will have to wait a few days to see how the stats add up. Had no idea about the recorder. I will look into what I can exclude from the record to limit DB writes.