EMHASS: An Energy Management for Home Assistant

OK, understood. Thank you!

A new patched release on the way.
Hopefully everything is back to normal.

3 Likes

I have a new error since the update

I use MLForecaster and every 5 min. Naive MPC Optim.
Worked fine before.

2024-01-30 16:34:58,649 - web_server - ERROR - Exception on /action/naive-mpc-optim [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 1463, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 872, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 870, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 855, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/emhass/web_server.py", line 50, in action_call
    input_data_dict = set_input_data_dict(config_path, str(data_path), costfun,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/emhass/command_line.py", line 120, in set_input_data_dict
    P_load_forecast = fcst.get_load_forecast(method=optim_conf['load_forecast_method'], set_mix_forecast=True, df_now=df_input_data)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/emhass/forecast.py", line 607, in get_load_forecast
    mlf = pickle.load(inp)
          ^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'mlforecaster' on <module 'emhass.machine_learning_forecaster' from '/usr/local/lib/python3.11/dist-packages/emhass/machine_learning_forecaster.py'>

When I put the model on naive, it works

Follow these steps:

  1. Go to your share folder
  2. Erase the file load_forecast_mlf.pkl
  3. Run the ML fit and tune routines again
  4. Retry your optimization using the MLForecaster model, should work
1 Like

I can confirm that the add-on update to v.0.6.1 was successful and it is working correctly.

1 Like

Hmm, thought I understood, but then again, perhaps not…

Considering the following:

  • “power hem” >=0, power imported from the grid
  • “Heat Pump Power Consumption Specification” >=0, deferrable load
  • “Solis Växelriktare AC Output Total Power” >=0, power supplied by the PV plant, if possible first consumed by the house, excess is exported to grid
  • “power production hem” >=0, power exported to the grid

So the following definition of the
sensor_power_load_no_var_loads: “power hem” + “Solis Växelriktare AC Output Total Power” - (“Heat Pump Power Consumption Specification” + “power production hem”), correct?

Thank you, I can confirm I have now successfully upgraded via the add on from 0.5.4 to 0.6.1.

However, I did have to perform one manual step for the upgrade which was to update the start and end timesteps to the correct number of deferrable loads (in my case 6).

It would be good to at least put a warning in the release notes that manual step maybe required, or better if the default values for start and end timesteps didn’t cause this failure on upgrade. Maybe either automatically populate the correct number or have this feature initially disabled until it is explicitly enabled after configuration. Some runtime error checking of this condition and presenting an informative error message would also be useful.

Looking forward to using this feature today as I need my EV charged by 10:00 for a trip.

- start_timesteps_of_each_deferrable_load: 0
- start_timesteps_of_each_deferrable_load: 0
- start_timesteps_of_each_deferrable_load: 0
- start_timesteps_of_each_deferrable_load: 0
- start_timesteps_of_each_deferrable_load: 0
- start_timesteps_of_each_deferrable_load: 0

Here is my optimisation with EV charging scheduled before 10am, nice work.

Hi Mark, glad to read that my first contribution has an added value!

Thanks for your feedbacks, fair points!

I’d be happy to give it some further thought, on how to make this new feature as least intrusive as possible.
As the number of features and hence the number of parameters in EMHASS grows, this will become more an important point.

1 Like

Hi David,

I did the update. Looks good but it’s still splitting loads. At the moment I only have two loads that require only one startup per day and then run continuous until done.

I tested this functionality recently. Its working fine for me. Would you mind to share your configuration?

costfun: profit
logging_level: INFO
set_total_pv_sell: false
set_nocharge_from_grid: false
set_nodischarge_to_grid: false
sensor_power_photovoltaics: sensor.pv_power
sensor_power_load_no_var_loads: sensor.power_load_no_var_loads
number_of_deferrable_loads: 2
list_nominal_power_of_deferrable_loads:
  - nominal_power_of_deferrable_loads: 1800
  - nominal_power_of_deferrable_loads: 1920
list_operating_hours_of_each_deferrable_load:
  - operating_hours_of_each_deferrable_load: 4
  - operating_hours_of_each_deferrable_load: 4
list_start_timesteps_of_each_deferrable_load:
  - start_timesteps_of_each_deferrable_load: 0
  - start_timesteps_of_each_deferrable_load: 0
list_end_timesteps_of_each_deferrable_load:
  - end_timesteps_of_each_deferrable_load: 0
  - end_timesteps_of_each_deferrable_load: 0
list_peak_hours_periods_start_hours:
  - peak_hours_periods_start_hours: "07:00"
  - peak_hours_periods_start_hours: "07:00"
list_peak_hours_periods_end_hours:
  - peak_hours_periods_end_hours: "22:00"
  - peak_hours_periods_end_hours: "22:00"
list_treat_deferrable_load_as_semi_cont:
  - treat_deferrable_load_as_semi_cont: true
  - treat_deferrable_load_as_semi_cont: true
list_set_deferrable_load_single_constant:
  - set_deferrable_load_single_constant: true
  - set_deferrable_load_single_constant: true
load_peak_hours_cost: 0.1119
load_offpeak_hours_cost: 0.1119
photovoltaic_production_sell_price: 0.0703
maximum_power_from_grid: 3500
list_pv_module_model:
  - pv_module_model: LONGi_Green_Energy_Technology_Co___Ltd__LR6_72HBD_380M
  - pv_module_model: LONGi_Green_Energy_Technology_Co___Ltd__LR6_72HBD_380M
list_pv_inverter_model:
  - pv_inverter_model: OutBack_Power_Technologies___Inc___SBX5048_120_240__240V_
  - pv_inverter_model: OutBack_Power_Technologies___Inc___SBX5048_120_240__240V_
list_surface_tilt:
  - surface_tilt: 30
  - surface_tilt: 30
list_surface_azimuth:
  - surface_azimuth: 90
  - surface_azimuth: 270
list_modules_per_string:
  - modules_per_string: 9
  - modules_per_string: 9
list_strings_per_inverter:
  - strings_per_inverter: 1
  - strings_per_inverter: 1
set_use_battery: false
battery_nominal_energy_capacity: 5000
hass_url: empty
long_lived_token: empty
optimization_time_step: 30
historic_days_to_retrieve: 9
method_ts_round: nearest
lp_solver: COIN_CMD
lp_solver_path: /usr/bin/cbc
set_battery_dynamic: false
battery_dynamic_max: 0.9
battery_dynamic_min: -0.9
weight_battery_discharge: 1
weight_battery_charge: 1
load_forecast_method: mlforecaster
battery_discharge_power_max: 1000
battery_charge_power_max: 1000
battery_discharge_efficiency: 0.95
battery_charge_efficiency: 0.95
battery_minimum_state_of_charge: 0.3
battery_maximum_state_of_charge: 0.9
battery_target_state_of_charge: 0.6
delta_forecast_daily: 1
weather_forecast_method: scrapper
load_cost_forecast_method: hp_hc_periods
set_zero_min: false
production_price_forecast_method: constant

index P_PV P_Load P_deferrable0 P_deferrable1 P_grid_pos P_grid_neg P_grid unit_load_cost unit_prod_price cost_profit cost_fun_profit
2024-01-31 07:00:00+01:00 -2 458 0 1920 2381 0 2381 0.112 0.07 -0.133 -0.133
2024-01-31 07:30:00+01:00 -2 466 0 1920 2389 0 2389 0.112 0.07 -0.134 -0.134
2024-01-31 08:00:00+01:00 -2 441 0 1920 2364 0 2364 0.112 0.07 -0.132 -0.132
2024-01-31 08:30:00+01:00 -2 320 0 1920 2243 0 2243 0.112 0.07 -0.126 -0.126
2024-01-31 09:00:00+01:00 -2 356 0 1920 2279 0 2279 0.112 0.07 -0.128 -0.128
2024-01-31 09:30:00+01:00 131 387 0 0 256 0 256 0.112 0.07 -0.014 -0.014
2024-01-31 10:00:00+01:00 454 509 0 0 55 0 55 0.112 0.07 -0.003 -0.003
2024-01-31 10:30:00+01:00 681 521 1800 0 1640 0 1640 0.112 0.07 -0.092 -0.092
2024-01-31 11:00:00+01:00 855 457 1800 0 1401 0 1401 0.112 0.07 -0.078 -0.078
2024-01-31 11:30:00+01:00 1024 494 1800 0 1270 0 1270 0.112 0.07 -0.071 -0.071
2024-01-31 12:00:00+01:00 1164 576 1800 0 1211 0 1211 0.112 0.07 -0.068 -0.068
2024-01-31 12:30:00+01:00 1180 514 1800 0 1134 0 1134 0.112 0.07 -0.063 -0.063
2024-01-31 13:00:00+01:00 1141 489 1800 0 1147 0 1147 0.112 0.07 -0.064 -0.064
2024-01-31 13:30:00+01:00 1024 720 1800 0 1496 0 1496 0.112 0.07 -0.084 -0.084
2024-01-31 14:00:00+01:00 869 612 1800 0 1543 0 1543 0.112 0.07 -0.086 -0.086
2024-01-31 14:30:00+01:00 686 600 0 1920 1834 0 1834 0.112 0.07 -0.103 -0.103
2024-01-31 15:00:00+01:00 486 406 0 1920 1840 0 1840 0.112 0.07 -0.103 -0.103
2024-01-31 15:30:00+01:00 407 379 0 1920 1892 0 1892 0.112 0.07 -0.106 -0.106

Ok I see why. In the code as the LP problem is formulated for now, that load starting at a value (1920) is not counted as a startup. A bit tricky to deal with these boundary conditions.
I will fix this in a future release.

Ok great. Looking forward to that release then. Thanks

Can you show how you have configured this in HA?
Rest command, maybe some templates…

thanks, works good now!

Another question out of interest

Does the EMHASS model makes a prediction based on all past data and does it distinguish the day of the week?

For example, on weekdays we have a different load profile than weekend.
Can it predict this?

Or is it better in this case to make manual load profiles (csv or list) and then pass them to EMHASS?

Yes definetly. This is actually one the good reasons to use this ML model. These variables are passed as covariates: year, month of the year, day_of_week, day_of_year, day, hour.

So along with the history data of your sensor, these variables will be also used to generate the future forecasts.

Thanks,

Because I always have the feeling it is based on day-1.

Is this because I do a naive-mpc-optim every 5 minutes? should I turn this off, or is there another way to pass SOC to EMHASS?

2024-02-01 11:56:11,391 - web_server - ERROR - The retrieved JSON is empty, check that correct day or variable names are passed
2024-02-01 11:56:11,392 - web_server - ERROR - Either the names of the passed variables are not correct or days_to_retrieve is larger than the recorded history of your sensor (check your recorder settings)
2024-02-01 11:56:11,392 - web_server - ERROR - Exception on /action/naive-mpc-optim [POST]
Traceback (most recent call last):
File “/usr/local/lib/python3.11/dist-packages/flask/app.py”, line 1463, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/flask/app.py”, line 872, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/flask/app.py”, line 870, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/flask/app.py”, line 855, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/emhass/web_server.py”, line 50, in action_call
input_data_dict = set_input_data_dict(config_path, str(data_path), costfun,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/emhass/command_line.py”, line 120, in set_input_data_dict
P_load_forecast = fcst.get_load_forecast(method=optim_conf[‘load_forecast_method’], set_mix_forecast=True, df_now=df_input_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/emhass/forecast.py”, line 586, in get_load_forecast
rh.get_data(days_list, var_list)
File “/usr/local/lib/python3.11/dist-packages/emhass/retrieve_hass.py”, line 150, in get_data
self.df_final = pd.concat([self.df_final, df_day], axis=0)
^^^^^^
UnboundLocalError: cannot access local variable ‘df_day’ where it is not associated with a value

Any idea how to resolve? I didn’t change anything

By default it only uses day -1, if you want to use more history you need to train and utilise the machine learning forecaster.
https://emhass.readthedocs.io/en/latest/mlforecaster.html

You don’t need to turn off MPC for this feature.

1 Like