EMHASS: An Energy Management for Home Assistant

Allright, found the issue:
The ./setup.sh file had line endings in Windows format (CRLF). Changed it to Unix type line endings (LF) in VS Code:

Might require a change in the setup.sh file in the Github repo? Will check.

No, not yet.
The boundary condition posing problems in this case is just the initial value of a deferrable load for the first time step. But as computing the number of deferrable startups depends on the difference of Pdef[k] - Pdef[k-1] then there is an issue to solve for the first time step. Setting it to zero is not an option as it could break MPC notably. So we need to figure out how to treat this.
The equations in the code are here:

I donā€™t use dev containers to develop.
Either use github codespaces online or either you can do everything locally on your PC.
Whatever option the procedure is the same for me:

  1. Set up a virtual environment: python -m venv .venv (here the virtual environment is called .venv)
  2. Activate the env: source .venv/bin/activate (if using Windows instead this would be .venv\Scripts\activate.bat
  3. Then git clone the EMHASS repository and from within the repo and with the virtual env activated just install the emhass package in editable mode with this: python -m pip install -e .
  4. VSCode should automatically catch that a new virtual env was created so you can start developing from there

Hope it helps

On this topic I just made a pull request presenting my two (in development) options for running and testing the code: Optional: two additional testing environment options by GeoDerp Ā· Pull Request #182 Ā· davidusb-geek/emhass Ā· GitHub

Do note I use Linux, this shouldnā€™t be any different in windows. But let me know.

Can anyone help me create a sensor for EMHASS load_cost_forcast on HA from the Octopus Agile Integration?

I have: event.octopus_energy_electricity_next_day_rates which looks likeā€¦

event_types:
  - octopus_energy_electricity_next_day_rates
event_type: octopus_energy_electricity_next_day_rates
rates:
  - start: "2024-02-05T00:00:00+00:00"
    end: "2024-02-05T00:30:00+00:00"
    value_inc_vat: -0.021
    is_capped: false
  - start: "2024-02-05T00:30:00+00:00"
    end: "2024-02-05T01:00:00+00:00"
    value_inc_vat: 0.0105
    is_capped: false
  - start: "2024-02-05T01:00:00+00:00"
    end: "2024-02-05T01:30:00+00:00"
    value_inc_vat: 0.0105
    is_capped: false
  - start: "2024-02-05T01:30:00+00:00"
    end: "2024-02-05T02:00:00+00:00"
    value_inc_vat: -0.01638
    is_capped: false
  - start: "2024-02-05T02:00:00+00:00"
    end: "2024-02-05T02:30:00+00:00"
    value_inc_vat: 0.0084
    is_capped: false
  - start: "2024-02-05T02:30:00+00:00"
    end: "2024-02-05T03:00:00+00:00"
    value_inc_vat: 0
    is_capped: false
  - start: "2024-02-05T03:00:00+00:00"
    end: "2024-02-05T03:30:00+00:00"
    value_inc_vat: -0.0063
    is_capped: false
  - start: "2024-02-05T03:30:00+00:00"
    end: "2024-02-05T04:00:00+00:00"
    value_inc_vat: -0.02562
    is_capped: false
  - start: "2024-02-05T04:00:00+00:00"
    end: "2024-02-05T04:30:00+00:00"
    value_inc_vat: 0
    is_capped: false
  - start: "2024-02-05T04:30:00+00:00"
    end: "2024-02-05T05:00:00+00:00"
    value_inc_vat: -0.01638
    is_capped: false
  - start: "2024-02-05T05:00:00+00:00"
    end: "2024-02-05T05:30:00+00:00"
    value_inc_vat: 0.0504
    is_capped: false
  - start: "2024-02-05T05:30:00+00:00"
    end: "2024-02-05T06:00:00+00:00"
    value_inc_vat: 0.07728
    is_capped: false
  - start: "2024-02-05T06:00:00+00:00"
    end: "2024-02-05T06:30:00+00:00"
    value_inc_vat: 0.11886
    is_capped: false
  - start: "2024-02-05T06:30:00+00:00"
    end: "2024-02-05T07:00:00+00:00"
    value_inc_vat: 0.1764
    is_capped: false
  - start: "2024-02-05T07:00:00+00:00"
    end: "2024-02-05T07:30:00+00:00"
    value_inc_vat: 0.1239
    is_capped: false
  - start: "2024-02-05T07:30:00+00:00"
    end: "2024-02-05T08:00:00+00:00"
    value_inc_vat: 0.1764
    is_capped: false
  - start: "2024-02-05T08:00:00+00:00"
    end: "2024-02-05T08:30:00+00:00"
    value_inc_vat: 0.1491
    is_capped: false
  - start: "2024-02-05T08:30:00+00:00"
    end: "2024-02-05T09:00:00+00:00"
    value_inc_vat: 0.16947
    is_capped: false
  - start: "2024-02-05T09:00:00+00:00"
    end: "2024-02-05T09:30:00+00:00"
    value_inc_vat: 0.13734
    is_capped: false
  - start: "2024-02-05T09:30:00+00:00"
    end: "2024-02-05T10:00:00+00:00"
    value_inc_vat: 0.14826
    is_capped: false
  - start: "2024-02-05T10:00:00+00:00"
    end: "2024-02-05T10:30:00+00:00"
    value_inc_vat: 0.1407
    is_capped: false
  - start: "2024-02-05T10:30:00+00:00"
    end: "2024-02-05T11:00:00+00:00"
    value_inc_vat: 0.14049
    is_capped: false
  - start: "2024-02-05T11:00:00+00:00"
    end: "2024-02-05T11:30:00+00:00"
    value_inc_vat: 0.14217
    is_capped: false
  - start: "2024-02-05T11:30:00+00:00"
    end: "2024-02-05T12:00:00+00:00"
    value_inc_vat: 0.15939
    is_capped: false
  - start: "2024-02-05T12:00:00+00:00"
    end: "2024-02-05T12:30:00+00:00"
    value_inc_vat: 0.1428
    is_capped: false
  - start: "2024-02-05T12:30:00+00:00"
    end: "2024-02-05T13:00:00+00:00"
    value_inc_vat: 0.14112
    is_capped: false
  - start: "2024-02-05T13:00:00+00:00"
    end: "2024-02-05T13:30:00+00:00"
    value_inc_vat: 0.14448
    is_capped: false
  - start: "2024-02-05T13:30:00+00:00"
    end: "2024-02-05T14:00:00+00:00"
    value_inc_vat: 0.1365
    is_capped: false
  - start: "2024-02-05T14:00:00+00:00"
    end: "2024-02-05T14:30:00+00:00"
    value_inc_vat: 0.13587
    is_capped: false
  - start: "2024-02-05T14:30:00+00:00"
    end: "2024-02-05T15:00:00+00:00"
    value_inc_vat: 0.13167
    is_capped: false
  - start: "2024-02-05T15:00:00+00:00"
    end: "2024-02-05T15:30:00+00:00"
    value_inc_vat: 0.13209
    is_capped: false
  - start: "2024-02-05T15:30:00+00:00"
    end: "2024-02-05T16:00:00+00:00"
    value_inc_vat: 0.13503
    is_capped: false
  - start: "2024-02-05T16:00:00+00:00"
    end: "2024-02-05T16:30:00+00:00"
    value_inc_vat: 0.2835
    is_capped: false
  - start: "2024-02-05T16:30:00+00:00"
    end: "2024-02-05T17:00:00+00:00"
    value_inc_vat: 0.315
    is_capped: false
  - start: "2024-02-05T17:00:00+00:00"
    end: "2024-02-05T17:30:00+00:00"
    value_inc_vat: 0.315
    is_capped: false
  - start: "2024-02-05T17:30:00+00:00"
    end: "2024-02-05T18:00:00+00:00"
    value_inc_vat: 0.31836
    is_capped: false
  - start: "2024-02-05T18:00:00+00:00"
    end: "2024-02-05T18:30:00+00:00"
    value_inc_vat: 0.31332
    is_capped: false
  - start: "2024-02-05T18:30:00+00:00"
    end: "2024-02-05T19:00:00+00:00"
    value_inc_vat: 0.294
    is_capped: false
  - start: "2024-02-05T19:00:00+00:00"
    end: "2024-02-05T19:30:00+00:00"
    value_inc_vat: 0.1512
    is_capped: false
  - start: "2024-02-05T19:30:00+00:00"
    end: "2024-02-05T20:00:00+00:00"
    value_inc_vat: 0.147
    is_capped: false
  - start: "2024-02-05T20:00:00+00:00"
    end: "2024-02-05T20:30:00+00:00"
    value_inc_vat: 0.13965
    is_capped: false
  - start: "2024-02-05T20:30:00+00:00"
    end: "2024-02-05T21:00:00+00:00"
    value_inc_vat: 0.12096
    is_capped: false
  - start: "2024-02-05T21:00:00+00:00"
    end: "2024-02-05T21:30:00+00:00"
    value_inc_vat: 0.12537
    is_capped: false
  - start: "2024-02-05T21:30:00+00:00"
    end: "2024-02-05T22:00:00+00:00"
    value_inc_vat: 0.04599
    is_capped: false
  - start: "2024-02-05T22:00:00+00:00"
    end: "2024-02-05T22:30:00+00:00"
    value_inc_vat: 0.08064
    is_capped: false
  - start: "2024-02-05T22:30:00+00:00"
    end: "2024-02-05T23:00:00+00:00"
    value_inc_vat: 0.042
    is_capped: false
min_rate: null
max_rate: null
average_rate: 1
mpan: 
serial_number: 
tariff_code: E-1R-AGILE-FLEX-22-11-25-B
friendly_name: Electricity Next Day Rates

Iā€™d like to end up with sensor.emhass_load_cost_forcast_AGILE in the correct format.

Thank you very much!

Hi There,

Iā€™m getting the following error in the log file and not sure what is going wrong.

2024-02-06 13:30:02,300 - 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 1455, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask/app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/emhass/web_server.py", line 181, 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 598, in get_load_forecast
    forecast_out.index = self.forecast_dates
    ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pandas/core/generic.py", line 6002, in __setattr__
    return object.__setattr__(self, name, value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas/_libs/properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "/usr/local/lib/python3.11/dist-packages/pandas/core/generic.py", line 730, in _set_axis
    self._mgr.set_axis(axis, labels)
  File "/usr/local/lib/python3.11/dist-packages/pandas/core/internals/managers.py", line 225, in set_axis
    self._validate_set_axis(axis, new_labels)
  File "/usr/local/lib/python3.11/dist-packages/pandas/core/internals/base.py", line 70, in _validate_set_axis
    raise ValueError(
ValueError: Length mismatch: Expected axis has 0 elements, new values have 32 elements

I took a close look at my load no var and noticed some negative values, will this cause such an error?

What could be wrong with my configuration?

Iā€™m using Nordpool prices and next day prices are available around 13pm. My idea was to define "prediction_horizon" in hours that prices are available, but seems that emhass still is asking 48h of data instead of 13h as is available at the moment.

Curl:

"curl -i -H \"Content-Type:application/json\" -X POST -d '{
    \"load_cost_forecast\":{{((state_attr('sensor.nordpool_with_vat_and_tarifss', 'raw_today') | map(attribute='value') | list  + state_attr('sensor.nordpool_with_vat_and_tarifss', 'raw_tomorrow') | map(attribute='value') | list))[now().hour:][:24] }},
    \"prod_price_forecast\":{{((state_attr('sensor.nordpool_kwh_ee_eur_3_10_0', 'raw_today') | map(attribute='value') | list  + state_attr('sensor.nordpool_kwh_ee_eur_3_10_0', 'raw_tomorrow') | map(attribute='value') | list))[now().hour:][:24] }}, 
    \"prediction_horizon\":{{min(48, (((state_attr('sensor.nordpool_kwh_ee_eur_3_10_0', 'raw_today')|map(attribute='value')|list + state_attr('sensor.nordpool_kwh_ee_eur_3_10_0', 'raw_tomorrow') | map(attribute='value')| list)[now().hour:][:48]|list|length)))}},    
    \"pv_power_forecast\":{{([states('sensor.solcast_pv_forecast_power_now')|int(0)] + state_attr('sensor.solcast_pv_forecast_forecast_today', 'detailedHourly')|selectattr('period_start','gt',utcnow()) | map(attribute='pv_estimate')|map('multiply',1000)|map('int')|list + state_attr('sensor.solcast_pv_forecast_forecast_tomorrow', 'detailedHourly')|selectattr('period_start','gt',utcnow()) | map(attribute='pv_estimate')|map('multiply',1000)|map('int')|list)| tojson}},
    \"def_total_hours\":[{{states('sensor.heat_pump_heating_time_today')}},{{states('sensor.heat_pump_how_water_time_today')}},4],
    \"P_deferrable_nom\":[{{ (states('sensor.heat_pump_heating_average_power') | float * 1000) | round(0) }},{{ (states('sensor.heat_pump_daily_energy_tariffs_hot_water') | float) * 1000 | round(0) }},2700],
    \"delta_forecast\":2
    }' http://192.168.1.35:5001/action/dayahead-optim"

Result:

"curl -i -H \"Content-Type:application/json\" -X POST -d '{
    \"load_cost_forecast\":[0.181, 0.181, 0.181, 0.181, 0.181, 0.205, 0.303, 0.364, 0.266, 0.248, 0.176, 0.186, 0.186],
    \"prod_price_forecast\":[0.1, 0.1, 0.1, 0.1, 0.1, 0.12, 0.2, 0.25, 0.17, 0.155, 0.096, 0.12, 0.12], 
    \"prediction_horizon\":13,    
    \"pv_power_forecast\":[1037, 835, 981, 620, 271, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 213, 472, 780, 945, 803, 474, 230, 46, 0, 0, 0, 0, 0, 0, 0],
    \"def_total_hours\":[3.88,0.0,4],
    \"P_deferrable_nom\":[1780,0.0,2700],
    \"delta_forecast\":2
    }' http://192.168.1.35:5001/action/dayahead-optim"

Error:

2024-02-06 11:05:32,294 - web_server - INFO - Setting up needed data
2024-02-06 11:05:32,298 - web_server - ERROR - ERROR: The passed data is either not a list or the length is not correct, length should be 48
2024-02-06 11:05:32,298 - web_server - ERROR - Passed type is <class 'list'> and length is 37
2024-02-06 11:05:32,298 - web_server - ERROR - ERROR: The passed data is either not a list or the length is not correct, length should be 48
2024-02-06 11:05:32,299 - web_server - ERROR - Passed type is <class 'list'> and length is 13
2024-02-06 11:05:32,299 - web_server - ERROR - ERROR: The passed data is either not a list or the length is not correct, length should be 48
2024-02-06 11:05:32,299 - web_server - ERROR - Passed type is <class 'list'> and length is 13
2024-02-06 11:05:32,304 - web_server - INFO - Retrieving weather forecast data using method = scrapper
2024-02-06 11:05:34,006 - web_server - INFO - Retrieving data from hass for load forecast using method = naive
2024-02-06 11:05:34,006 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-06 11:05:34,081 - web_server - INFO -  >> Performing dayahead optimization...
2024-02-06 11:05:34,081 - web_server - INFO - Performing day-ahead forecast optimization
2024-02-06 11:05:34,084 - web_server - INFO - Perform optimization for the day-ahead
2024-02-06 11:05:34,084 - web_server - ERROR - Exception on /action/dayahead-optim [POST]

Also Solcast gives 37 datapoints, but i guess i can somehow limit it?

EDIT: Itā€™s 48 datapoints, because i chnaged the freq in config to 30. Now itā€™s back to 24 datapoints as earlieer, but it looks like you cannot change the dayahead window and itā€™s always 24h when freq is 60. So i just have to wait for the nordpool data to come to test and in future run it 23:59 once and then can do MPC optimization on the go. right?

This is my current working post MPC call command with Nordpool.
Similar setup in same geographic area.
Maybe you will find some inspiration in this.

post_mpc_optim:
  "curl -i -H \"Content-Type: application/json\" -X POST -d '{\"prediction_horizon\":{{min(24,(state_attr('sensor.nordpool_kwh_lv_eur_3_083_0', 'raw_today') |selectattr('start','gt',utcnow()) |map(attribute='value') |list |length+state_attr('sensor.nordpool_kwh_lv_eur_3_083_0', 'raw_tomorrow') |map(attribute='value') |list |length+1))}},
  \"prod_price_forecast\":{{([states('sensor.nordpool_kwh_lv_eur_3_083_0')|float(0)] + state_attr('sensor.nordpool_kwh_lv_eur_3_083_0', 'raw_today') | selectattr('start','gt',utcnow()) | map(attribute='value') | list + state_attr('sensor.nordpool_kwh_lv_eur_3_083_0', 'raw_tomorrow') | map(attribute='value') | list)[:24] |tojson}},
  \"load_cost_forecast\":{{([states('sensor.nordpool_kwh_lv_eur_3_10_021')|float(0)] + state_attr('sensor.nordpool_kwh_lv_eur_3_10_021', 'raw_today') | selectattr('start','gt',utcnow()) | map(attribute='value') | list + state_attr('sensor.nordpool_kwh_lv_eur_3_10_021', 'raw_tomorrow') | map(attribute='value') | list)[:24] |tojson}},
  \"pv_power_forecast\":{{([states('sensor.solcast_pv_forecast_forecast_this_hour')|float(0)] + state_attr('sensor.solcast_pv_forecast_forecast_today', 'detailedHourly') | selectattr('period_start','gt',utcnow()) | map(attribute='pv_estimate') | map('multiply',1000) | map('int')|list + state_attr('sensor.solcast_pv_forecast_forecast_tomorrow', 'detailedHourly') | map(attribute='pv_estimate') | map('multiply',1000) | map('int') | list)[:24] | tojson}},
  \"load_power_forecast\":{{([states('sensor.house_consumption_power_no_def_loads_60m_mean') | int] + (states('input_text.fi_fo_buffer').split(', ') | map('int') | list)[1:]) | tojson}},
  \"soc_init\":{{(states('sensor.battery_state_of_charge')|int(0))/100 }},\"alpha\":1,\"beta\":0,\"optimization_time_step\":60,\"def_total_hours\":{{states('sensor.def_total_hours_ev_charging') |list}} ,\"def_end_timestep\":{{states('sensor.hours_till_ev_charged') |list}}}' http://localhost:5000/action/naive-mpc-optim"

This is the solution for an Octopus Agile integration:

  - platform: rest
    name: load_cost
    scan_interval: 3600   # every hour
    resource_template: >-
      https://api.octopus.energy/v1/products/AGILE-OUTGOING-19-05-13/electricity-tariffs/E-1R-AGILE-OUTGOING-19-05-13-A/standard-unit-rates/?period_from=
      {% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
      {{ ts_now | timestamp_custom ('%Y-%m-%dT%H:%M:%SZ', local=false) }}&period_to=
      {{ (ts_now + 24*60*60) | timestamp_custom ('%Y-%m-%dT%H:%M:%SZ', local=false) }}
    unit_of_measurement: 'p/kWh'
    value_template: >-
      {{ (value_json.results[-1].value_inc_vat) | float(7.5) }}
    json_attributes:
      - "results"

Then this template can be used to extract those list of values:

{{ (state_attr('sensor.load_cost', 'results') | map(attribute='value_inc_vat') | list)[:48][::-1] | tojson }}

I also found this log entry.

2024-02-07 07:40:01,624 - web_server - ERROR - Variable sensor.power_load_no_var_loads was not found. This is typically because no data could be retrieved from Home Assistant

Yet the sensor exists?

@davidusb Hi,
After tonights update I get this error again.

2024-02-07 07:15:31,001 - web_server - ERROR - The retrieved JSON is empty, check that correct day or variable names are passed
2024-02-07 07:15:31,002 - 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-07 07:15:31,002 - web_server - ERROR - Exception on /action/forecast-model-fit [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 146, in set_input_data_dict
    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
2024-02-07 07:15:42,336 - web_server - INFO - Setting up needed data
2024-02-07 07:15:42,345 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-07 07:15:42,374 - web_server - ERROR - The retrieved JSON is empty, check that correct day or variable names are passed
2024-02-07 07:15:42,374 - 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-07 07:15:42,375 - web_server - ERROR - Exception on /action/perfect-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 78, in set_input_data_dict
    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

Solved on the latest release.

Erase the.pkl files in the shared folder and try fit method again then optimization.

Hi David,

tried erasing the files but the error stays.

2024-02-07 07:51:40,298 - web_server - ERROR - The retrieved JSON is empty, check that correct day or variable names are passed
2024-02-07 07:51:40,298 - 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-07 07:51:40,299 - web_server - ERROR - Exception on /action/forecast-model-fit [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 146, in set_input_data_dict
    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
2024-02-07 07:52:03,800 - web_server - INFO - Setting up needed data
2024-02-07 07:52:03,809 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-07 07:52:03,843 - web_server - ERROR - The retrieved JSON is empty, check that correct day or variable names are passed
2024-02-07 07:52:03,843 - 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-07 07:52:03,844 - web_server - ERROR - Exception on /action/perfect-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 78, in set_input_data_dict
    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

I have a specific questions where I canā€™t seem to find the answer in the documenation or this forum. Tried searching and even fed all the info in chatGPT4, but no luck.

I would like to use this as a container, but only to predict PV and Load. It is working great but I canā€™t seem to find a way to easily acces the P_PV and P_Load values in home assistant. According to some info ChatGPT found there should be a sensor.p_load and a sensor.p_pv but itā€™s not there.

Would be a great help if someone could point me in the right direction.
Thanks in advance

Yes EMHASS can be used just for that.
Set a basic configuration with just one deferrable load.
Schedule an optimization (day-ahead or MPC) and schedule the data publish task.
This data publish task will provide several sensors to HA, including these two sensors: sensor.p_pv_forecast and sensor.p_load_forecast.

No need for chatGPT here, everything is very well explained in this table on the documentation: https://emhass.readthedocs.io/en/latest/intro.html#computed-variables-and-published-data

Youā€™re having a problem retrieving data from HA.
But with the given information I cannot assist you further.
Other than what already on the error message:

  • Check that correct day or variable names are passed
  • Check that the names of the passed variables are correct
  • Check that days_to_retrieve is larger than the recorded history of your sensor

You are having this error on a forecast-model-fit, so whats the amount of data period that you are trying to retrieve?

Everything worked until the update. I didnā€™t change anything, I only did the update.
Days to retrieve was set to 9 in the config and I now changed it to 2 but I recall it needed more days.
I had this problem before after an update from emhass and then I just waited a few days and it worked after that again. It seems like every time I update emhass something happens with the sensor data or something.

Now a few hours later it started working again on itā€™s own.

Exactly the same issue after a long reboot of HA; itā€™s like EMHASS cannot stomach a small gap in the sensor data. Oddly it does not happen at every reboot but this time, after updating 5 integrations it took a bit longer than normal for the reboot to complete and bingoā€¦ errors in emhass.

Deleted the files to no avail; also upgraded to the latest version. Typically it takes 48 hrs before the sensor glitch is digested and a clean data set is available, this could explain why yours is up again.

1 Like

For anyone coming across this issue.
It started working without me changing anything and I do not know why.
My assumption is that the negative values passed out of the 2 day history, but I could be completely wrong.

YMMV.