History stats not appearing

I set up some sensors to combine state for my thermostats. Now I want to see historical data on them. I’m having trouble having the history_stats sensors show up in Home Assistant. I don’t see any errors in the logs, and the YAML checker passes. Does it just take time for history_stats to appear?

configuration.yaml:


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template: !include template.yaml
sensor: !include sensors.yaml
entity_controller: !include entitycontroller.yaml

template.yaml:

sensor: 
  - name: Upstairs Thermostat State
    state: "{{ state_attr('climate.upstairs_thermostat', 'hvac_action') }}"
  - name: Downstairs Thermostat State
    state: "{{ state_attr('climate.downstairs_thermostat', 'hvac_action') }}"
  - name: Overall Thermostat State
    state: >
      {% set hvac = [states('sensor.upstairs_thermostat_state'), states('sensor.downstairs_thermostat_state')] %}
      {% set num_heating = hvac | select('eq', 'heating') | list | count %}
      {% set num_cooling = hvac | select('eq', 'cooling') | list | count %}
      {% set num_idle = hvac | select('eq', 'idle') | list | count %}
      {% if num_heating > 0 and num_cooling > 0 %}
        mixed!
      {% elif num_heating > 0 %}
        heating
      {% elif num_cooling > 0 %}
        cooling
      {% elif num_idle > 0 %}
        idle
      {% else %}
        off
      {% endif %}

sensors.yaml:

# HVAC Stats
- platform: history_stats
  name: HVAC Heating Last 24 Hrs
  entity_id: sensor.overall_thermostat_state
  state: heating
  type: time
  end: "{{ now() }}"
  duration:
    hours: 24

- platform: history_stats
  name: HVAC Cooling Last 24 Hrs
  entity_id: sensor.overall_thermostat_state
  state: cooling
  type: time
  end: "{{ now() }}"
  duration:
    hours: 24

I believe they need a device class or unit_of_measure key in order to be stored in history.

Are you sure? I’ve written my yaml based on some examples that seemed to have worked for others recently: https://www.reddit.com/r/homeassistant/comments/199bgvy/comment/kidy7ir/

And if that’s true, what device_class/unit_of_measure would be appropriate for this type of data? Maybe a binary_sensor?

I’m not sure if I did anything to fix it, but it seems to have added the history stats entities after a full HA restart.

If you didn’t have any history stats sensors before, then that would explain it: when using a platform for the first time you cannot use the reload config functionality — you must restart HA.

Yup, I see where it says to restart in the documentation now :person_facepalming:

Unfortunately, even though the history sensors now appear, they always have a state of 0.0 even when confirming that the sensor state im tracking is on.

Is there a good way to debug this? I don’t see anything in the logs. I’m tempted to play around with a fork on github at this point.

oh man, figured it out after pulling down the code and attaching a debugger. I was bitten by YAML boolean syntax :confused:

I changed my YAML from above to one using binary_sensors. This was what my config for one of the history_stats looked like:

- platform: history_stats
  name: HVAC Heating Last 24 Hrs
  entity_id: binary_sensor.any_thermostat_is_heating
  state: on
  type: time
  end: "{{ now() }}"
  duration:
    hours: 24

Zoom into the problem:

  state: on

This sets the state matcher to True, which does not match the "on" state. Hours wasted over missing quotes :cry:. It should have been

  state: "on"
1 Like