Calculating daily power usage for all relevant sensors

Hi All

I’m trying to figure out how to sum up the daily usage of power from all the sensors that deliver that value.
So far I’ve taken this from another thread and modified it slightly:

{{ states.sensor|selectattr('attributes.unit_of_measurement', 'equalto', 'kWh')|rejectattr('entity_id', 'equalto', 'sensor.power')|map(attribute='state')|map('float')|sum }}

But it takes too many sensors, and thus gives too high a value (as there are also yearly sensors in some of the entities).

These entries (please disregard the utility_meter type, as they are still not correct):
daily_powerusage_sensors

I then created a group, to only take the relevant ones:

{{ states.sensor|selectattr( 'entity_id','in',state_attr('group.daily_power_usage','entity_id')) | rejectattr('entity_id', 'equalto', 'sensor.power') | map(attribute='state')|map('float')|sum }}

This seems to give me the correct result, but is it really necessary to do a group, or can I filter the name of the sensor by ‘_daily_energy’ or something like that?

if you want to go the _daily_engery route without an entity list, you’d have to do a for loop and use namespace to add up the results and divide by the count. It won’t be a “pretty one line expression”.

{% set ns = namespace(total=0, count=0) %}
{% for s in states.sensor %}
{% if s.entity_id.endswith('_daily_energy') %}
{% set ns.total = ns.total + s.state | float %}
{% set ns.count = ns.count + 1 %}
{% endif %}
{% endfor %}
{{ ns.total / ns.count }}
2 Likes

Ok, I’ll KISS and just make a group then :smiley:
Btw. why would I divide by the count, that will just give me an average, won’t it?

Yes, I was not fully reading your comment and assuming. My bad.

1 Like

Wouldn’t a sum (custom) component be a good idea? I don’t understand someone has not developed that already.

1 Like

cause it’s super simple with a template. Why reinvent the wheel. Especially if people want to glob for entity_id’s anyways

I am trying to achieve something quite similar and thought I could tweak the above template to use the relevant attribute of my LightWaveRF Gen 22 sockets and lights but am currently failing.

Can anyone see what I am likely doing wrong in this value_template?

{{ states.sensor|selectattr('attributes.unit_of_measurement', 'equalto', 'W') | 
rejectattr('entity_id', 'equalto', 'sensor.total_power') | 
map(attribute='attributes.current_power_w') | 
map('int')|sum }}

Thanks in advance for any thoughts.

Maybe I missed it but nothing seems wrong with it. Paste it into the Template Editor and, starting from the end of the template, remove each filter in succession until you start seeing results. That will help narrow down which filter is the culprit.

Now I really feel like a newbie - I didn’t realise there is a template editor.

Thank you, looking

Yup. Handy tool this!

definedError: 'mappingproxy object' has no attribute 'current_power_w'

I interpret this error message as saying that not all sensors which have a unit_of_measurement of “W” have an attribute of current_power_w - which is certainly true. My conclusion is that I should try setting up a group and iterating round it (I think I saw a method for that somewhere else on the forum.)

Create a group and then use expand, like this:

expand('group.xxxx') | rejectattr(....)

That looks helpful, thank you.

Well, working lovely now - thank you :slight_smile:

1 Like

Can you share how you achieved all this? Especially how did you convert from W to kWh and the monthly reset.

It’ll be very helpful.

1 Like

Although,my sensors are reporting in W, not kWh. Maybe I’m just dumb, but would it automatically convert it to kWh?

Oh I forgot the link to the integration sensor:

This is for W to kW/h and the other is for the monthly reset.

1 Like

Very happy to - although that conversion comes ‘for free’ with the Lightwave sockets and lightswitches. Each device has a current W and kWh and resets at the end of each month which I just use templates to add up. There are some reliability difficulties with some of the devices but nearly all work. I’ll write a longer post describing the other bits in a bit.

1 Like

Although it took a while to do, there is definitely no rocket science here.

In outline (and ignoring the complication that I am having to do it separately for two parts of the property), I

  1. Used LightwaveRF Gen 2 light switches and sockets around the house.
  2. Created a group for the sockets and switches in groups.yaml
  3. Create template sensors to add up the power and the consumption
  4. Used the standard gauge card to display the template numbers
  5. Used auto-entities and the flex-table card for the long form display.
  6. Added a power clamp to get a grand total power number and created a template sensor which is the difference between the clamp value and the total of the switches and sockets. This represents the power which is not being consumed via a LightWave socket or switch.

My Lightwave kit consists of

  1. a LinkPlus hub
  2. some light switches
  3. some sockets
  4. an induction clamp power monitor

My group looks something like this

svalbardpower:
    name: Upper floors power
    entities:
      - light.bathroom_pendant
      - light.bathroom_shower
      - light.bathroom_uplights
      - light.bedroom_pendants
      - light.bedroom_spots
      - light.corner
      - light.dining_lights
      - light.kitchen_pendants
      - light.kitchen_sink
      - light.kitchen_spots
      - light.laundry_spots
      - light.living_spots
      - light.terrace
      - light.upper_stairs_downlights
      - light.upper_stairs_tube
      - light.terrace
      - switch.apple_tv
      - switch.cabinet_left_l
      - switch.cabinet_light
      - switch.cabinet_right_l
      - switch.cabinet_right_r
      - switch.coffee_grinder
      - switch.coffee_machine
      - switch.devialet_dialog
      - switch.dishwasher
      - switch.drinks_fridge
      - switch.first_floor_hubs
      - switch.first_floor_router
      - switch.home_cinema_amp
      - switch.homepod_bedroom_east
      - switch.homepod_bedroom_south
      - switch.homepod_kitchen_south
      - switch.homepod_living_left
      - switch.homepod_living_right
      - switch.imac
      - switch.kitchen_ne_r
      - switch.kitchenaid
      - switch.large_lava_lamp
      - switch.lava_lamp
      - switch.letter_a
      - switch.living_desk_r
      - switch.mi_vacuum
      - switch.nutri_ninja
      - switch.oven
      - switch.phantom_left
      - switch.phantom_right
      - switch.qooker
      - switch.raspberryscratch
      - switch.record_player
      - switch.second_floor_router
      - switch.sofa_floorbox_l
      - switch.sofa_floorbox_r
      - switch.sous_vide
      - switch.stick_blender
      - switch.sub_woofer
      - switch.thermomix
      - switch.toaster
      - switch.tv_left
      - switch.tv_right
      - switch.vacuum_sealer
      - switch.velux_active
      - switch.washing_machine
      - switch.xbox_left
      - switch.xbox_right

My templates look something like this

      cost_instant_svalbard:
        friendly_name: "Instantaneous Cost Upper - Monthly"
        value_template: >
          {{ (states('sensor.power_svalbard')| float * states('input_number.kwh_cost') | float /1000.0*24*30/100.0)|round(2)}}
      cost_svalbard:
        friendly_name: "Cost Upper"
        value_template: >
          {{ (states('sensor.consumption_svalbard')| float * states('input_number.kwh_cost') | float /100.0)| round(2)}}
      power_missing_svalbard:
        friendly_name: "Missing Power Upper w"
        value_template: >
          {{ states('sensor.power_meter')| int - states('sensor.power_svalbard')| int }}
      power_svalbard:
        friendly_name: "Power Upper w"
        value_template: >
          {{ expand('group.svalbardpower')|rejectattr('attributes.lwrf_switch','eq',0)|map(attribute='attributes.current_power_w')|map('int')|sum }}
      consumption_svalbard:
        friendly_name: "Consumption Upper kwh"
        value_template: >
          {{ (expand('group.svalbardpower')|map(attribute='attributes.lwrf_energy')|map('float')|sum/1000.0) | round(2)}}

The long form list of devices is done like this

type: 'custom:auto-entities'
filter:
  include:
    - group: group.svalbardpower
  exclude:
    - entity_id: switch.wall_panel
    - attributes:
        lwrf_power: <1
    - state: 'off'
card:
  type: 'custom:flex-table-card'
  title: Upper Power
  sort_by: lwrf_power-
  strict: true
  clickable: true
  columns:
    - name: Name
      data: name
    - name: Watts
      data: lwrf_power
      align: right
    - name: Consumed
      data: lwrf_energy
      align: right
      modify: parseFloat(x).toFixed(0)
    - name: State
      data: state
      align: right

I’m not sure if that helps anyone but happy to chat if anyone is looking to do the same.

Kind thoughts,

Andrew

HI Andrew,
I dont know if i will do exactly the same as you…
I have two different Gauge Outputs from my PV Systems. They are mesured by two different Smartmeters. Is it possible to collect it in ONE Gauge? Im not able to do it.

One shows energy 5 kWh the other shows 7kWh so I would like to see ONE with 12 kWh.
The Same with the actual power
I hope youre understanding me instead of my bad english :slight_smile:
Sascha