Total / Average multiple history_stats together

Hello, i’ve been trying to work on this the past few days trying countless configurations and researching on google, but I can’t seem to get it to work correctly.

I have a lovelace card that lists all the lights in the house and how many hours each light has been turned on using individual histort_stats configurations for each entity.

What i’m trying to achieve is creating an additional history_stat that provides an average of all the lights in the ‘on’ status.

Here is my most recent attempt which is not currently working:
configuration.yaml:

template:
  - sensor:
      - name: "Average_Lights"
        state: >
          {% set A = states('sensor.light_hs.office_recessed')| float %}
          {% set B = states('sensor.light_hs.kitchen_island')| float %}
          {{ ((A + B) / 2) }}

sensors.yaml
Note: This is just a sample, there are around 30 light entities i’m tracking with HS.
These are currently working fine.

- platform: history_stats
  name: dummy
  state: "dummy"
  entity_id: light.back_patio_fan
  <<: &daily_total
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

- platform: history_stats
  name: light_hs.back_patio_fan
  entity_id: light.back_patio_fan
  <<: *daily_total

- platform: history_stats
  name: light_hs.back_patio_recessed
  entity_id: light.back_patio_recessed
  <<: *daily_total

Lovelace Card:
This card works fine as well (Except the average bar), but adding it for reference.

type: custom:vertical-stack-in-card
cards:
  - type: horizontal-stack
    cards:
      - type: vertical-stack
        cards:
          - type: custom:auto-entities
            card:
              type: custom:bar-card
              max: 10
              severity:
                - color: '#40bf40'
                  from: 0
                  to: 3
                - color: '#bf9540'
                  from: 3.01
                  to: 6
                - color: '#bf4040'
                  from: 6.01
                  to: 10
              show_header_toggle: false
              title: Lights
              height: 16
              positions:
                value: inside
                indicator: outside
                icon: inside
                name: inside
            sort:
              method: state
              numeric: true
              reverse: true
            filter:
              include:
                - entity_id: '*light.hs*'
                - entity_id: '*average_lights*'
              exclude:
                - state: <= 0

It’s not working because an entity’s name (its object_id) can never contain a period.

The names of your History Stats sensors contain periods:

name: light_hs.back_patio_fan
              ^
name: light_hs.back_patio_recessed
              ^
name: light_hs.back_patio_recessed
              ^

When Home Assistant creates the History Stats sensor, it replaces the period with an underscore. Therefore the entity_id of this History Stats sensor:

name: light_hs.back_patio_fan

becomes:

sensor.light_hs_back_patio_fan

and not:

sensor.light_hs.back_patio_fan

That’s why your Template Sensor fails to work because it references entities that don’t exist. Double-check the entity_id of your History Stats sensors in Developer Tools > States and you’ll see their names have had the period replaced by an underscore.

I suggest you replace this:

          {% set A = states('sensor.light_hs.office_recessed')| float %}
          {% set B = states('sensor.light_hs.kitchen_island')| float %}

with this:

          {% set A = states('sensor.light_hs_office_recessed')| float(0) %}
          {% set B = states('sensor.light_hs_kitchen_island')| float(0) %}

Ughh - I figured it was something small - I didn’t realize the system did an auto-replacement of the periods. Adding the (0) after float, is that for rounding?

One additional question, I have around 40 stats I want to average, but only those who are above 0 so the average isn’t too skewed…is there a way to make the syntax so I don’t have to list each HS individually - almost like the auto-entities I use in Lovelace?

Thank you so much for the help!

No, that’s a default value. In the upcoming December release of Home Assistant, it will be mandatory to provide a default value. In the event float is unable to convert the supplied value, it will report a default value. Traditionally float would report 0 but now you must indicate what value you want it to use. That’s why I suggested you use float(0) to indicate it will ise 0 as the default value.

For more information, refer to this:

I suggest you create a group containing the 40 History Stats sensors in order to simplify the template that will calculate the average of all sensors whose value is greater than zero.

template:
  - sensor:
      - name: "Average_Lights"
        state: >
          {{ expand('group.your_group') | map(attribute='state')
              | map('float', 0) | select('gt', 0) | average | round(2) }}