#Assistance with variable cost model

1 messages · Page 1 of 1 (latest)

tropic urchin
#

Heyo! I'm looking for some assistance in creating what I assume will have to be an automation. My power cost model is as follows:

Monthly usage:

0 - 100kWh = 2.7033
101 - 400kWh = 3.1637
401 - 650kWh = 3.4468
650+ = 3.7158```

I'm using a Sonoff DB board meter to measure all power coming into the house. I need a counter that resets every month, which then takes the cost depending on how much power I've used and chooses the correct cost. Any help would be appreciated!
rocky summit
#

A couple of ways to do this but the easiest is probably just a utility meter and a template sensor

#

Utility meter pointed at your energy meter set to monthly reset will let you chop up your power useage (you can even change the reset day of month if you get billed 10th to the 10th or something)

#

Then a template that looks something like this:

{% set monthly = states('sensor.monthly_energy_utility_meter') | float (0) %}
{% if monthly <= 100 %}
  2.7033
{% elif monthly <= 400 %}
  3.1637
{% elif monthly <= 650 %}
  3.4468
{% else %}
  3.7158
{% endif %}
tropic urchin
tropic urchin
rocky summit
#

that helper page is also where you can make your utility meter (do that first, so you know what name to change sensor.monthly_energy_utility_meter to)

tropic urchin
#

Yea I got that boi already.

And then this will give me my cost per kwh? Do I then use the energy config screen and point it at the template sensor?

#

The issue I've been having is that the energy page seems to retroactively change the costs from what I can see? So when this updates it'll update all the data and costs won't be accurate

rocky summit
#

How have you set it up?
I use "entity with current price" and it works fine

tropic urchin
#

Unless I'm just wrong. I had it set up with automations and stuff previously. So will have to wait and see over the course of the month I guess

#

Thanks!

tropic urchin
#

Heyo. Another thing. According to my ewelink app I've pulled 5.88 today (It's the 1st so that's the full month's cost so far)

Home assistant is showing 39kwh

#

I set the utility helper to reset monthly from today

tropic urchin
#

Lol I broke it. Starting again lol

wild zenith
#

Hello! My apologies, I do not mean to hijack your post, but I am trying to do something extremely similar, and the "start here" seems to imply trying to avoid creating duplicates.

I'm very new to HA and sometimes the basics still escape me.

My utility rates are defined as follows:

Demand Charge: $3.15/kW during peak hours
Peak hours occur Monday through Friday from 7 to 10 a.m. and 5 to 8 p.m. There are no demand charges on weekends or major federal holidays, including New Year’s Day, Memorial Day, Independence Day, Labor Day, Thanksgiving, and Christmas. In the event that the holiday occurs on Sunday, there are no demand charges on the following Monday.
Energy Charge:
$0.0552 per kWh (for the first 600 kWh per month)
$0.0660 per kWh (for 601-3,500 kWh per month)
$0.1002 per kWh (for 3,501 and over kWh per month)
Basic Charge (single-phase service): $24.31 per month (not sure how to, or if it's even possible to include this)

This is where I am so far, but I don’t know enough about this and I think the formatting, and probably variables are off. I'm also interested in finding a way to define holidays without having to manually update static dates in the template.... would it be possible to reference some sort of calendar object?

if date =['2024-11-28', '2024-12-25', '2025-01-01', '2025-05-26', '2025-07-04', '2025-09-01', '2025-11-27', '2025-12-25']
total <601
price =0.0552
or total >=601
and total <=3500
price =0.0660
or total <3501
price 0.1002
elseif now().weekday >=0
and now().weekday <5
and now().hour >=7
and now().hour <10
price=3.15
elseif now().weekday >=0
and now().weekday <5
and now().hour >=17
and now().hour <20
price=3.15
else total <601
price =0.0552
or total >=601
and total <=3500
price =0.0660
or total <3501
price 0.1002

rocky summit
#

I think this is different enough you could justify a new thread but it's fine 🙂

#

You definitely can reference a calendar object, it returns a state of "on" during an event, so if you set up a calendar with national holidays booked as all day events you can do states('calendar.national_holidays') == "on" to check if it's a holiday

#

However, even simpler there's the holiday integration

sick narwhalBOT
rocky summit
#

behaves the same, but you don't even need to set up the calendar beyond telling it where you live

wild zenith
#

Oh nice!

#

Honestly, i really don't know YAML, and to make the above I've been at it for days... any chance you might be able to help me update it?

rocky summit
#

as for the peak hours, i'd consider condensing all of that down into a scheduler helper

wild zenith
#

So, two helpers?

rocky summit
#

just one

#

you can define multiple times of day each day

wild zenith
#

The template is also a helper though? Or would the rates and whatnot also go into the scheduler?

rocky summit
#

oh, sorry yes - 2 including the template 🙂

wild zenith
#

I apologize, i feel like I'm maybe asking for more than I should here, there's just so much I don't know and have never seen. 😬

rocky summit
#

no worries

#

I would condense your logic down to something like:

{% if states('calendar.national_holidays') == 'on' or states('sensor.peak_time_scheduler') == 'off' %}
  {% set monthly = states('sensor.monthly_energy_utility_meter') | float (0) %}
  {% if monthly < 601 %}
    0.0552
  {% elif monthly < 3501 %}
    0.0660
  {% else %}
    0.1002
  {% endif %}
{% else %}
  3.15
{% endif %}
#

3 helpers including the utility meter i guess as well, unless you already have the monthly value from something else?

wild zenith
#

Yah i think I've got that piece setup correctly. It's being supplied by a SolarEdge integration.

rocky summit
#

cool, so you'd just need to point that states in the 2nd line at that sensor correctly

wild zenith
#

'sensor.peak_time_scheduler' should be replaced by the the SE object name?

rocky summit
#

no, the 'sensor.monthly_energy_utility_meter'

#

the 'sensor.peak_time_scheduler' is whatever you call your scheduler helper

wild zenith
#

Oh gotcha... on mobile, think the word wrap got me.

rocky summit
#

haha gotcha

wild zenith
rocky summit
#

ye

wild zenith
#

Thanks so much! I'll go see how far I can get!

rocky summit
#

should end up looking something like:

wild zenith
#

Ok so I have the calendar, the schedule helper, and the utility sorted out.

The rates block you supplied goes into a template then? Or is it a number helper or something?

rocky summit
#

yeah a template

#

specifically a template sensor

#

not the confusingly named "template number"

wild zenith
#

Do I leave the dropdowns blank?

Unit of measurement I typed in USD/kWh

No idea what to enter for Device Class, State Class or Device.

#

I need to find a HA boot camp or something. 🤣

rocky summit
#

depends what you want, but for something like this I'd make state class "measurement", leave device class empty (it basically only exists to set default icons and stuff) and device is optional but you can point it at your SolarEdge inverter or something and it will show up when you are looking at the device page for it

#

nice to have if you need to keep track of what is doing what 🙂

wild zenith
#

Ok! The icon next to the object is a red exclamation mark... not actually anything wrong, just no icon because we didn't set device class i assume?

rocky summit
#

just refresh the page and it goes away

#

it's a weird bug in the frontend at the moment

wild zenith
#

Ok, I added it here. I'm guessing it will either take time to update, or apply going forward and not retroactively?

rocky summit
#

yeah it won't apply retroactively

#

but all looks good otherwise 🙂

wild zenith
#

Ik ill be patient. 😁

#

One final question, on the holiday calendar, is there a way to edit/delete the holidays? Not all on the calendar apply.

rocky summit
#

good question, i don't know!

#

if that's a major issue, maybe just make an actual local calendar and point it at that instead of the one the holiday integration generates

wild zenith
#

Sounds good. I'll poke around and see what I can break 🤣

Thank you so much, this was so helpful and I learned quite a bit!

rocky summit
#

no worries 🙂

wild zenith
rocky summit
wild zenith
#

Gotcha. Had a feeling that might be the case. If that's the worst thing I have to do, it still sounds pretty awesome.

rocky summit
#

what are the holidays you are trying to ignore btw? if those are fixed date ones it might be easier to just exclude them manually

wild zenith
#

I haven't looked through the whole calendar yet, but the one that caught my eye right off the bat was Election Day.

#

Basically everything except New Year’s Day, Memorial Day, Independence Day, Labor Day, Thanksgiving, and Christmas

rocky summit
#

ah yeah, that'd be tricky

#

not sure how to get around that other than doing it manually

#

unless you want to set up an automation to work out when the weird ones are and add them to the calendar and run it on Jan 1st every year 😛

wild zenith
#

Wonder if being able to specify holidays might end up being a ln added feature in the future...

wild zenith
rocky summit
#

tbh i think the holidays integration is just a wrapper around a standard python liubrary so unlikely to get updated

wild zenith
#

I'll check it out!

#

I noticed something... hoping it won't cause issues. The rates we created is in USD/kWh, but I see in the configure grid consumption where you set the entity it says "Entity with the current price (USD/Wh)". Do you think it'll calculate wrong?

rocky summit
#

Possibly... I think it tries to match the units of your imported energy sensor

#

So you might need to add 3 extra 0s to turn it into a cost/Wh

#

If your main sensor is in Wh

wild zenith
#

I'll have to dig around to see if i can figure out what it is reporting. Thanks for the insight. 😁

#

Yah it's reporting in Wh. So 3 zeros to each rate, like this?

0.0000552
0.0000660
0.0001002
0.0031500

rocky summit
#

Yeah

#

Otherwise you're gonna have really big costs :p

wild zenith
#

Do i need to adjust the other values?
601
3501

rocky summit
#

Was just about to say that, yes

wild zenith
#

Those should then be
601000
3501000

#

Ah! I thought i was going to have to check back in a day or so, but after converting there is now a cost showing. Thanks again!

wild zenith
#

@rocky summit I'm trying out that github holiday calendar you found. Looks like it's going to work. I had to change the sensor though. This calendars state is a countdown to the next holiday, with the holiday being 0, so I changed it from 'on' in the code you provided to '0'.

The only thing I'm not clear on is how/when it retrieves holiday date updates. It's populated up through Christmas of 2025 and is blank after that. I'm assuming at some point it will retrieve more dates... but it's just a guess.

rocky summit
#

I think it should run through until end of the next year, so at the start of 2025 it will update to the end of 2026

#

The one thing to note with a counter like that is that all states are strings - i.e. Text, so if you want to check the value of it is zero you need to do states(blah) == "0", or you need to turn it into a number with states(blah) | int == 0 either will work for equal but if you were trying to do less than or something you'd want to turn it into a number for the comparison

wild zenith
#

It's currently

{% if states('calendar.flathead_electric_observed_holidays') == '0' or states('schedule.flathead_electric_peak') == 'off' %}

So it sounds like that would work, but for better compatability/future modifications the below would be better, correct?

{% if states('calendar.flathead_electric_observed_holidays') | int == '0' or states('schedule.flathead_electric_peak') == 'off' %}

#

Sorry, looked for a way to highlight the code section as you did, but I'm not finding it. 🤔

#

@rocky summit on the below block

{% set monthly = states('sensor.solaredge_imported_energy') | float (0) %}
{% if monthly < 601000 %}
0.0000552
{% elif monthly < 3501000 %}
0.0000660
{% else %}
0.0001002
{% endif %}
{% else %}
0.0031500
{% endif %}

The if monthly is telling it to sum the imported energy over the course of the month and change the rate based on the entire month the consumption, right? I get why the

{% else %}
0.0031500

Doesn't need elif monthly, but

{% else %}
0.0001002

Doesn't need to specify monthly?

Lastly, I'm assuming this is based off of a calendar month, any thoughts on how I could base it off of a billing cycle?

rocky summit
#

the reason why the first "else" doesn't need monthly is because the only reason it would have got there is if monthly is not < 3501000 - i.e. in your 3rd cost bracket

#

and yeah it's likely the solaredge will chop it based on calendar month. if you want to change it round I'd suggest using a utility meter set to monthly looking at a "total imported energy" sensor if SE give that to you, as you can specify day of month to reset it on

wild zenith
rocky summit
#

that looks quite likely

wild zenith
#

This is the only important related sensor. Looking at the available sensors, I'd guess the others account for all energy or specific sources, so they probably wouldn't be appropriate.

rocky summit
#

well the utility meter should still work

#

just point it at that set to monthly and it will count up over the month between the days you set

wild zenith
#

Sorry, I'm not sure what you mean by utility meter, or how to point it to it, or how to set the days. 😬

rocky summit
#

it's in the helpers

wild zenith
#

I leave imported energy the object grid consumption in the energy dashboard is looking at, or do I point it to this new object?

#

And how do I point the rate code at this?

rocky summit
#

leave the energy dashboard alone

rocky summit
#

you should be able to find it in the settings

wild zenith
#

Ok, I now have this:

#

Thanks so much for teaching me about all of these options!

wild zenith
#

@rocky summit Please check your private messages when you get a moment. 😁

tropic urchin
wild zenith
tropic urchin
#

Did you get everything working?

sick narwhalBOT
#

ChatGPT and other "AI" systems do a fantastic job of generating well structured, convincing looking answers... that are either totally garbage or, if you're lucky, have subtle flaws. Don't use them to "help" others, and don't use them yourself.

tropic urchin
#

Gets the basics, syntax, and layout down enough for plebs like me to troubleshoot