Trouble with History Stats/Binary Sensors

Trying to track some weekly heating usage. Have the appropriate binary sensors up and running. Can’t get history stats sensors to track them as expected. None of the history stats sensors show up under statistics or in the STATES section under developer tools. I’m sure I’ve overlooked something obvious, but it’s been couple days and nothing I’ve read has led me to a fix.

Here’s my configuration YAML. Any insight or help would be greatly appreciate.

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

input_datetime:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
    - name: HVAC Activity (Kids Floor)
      state: "{{ state_attr('climate.kids_floor', 'hvac_action')}}"
    - name: HVAC Activity (Living Room)
      state: "{{ state_attr('climate.living_room', 'hvac_action' )}}"
    - name: HVAC Activity (Family Room)
      state: "{{ state_attr('climate.family_room', 'hvac_action')}}"
  - binary_sensor:
    - name: "Heating On Living Room"
      state: "{{ is_state_attr('climate.living_room', 'hvac_action', 'heating')}}"
    - name: "Heating On Family Room"
      state: "{{ is_state_attr('climate.family_room', 'hvac_action', 'heating')}}"
    - name: "Heating On Kids Floor"
      state: "{{ is_state_attr('climate.kids_floor', 'hvac_action', 'heating')}}"

sensor:
- platform: history_stats
  name: Heat for Living Room (Weekly)
  entity_id: "binary_sensor.heating_on_living_room"
  state: 'on'
  type: time
  start: >- 
    '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  duration:
    hours: 168

- platform: history_stats
  name: Heat for Kids Floor (Weekly)
  entity_id: "binary_sensor.heating_on_kids_floor"
  state: 'on'
  type: time
  start: >- 
    '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  duration:
    hours: 168

- platform: history_stats
  name: Heat for Family Room (Weekly)
  entity_id: "binary_sensor.heating_on_living_room"
  state: 'on'
  type: time
  start: >- 
    '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  duration:
    hours: 168

Unless you made a error whilst copying, the indentation under sensor: is ‘off’ with two spaces

and I donot understand your duration … you set the start to ‘today’ (morning) midnight and then add 168 hours?
History Stats - Home Assistant (home-assistant.io)

Thanks for your response.

So as I understand it, you’re looking for this:

    sensor:
- platform: history_stats
  name: Heat for Living Room (Weekly)
  entity_id: "binary_sensor.heating_on_living_room"
  state: 'on'
  type: time
  start: >- 
    '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  duration:
    hours: 168

If I do that, it causes errors with my configuration.

If I’m misunderstanding your instructions, apologies for any confusion.

Returning it back to how it originally was provides no errors upon config check.

The Duration section was meant to show the stats for 1 week, or 168 hours. The “start” field specifies the start time for the calculation. In this case, the start time is set to the current time (using the “now()” function), but with the hours, minutes, and seconds set to 0. This effectively sets the start time to midnight on the current day.

The “duration” field specifies the duration over which the sensor will calculate the duration. In this case, the duration is set to 168 hours, which is equivalent to 7 days.

Overall, this sensor definition appears to be correct for calculating the duration that the binary sensor with the entity ID “binary_sensor.heating_on_living_room” has been in the “on” state over the past week, as I understand it. But please, let me know if there’s a better way. Thanks.

1 Like

I am not providing instructions just guidelines/comment so you can reflect on your choises and not pointless follow others (me).
For a weekly sensor you are already deviating from the doc…hence
Added to that you have copied the sensor details without proper indentation, so if this is the same in your yaml then it will not work at-all
not (!) providing the end-solution, I would expect this for a weekly sensor, look at indentation ans start/end (along doc)

sensor:
  - platform: history_stats
    name: Heat for Living Room (Weekly)
    entity_id: "binary_sensor.heating_on_living_room"
    state: 'on'
    type: time
    start: "{{ as_timestamp( now().replace(hour=0, minute=0, second=0, microsecond=0) ) - now().weekday() * 86400 }}"
    end: "{{ now() }}"