Danielhiversen / pyTibber

Async Python 3 library for Tibber
GNU General Public License v3.0
95 stars 33 forks source link

Add intraday price rank #288

Closed haugene closed 7 months ago

haugene commented 8 months ago

TL;DR: The goal of this PR is to add a new attribute to the TibberSensorElPrice in the HomeAssistant integration that indicates how the electricity price for the current hour compares to other hours of the current day.

I was thinking it could be named intraday_price_ranking, or something similar. Naming things is hard... The implementation is a bit rough but I thought I'd ship a PR instead of opening an issue, to have something concrete to discuss.

The motivation for having an attribute like this is to be able to better plan electric use that can be postponed. For example a water heater. I want it to run 4, 6 or maybe 8 hours a day - preferably in the cheapest hours. And that't not possible with the integration today. Looking at average prices can get you far, but not with the same level of control.

When I started writing my automation I first googled the problem of course, and I ended up in threads like this and this. HomeAssistant isn't great at future values and people have a lot of suggestions for template sensors, but this would be a precise enough workaround for me at least. I also think it will be a lot simpler for the average user, even as a developer I find the copy-pasting of large template sensors a bit heavy at times :sweat_smile:

I have forked HomeAssistant and tested this implementation there, it looks like it's working fine. If this should be introduced as a fourth return value in a tuple from current_price_data like I'm proposing here or if it should be included in the current_attributes dict, or something else, is up to you of course.

Curious to hear your thoughts on this. And I'll set the dev environment up a bit more properly and run the linters and stuff if you're positive and want to merge it after some adjustments.

ismarslomic commented 8 months ago

There is already an attribute price_level on Electricity Price sensor, with values:

I have created an template sensor for this attribute and used it in HA Automation to adjust heating based on price level for current hour. Not completely sure what the differences would be between this existing attribute and the one you are proposing @haugene (lenge siden sist, bro!).

haugene commented 8 months ago

Hey @ismarslomic! (alt for lenge siden!).

That's a good question, and I think a lot of people use the price_level for these kinds of use cases. But... in Tibber the PriceLevel is defined as:

Price level based on trailing price average (3 days for hourly values and 30 days for daily values)

Since this is hourly prices that means compared to the last 3 days. So one example of where price_level comes in short is if the price is trending upwards as temperatures are falling, then my water heater might be off for days. And that's too long I think. There's high WAF to be had by always having hot water :wink: As long as prices are increasing the 3-day average might never catch up and theoretically this could go on for weeks (although realistically it won't)

In your case of adjusting heating based on price level you could for example use price_level to set which temperature to heat your living room to. But maybe also sprinkle some intraday_price_ranking on top to know which hours to try to achieve it. For example. There might be some cases where these can interact nicely.

Danielhiversen commented 8 months ago

Thanks for your contribution.

I do not want to add all types of price mangling, because everyone has their way to do it and include it in some automation. But I think this is generic enough to be included.

Four return values is not great, but it is ok for now. I plan to refactor this code later.

haugene commented 8 months ago

Great! I'm taking this out of "draft" state then. I see that I've upset some linters. Let me know if you want me to follow up on anything here. And thanks for your great work on this integration :raised_hands:

Danielhiversen commented 7 months ago

If you fix the linters, I can merge this

haugene commented 7 months ago

If you fix the linters, I can merge this

I don't have python 3.12 locally, but fixed the linting errors on 3.11 now and hopefully that follows through to 3.12. Can get 3.12 up and running if it fails for some reason. I also added a simple test that the current price rank should exist and be within the interval 1-24 (I didn't zero-index it as I think 1-24 is more human friendly - but shout out if you think it should be).

haugene commented 7 months ago

FYI: Once you've merged this and pushed a new versioned release I can make a PR to include it in the HASS component if you want. Let me know.