EMHASS: An Energy Management for Home Assistant

Otherwise I’ve just created some dockerhub images for that sandalone version of emhass.
You can install them and try that with:

docker pull davidusb/emhass-docker-standalone:latest
1 Like

This is awesome, thanks!

I just installed it remotely and it showed no errors.
When I’m home I’ll test it (can not change the firewall from remote and therefore can’t access port 5000 at the moment)

Thanks!

So I just tested it. EMHASS is up and running.

When triggering an optimization
curl -i -H "Content-Type: application/json" -X POST -d '{"prod_price_forecast":[0.194796,0.194796,0.18202800000000002,0.18202800000000002,0.1647,0.1647,0.15594,0.15594,0.15291599999999997,0.15291599999999997,0.135456,0.135456,0.134412,0.134412,0.138288,0.138288,0.13824,0.13824,0.131124,0.131124,0.13827599999999998,0.13827599999999998,0.134856,0.134856,0.158556,0.158556,0.192192,0.192192,0.20418,0.20418,0.201132,0.201132,0.196824,0.196824,0.19288799999999998,0.19288799999999998,0.189432,0.189432,0.19218,0.19218,0.19836,0.19836,0.210444,0.210444,0.20986799999999997,0.20986799999999997,0.222132,0.222132],"solcast_rooftop_id":"MYID","solcast_api_key":"MYKEY"}' http://192.168.0.XX:5000/action/dayahead-optim

I get

|2023-01-18T17:26:24.265049684Z|stdout|KeyError: 'SOCtarget'|
|---|---|---|
|2023-01-18T17:26:24.264994871Z|stdout|    soc_init = self.plant_conf['SOCtarget']|
|2023-01-18T17:26:24.264931720Z|stdout|  File /usr/local/lib/python3.8/site-packages/emhass-0.3.21-py3.8.egg/emhass/optimization.py, line 127, in perform_optimization|
|2023-01-18T17:26:24.264873182Z|stdout|    self.opt_res = self.perform_optimization(df_input_data, P_PV.values.ravel(),|
|2023-01-18T17:26:24.264806718Z|stdout|  File /usr/local/lib/python3.8/site-packages/emhass-0.3.21-py3.8.egg/emhass/optimization.py, line 487, in perform_dayahead_forecast_optim|
|2023-01-18T17:26:24.264752130Z|stdout|    opt_res_dayahead = input_data_dict['opt'].perform_dayahead_forecast_optim(|
|2023-01-18T17:26:24.264664541Z|stdout|  File /usr/local/lib/python3.8/site-packages/emhass-0.3.21-py3.8.egg/emhass/command_line.py, line 203, in dayahead_forecast_optim|
|2023-01-18T17:26:24.264609652Z|stdout|    opt_res = dayahead_forecast_optim(input_data_dict, app.logger)|
|2023-01-18T17:26:24.264536476Z|stdout|  File src/emhass/web_server.py, line 151, in action_call|
|2023-01-18T17:26:24.264477450Z|stdout|    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)|
|2023-01-18T17:26:24.264413837Z|stdout|  File /usr/local/lib/python3.8/site-packages/flask/app.py, line 1796, in dispatch_request|
|2023-01-18T17:26:24.264361898Z|stdout|    rv = self.dispatch_request()|
|2023-01-18T17:26:24.264301473Z|stdout|  File /usr/local/lib/python3.8/site-packages/flask/app.py, line 1820, in full_dispatch_request|
|2023-01-18T17:26:24.264247372Z|stdout|    rv = self.handle_user_exception(e)|
|2023-01-18T17:26:24.264187746Z|stdout|  File /usr/local/lib/python3.8/site-packages/flask/app.py, line 1822, in full_dispatch_request|
|2023-01-18T17:26:24.264134557Z|stdout|    response = self.full_dispatch_request()|
|2023-01-18T17:26:24.264066356Z|stdout|  File /usr/local/lib/python3.8/site-packages/flask/app.py, line 2525, in wsgi_app|
|2023-01-18T17:26:24.264005518Z|stdout|Traceback (most recent call last):|
|2023-01-18T17:26:24.263914754Z|stdout|[2023-01-18 17:26:24,263] ERROR in app: Exception on /action/dayahead-optim [POST]|
|2023-01-18T17:26:24.262605983Z|stdout|[2023-01-18 17:26:24,262] INFO in optimization: Perform optimization for the day-ahead|
|2023-01-18T17:26:24.241398772Z|stdout|[2023-01-18 17:26:24,240] INFO in command_line: Performing day-ahead forecast optimization|
|2023-01-18T17:26:24.240422969Z|stdout|[2023-01-18 17:26:24,239] INFO in web_server:  >> Performing dayahead optimization...|
|2023-01-18T17:26:16.906745391Z|stdout|[2023-01-18 17:26:16,906] INFO in retrieve_hass: Retrieve hass get data method initiated...|
|2023-01-18T17:26:16.905492021Z|stdout|[2023-01-18 17:26:16,904] INFO in forecast: Retrieving data from hass for load forecast using method = naive|
|2023-01-18T17:26:16.221098640Z|stdout|[2023-01-18 17:26:16,220] INFO in forecast: Retrieving weather forecast data using method = solcast|
|2023-01-18T17:26:16.218053466Z|stdout|[2023-01-18 17:26:16,217] WARNING in utils: There are non numeric values on the passed data, check for missing values (nans, null, etc)|
|2023-01-18T17:26:16.215547314Z|stdout|[2023-01-18 17:26:16,215] INFO in command_line: Setting up needed data|

Is this a problem with my optimization call or anything else?

Thanks!

Yes this is a silly bug that should be fixed within the code. For now you can make it work by putting the default configuration parameters for a battery even if you don’t have one. Put the default values inside config_emhass.yaml.
You can find the default values here: emhass/config_emhass.yaml at 4b75afde01b48b335d0770c35de020393822c090 · davidusb-geek/emhass · GitHub

Perfect, it’s working now!

Thanks again!

1 Like

@davidusb Sure, you mean when viewing the history in HA?

The error is not explicit at all but I suspect that it is your url and token configuration. If using the supervisor then just leave the default values like this:

hass_url: empty
long_lived_token: empty
1 Like

Oh wow, that was it!! Set hass_url and long_lived_token to empty and the data is now coming in!

Thanks so much :smile:

1 Like

I’m having the following problem:

I can NOT run the next-day optimization between 00:00 and 14:00 as I would not have price data for all the next 24 hours but only till midnight. The reason for that is that I only receive the prices for the upcoming day at 14:00.
This results in the heatpump used for water heating frequently running to short as it is for example planned to run from 12:00 till 16:00 but when I call the dayahead-optimization on 14:00 this decides to run it again the following day and stops it right now.

IWhat’d be the best way to run the optimization every hour (also to have the newest solar forecast) and not have it stop the heatpump as it decides at 14:00 that it’d be better to run tomorrow?
Is it necessary to import the the data for all upcoming 24 hours or could I for example only import 18 hours when it’s 06:00 in the morning?

Thanks

This kind of use case was the reason to code a pseudo model predictive controller within EMHASS. So you can use that as a solution. See the docs for info on how to setup that.

Otherwise if you’re using Nordpool (?), then you need to concatenate the prices forecasts for “today” and “tomorrow” values. I’ve just updated the docs with an example to do this: The forecast module — emhass 0.3.21 documentation

1 Like

Thanks for you reply.

I looked through the MPC controller before and especially had a look on the published scripts from @markpurcell as in EMHASS: An Energy Management for Home Assistant - #56 by markpurcell
In those scripts he has a “standard” day-ahead curl and one for mpc. Is both necessary or is it just to switch between them?

About the prediction horizon: Mine is currently 48 as I have 30 minutes intervals. As I don’t have prices for the upcoming 24 hours from 01:00 till 13:59, can I decrease the number of imported hours step by step between those times?

I’m using a similar provider as Nordpool (Awattar) but they only publish the prices for the upcoming day at 14:00 the present day.

What I don’t understand yet is the prediction_horizon. The documentation states “Set the prediction horizon and receding horizon parameters.”. Do I therefore have to pass in any historical data?
There seems to be a possibility to import live vs forecasted values and therefore optimize it. How would this be done?

Sorry for asking so many question here and thanks for your support.

You only need to run one.
I setup the “standard” day ahead as my first instance of EMHASS to understand the system and would recommend implementing this simpler case first to get things working.

I now exclusively run the mpc optimisation because my forecast prices change every 5 minutes. If my prices were only set once a day then the day-ahead optimisation would be sufficient, provided your other variables are not changing.

I have a similar issue here, sometimes I have a full 24 hours of forecasts, sometimes the forecasts finish at 0300, until 1230 when the next rolling window is published.

I use a script to count the number of forecasts I have access to and use that to set prediction_horizon, and include this with each call to the mpc_optimisation:

prediction_horizon\":{{min(48,
          (state_attr('sensor.amber_feed_in_forecast', 'forecasts')|map(attribute='per_kwh')|list|length)+1)
          }},
1 Like

This graph explains the notion of prediction horizon

2 Likes

Hi!

Great add-on!
I can do day-ahead optimization with real forcast price data and after that reload the webpage and everything looks great and works great until I reload HA or the add-on, then the default CSV is loaded. Is this by design?

Best regards, L

Hi. Yes that is the current normal behavior. It’s not optimal I know but I have not found a proper way to retain the current optimization results and reload them after a restart. If anyone have a solution to do this please share, help is wanted.
What I do to try to overcome to that situation is an automation that detects a restart and then relaunch the optimization task. I’ve put the automation that I’m using for this in the documentation: A real study case — emhass 0.3.21 documentation
This solution is perfect if using MPC

Thanks!
I’ll look in to it. You can ignore my other post :slight_smile:

By the way, can I use 0 loads? I would like to use EMHASS only for battery management.

Thanks for your feedback.

Well what is changing is the pv forecast as it is getting more precise when it gets closer (which is during the time where I don’t have 24 hours of upcoming prices available).

So it’d be nice to call only an mpc optimization for the upcoming x hours where I have prices available while pv forecasts are the newest ones.

Does using mpc automatically get the newest “real” or “live” pv and power consumption data and is it used for optimization?

One of the beautiful adjectives of EMHASS is you can run different optimisation depending on your needs.

Day-ahead provides a good optimisation for the next 24 hrs.

I wanted almost real time optimisation so I run MPC every minute, with updated real time values for buy/sell costs, solar production and household load.

To achieve this I needed to format each of my forecast arrays with the live value as the first entry and then 47 forecast entry’s.

With mpc, you still need to provide all forecasts for the full 24 hrs (or prediction_horizon), it doesn’t provide a more accurate forecast for a shorter time frame.

PS regarding the lost optimization results. Just so we are talking about the same thing, the manual states that optimization results are saved in the CSV and that you left a sample CSV in place for first run. Isn’t the last results saved in the CSV? If it was it could be loaded like the defaults (sample CSV) are after reload/reboot(?)
DS.