History_stats integration availability question

Hi there,

I started using the history_stats recently, here’s my entry in configuration.yaml:

  - platform: history_stats
    name: Ethan nap duration today
    entity_id: binary_sensor.ethans_room_motion_occupancy
    state: "off"
    type: time
    start: "{{ states('input_datetime.nap_mode_started') }}"
    end: "{{ now() }}"

When HA starts up, it prints this error:

Platform history_stats not ready yet: Parsing error: start must be a datetime or a timestamp: could not convert string to float: ‘unknown’; Retrying in background in 30 seconds

5:59:59 AM – (WARNING) Sensor

Error fetching Ethan nap duration today data: Parsing error: start must be a datetime or a timestamp: could not convert string to float: ‘unknown’

5:59:59 AM – (ERROR) history_stats

This makes sense to me, since it’s trying to load it before the date time helper is available I would guess.

Normally in a template sensor I’d just have that date time sensor in the “availability” section.

is there an equivalent for history_stats, or if not, what am I doing wrong?

Thanks!
matt

Maybe try to add a default filter? So it’ll use a known value if your input_datetime isn’t defined.

start: "{{ states('input_datetime.nap_mode_started') | default(as_datetime(0)) }}"
1 Like

This may be related to a long-running problem with history_stats.

Sometimes, if whatever state you’re tracking hasn’t changed in a while (maybe since HA was started) and it suddenly gets a value, it will total up the time since midnight instead of just the time since the state changed.

For me this most recently happened with my “AC on Today” counter. I hadn’t run the air conditioner for a few days, probably since the last time HA started. Then I turned it on one day when it got hot.

The value of this entity totaled up the time the AC ran, PLUS the time at which the sensor changed. For example, if I turned it on at 14:03 and it ran for 15 minutes, the entity would show “14:18” for a total run time that day. Here’s the configuration.yaml entry:

  - platform: history_stats
    name: AC on today
    entity_id: sensor.1st_floor_action
    state: 'cooling'
    type: time
    start: '{{ today_at() }}'
    end: '{{ now() }}'

I suspect the problem is with the value being undefined, then changing to “cooling.” I probably hadn’t run the heat or air conditioner on that thermostat since HA was last restarted. I didn’t review the logs at the time, but perhaps it had the same warning as shown in the OP.

This has been discussed here before, but I couldn’t locate the thread with a quick search.

1 Like

@TacocaT hmm that did not work for me :confused: