How to create a template that has the top n devices drawing power

Looking for some help from the community. I’m building a cost explorer dashboard for electricity consumption.

I currently use the Octopus Energy integration (UK energy provider) which can show my energy consumption (in kWh) per 30 mins and the corresponding cost. That works well and I can use ApexChart card to show a nice graph with a brush explorer which I can use to drill down into each 30 minute segment:

Screenshot 2023-10-02 at 16.53.04

Happy days!

Now, here’s where i’m trying to take it further. I’m looking to create a sensor (or perhaps use an integration that already exists) to capture the top n energy consuming entities per 30 mins that I can graph alongside this.

My current thinking is that I need a template sensor that is hooked up to the history stats integration that:

  1. Pulls in all the devices with 'device_class' = 'energy'. And,

  2. Orders them by the kWh they’re drawing and selects the top result.

I’ve managed to do the first part, as that’s the easy bit:

{{ states.sensor 
    | selectattr('attributes.device_class', 'eq', 'energy') 
    | list
}}

that gives me a list of entities like so:

<template TemplateState(<
  state sensor.server_energy_kwh=0.02; 
  unit_of_measurement=kWh, 
  device_class=energy, 
  friendly_name=Office: Energy [kWh] 
  @ 2023-10-02T12:43:58.560131+01:00
  >)>
etc etc

Now, what I’m looking for help to do is the second part, template something that orders that list by the state of each entity and selects the top result into a sensor. From there I can push that data into the history stats integration.

Any hints anybody may have, I’d be really thankful!

You could maybe use “Battery State Card”, with “sort_by” & “collapse | number”

oops, i missed the last part , of "push the data into history_stat :slight_smile:

Yep, the key bit for me is getting the data into a statistical sensor, or at least something that I can compare against. The idea is to build myself a dashboard or even just a simple graph using ApexCharts that has the kWH, the cost and then the most power hungry device, so I can quickly see where I can adjust the household’s habits.

Eventually I plan to feed all this data plus other environmental stats (weather, room/home occupancy, calendar entries, heating set points etc) to build up an AI model of energy habits on a day-to-day basis to predict when we’re likely to consume more energy and when we can cut down energy use. Baby steps at the moment though.

2 Likes

I think something like this should atleast do the “sort”

{%set powertop= expand(
"sensor.tp2_current_power",
"sensor.dplug_power",
"sensor.tp3_current_power",
"sensor.tp1_current_power",
"sensor.techwall_power")%}
{%set powertop=powertop|sort(attribute='state',reverse=false)|map(attribute='entity_id')|list%}

ohh well, your list is already “sorted” by

{{ states.sensor 
    | selectattr('attributes.device_class', 'eq', 'energy') 
    | list
}}

or ? … so left is “pick the top 10” :slight_smile: … or don’t you actually need some kind of “tressholds” on each “consumer” ? … i mean the i.e top-5 would most likely be , oven, diskwasher, laundrymashine/dryer and other similar “hardware” i.e used regularly, when “needed”

i.e you( AI) can’t “compare” individual devices based upon Kvh( or W), and “time-on”, true one can, but obviously a power-outlet consumes whats plugged into it, so power-out-1 with a led-strip" on most of the day, would never show up in your top consumer.
I’m just thinking you should/could find another approach, as your “energy-devices” consumption/datas is already stored in long-term-db, so i would most likely use i.e groups , and not a template like all_energy devices.
And for the “view/ApexChart” the same, no point in showing Fridge/Freezer along with out-let for car-charging
And for your TV, you definitely need “tressholds” ( you should easy be able to know how much it use in st-by VS “on” ( so here it’s amount(of total time) and time of day) which are relevant (you can’t make your TV consume less, while on)
Washer etc(devices). same here, tressholds … time of day/electricity price., and your electrical heaters uses more KwH in the winter-time-to reach same indoor temperature as during summer

By i can agree upon, the current Energy-Dashboard, leaves much on the “wish list” :), for both daily overview, and for “analyzes”

So your “energy” Datas, are already in your longterm-statistics, your “template” above shows all it’s details ( which all details you don’t need for compare, or seeing in i.e graphs )

Sorry i can’t give you relevant examples, as i don’t store i.e when lights/switches are “on” and i only have a few “energy” devices, and as i have been around “the House” with power/energy monitoring devices in , i have a fairly good overview of what each Utility/Hardware uses when “on” ( as mentioned in my “examples” , i can’t do much about what my washer draws(beside washing on 50 degree, instead of 60, and do it when the electricity price is low)
AI would probably not figure out, whether i washed clothes in 40 vs 90, so it would “Alarm” me that my Washing-Mashine is about to burn ! :grin:

per 30min, you will eventually find this “trivial” And AI will drawn you in contradictory suggestions/advices :slight_smile:
IF you use groups, and compare/view various/selected energy-devices in groups, you’ll find something to compare

Taking this request literally, if you just want the largest-magnitude energy number at any time, this will do it:

{{ states.sensor 
    | selectattr('attributes.device_class', 'eq', 'energy') 
    | map(attribute='state')
    | map('float')
    | sort(true)
    | first
}}

Thank you @Troon I’ve marked your reply as the solution as it answer the question as posed.

@boheme61 thank you for taking the time to write such a detailed reply.
There’s a lot to unpack there, and yes I agree with your thoughts around appliances. I’m able to filter these out as my appliances come with built in energy monitoring (Fridge, Dishwasher, Washing Machine) and with the template above I can look to extend it to filter them out.
Next on my list I will be looking at thresholds as you say - TV etc. Fortunately (given the price per unit of electricity in the UK) I don’t have electrical heaters.

I’ve been thinking of raising a WTH to add more detailed comparison controls to the default energy dashboard. That’s one for the future perhaps.

Thank you both!

Well i wasn’t aware you just looking for the Top 1, KwH(amount), so i was thinking more like your initial template, and build upon that(at first) but as i have “energy-devices” showing usage per day/month/year, you can imagine which was in “Top” … and i like it simple, so i went for the entity-id

{{ states.sensor 
    | selectattr('attributes.device_class', 'eq', 'energy') 
    | sort(attribute='state',reverse=true)|map(attribute='entity_id')|list
}}

I guess it’s therefore my answer became a “puzzle” :slight_smile:

EDIT: another “worthy” thought … states.sensor is going through ALL your sensors, looking for (‘attributes.device_class’, ‘eq’, ‘energy’) every 30min if that’s what you want , another good reason of “manually” lay up a “strategy” i.e using groups, after all it might boils down to 5 devices/rooms/areas, worth monitoring, the rest is just a waste of '“energy” :slight_smile: