davidusb-geek / emhass-add-on

The Home Assistant Add-on for EMHASS: Energy Management Optimization for Home Assistant
MIT License
78 stars 23 forks source link

ERROR - The ML forecaster file was not found, please run a model fit method before this predict method #78

Closed overas closed 4 months ago

overas commented 4 months ago

Emhass 0.8.0 Hassos 2024.2.3

It seems the ML forecaster file is not stored after a restart of emhass addon. Reproduce the error: 1) Restart emhass addon. Everything is ok.

2024-02-26 19:13:17,761 - web_server - INFO - Launching the emhass webserver at: http://0.0.0.0:5000
2024-02-26 19:13:17,762 - web_server - INFO - Home Assistant data fetch will be performed using url: http://supervisor/core/api
2024-02-26 19:13:17,762 - web_server - INFO - The data path is: /app/data
2024-02-26 19:13:17,763 - web_server - INFO - Using core emhass version: 0.8.0
waitress   INFO  Serving on http://0.0.0.0:5000

2) Run RESTful Command: trigger_forecast

  trigger_forecast:
    url: http://localhost:5000/action/dayahead-optim
    method: POST
    content_type: "application/json"
    timeout: 300
    payload: >-
      {
      "load_cost_forecast":{{((state_attr('sensor.nordpool_med_nettleie', 'raw_today') | map(attribute='value') | list  + state_attr('sensor.nordpool_med_nettleie','raw_tomorrow') | map(attribute='value') | list))[now().hour:][:24] }},
      "prod_price_forecast":{{((state_attr('sensor.nordpool_uten_nettleie', 'raw_today') | map(attribute='value') | list  + state_attr('sensor.nordpool_uten_nettleie', 'raw_tomorrow') | map(attribute='value') | list))[now().hour:][:24]}}
      }

Here is the log after emhass addon restart and after running emhass forecast.

2024-02-26 19:04:28,808 - web_server - INFO - Launching the emhass webserver at: http://0.0.0.0:5000
2024-02-26 19:04:28,808 - web_server - INFO - Home Assistant data fetch will be performed using url: http://supervisor/core/api
2024-02-26 19:04:28,808 - web_server - INFO - The data path is: /app/data
2024-02-26 19:04:28,809 - web_server - INFO - Using core emhass version: 0.8.0
waitress   INFO  Serving on http://0.0.0.0:5000
2024-02-26 19:04:34,583 - web_server - INFO -  >> Setting input data dict
2024-02-26 19:04:34,584 - web_server - INFO - Setting up needed data
2024-02-26 19:04:34,623 - web_server - INFO - Retrieving weather forecast data using method = scrapper
2024-02-26 19:04:35,642 - web_server - INFO - Retrieving data from hass for load forecast using method = mlforecaster
2024-02-26 19:04:35,643 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-26 19:04:38,879 - web_server - ERROR - The ML forecaster file was not found, please run a model fit method before this predict method
2024-02-26 19:04:38,880 - web_server - ERROR - Exception on /action/dayahead-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 103, 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 93, in set_input_data_dict
    P_load_forecast = fcst.get_load_forecast(method=optim_conf['load_forecast_method'])
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/emhass/forecast.py", line 621, in get_load_forecast
    forecast_out = mlf.predict(data_last_window)
                   ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'predict'

3) Update mlforecast with service: rest_command.ml_forecast_fit and service: rest_command.ml_forecast_tune

2024-02-26 19:15:00,163 - web_server - INFO -  >> Setting input data dict
2024-02-26 19:15:00,163 - web_server - INFO - Setting up needed data
2024-02-26 19:15:00,166 - web_server - INFO -  >> Publishing data...
2024-02-26 19:15:00,166 - web_server - INFO - Publishing data to HASS instance
2024-02-26 19:15:00,166 - web_server - ERROR - File not found error, run an optimization task first.
2024-02-26 19:17:09,743 - web_server - INFO -  >> Setting input data dict
2024-02-26 19:17:09,743 - web_server - INFO - Setting up needed data
2024-02-26 19:17:09,747 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-26 19:17:19,080 - web_server - INFO -  >> Performing a machine learning forecast model fit...
2024-02-26 19:17:19,081 - web_server - INFO - Performing a forecast model fit for load_forecast
2024-02-26 19:17:19,085 - web_server - INFO - Training a KNeighborsRegressor model
2024-02-26 19:17:19,111 - web_server - INFO - Elapsed time for model fit: 0.025098323822021484
2024-02-26 19:17:19,141 - web_server - INFO - Prediction R2 score of fitted model on test data: -0.484651809824203
2024-02-26 19:17:29,751 - web_server - INFO -  >> Setting input data dict
2024-02-26 19:17:29,752 - web_server - INFO - Setting up needed data
2024-02-26 19:17:29,756 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-26 19:17:38,972 - web_server - INFO -  >> Performing a machine learning forecast model tune...
2024-02-26 19:17:38,973 - web_server - INFO - Bayesian hyperparameter optimization with backtesting
Number of models compared: 70,
         10 bayesian search in each lag configuration.

lags grid:   0%|          | 0/7 [00:00<?, ?it/s]
lags grid:  14%|█▍        | 1/7 [00:02<00:13,  2.22s/it]
lags grid:  29%|██▊       | 2/7 [00:02<00:05,  1.17s/it]
lags grid:  43%|████▎     | 3/7 [00:03<00:03,  1.19it/s]
lags grid:  57%|█████▋    | 4/7 [00:03<00:01,  1.51it/s]
lags grid:  71%|███████▏  | 5/7 [00:03<00:01,  1.78it/s]
lags grid:  86%|████████▌ | 6/7 [00:04<00:00,  1.89it/s]
lags grid: 100%|██████████| 7/7 [00:04<00:00,  2.04it/s]
lags grid: 100%|██████████| 7/7 [00:04<00:00,  1.47it/s]
2024-02-26 19:17:43,747 - web_server - INFO - Elapsed time: 4.772895097732544
2024-02-26 19:17:43,830 - web_server - INFO - R2 score for optimized prediction in train period: 0.13894382525291882
2024-02-26 19:17:43,839 - web_server - INFO - R2 score for optimized prediction in test period: -0.3226502690477693
2024-02-26 19:17:43,839 - web_server - INFO - Number of optimal lags obtained: 24

4) Run RESTful Command: trigger_forecast again. Now it works

2024-02-26 19:19:06,139 - web_server - INFO -  >> Setting input data dict
2024-02-26 19:19:06,139 - web_server - INFO - Setting up needed data
2024-02-26 19:19:06,144 - web_server - INFO - Retrieving weather forecast data using method = scrapper
2024-02-26 19:19:07,168 - web_server - INFO - Retrieving data from hass for load forecast using method = mlforecaster
2024-02-26 19:19:07,169 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-26 19:19:10,142 - web_server - INFO -  >> Performing dayahead optimization...
2024-02-26 19:19:10,142 - web_server - INFO - Performing day-ahead forecast optimization
2024-02-26 19:19:10,152 - web_server - INFO - Perform optimization for the day-ahead
2024-02-26 19:19:10,172 - web_server - INFO - Status: Optimal
2024-02-26 19:19:10,172 - web_server - INFO - Total value of the Cost function = -95.27

5) After a restart of emhass addon the error are ther again

2024-02-26 19:21:06,031 - web_server - INFO - Launching the emhass webserver at: http://0.0.0.0:5000
2024-02-26 19:21:06,031 - web_server - INFO - Home Assistant data fetch will be performed using url: http://supervisor/core/api
2024-02-26 19:21:06,032 - web_server - INFO - The data path is: /app/data
2024-02-26 19:21:06,033 - web_server - INFO - Using core emhass version: 0.8.0
waitress   INFO  Serving on http://0.0.0.0:5000
2024-02-26 19:21:13,739 - web_server - INFO -  >> Setting input data dict
2024-02-26 19:21:13,739 - web_server - INFO - Setting up needed data
2024-02-26 19:21:13,772 - web_server - INFO - Retrieving weather forecast data using method = scrapper
2024-02-26 19:21:14,901 - web_server - INFO - Retrieving data from hass for load forecast using method = mlforecaster
2024-02-26 19:21:14,901 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-26 19:21:18,215 - web_server - ERROR - The ML forecaster file was not found, please run a model fit method before this predict method
2024-02-26 19:21:18,215 - web_server - ERROR - Exception on /action/dayahead-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 103, 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 93, in set_input_data_dict
    P_load_forecast = fcst.get_load_forecast(method=optim_conf['load_forecast_method'])
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/emhass/forecast.py", line 621, in get_load_forecast
    forecast_out = mlf.predict(data_last_window)
                   ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'predict'
overas commented 4 months ago

Using Emhass 0.6.6 addon I do not get this error

Here is the log after emhass addon restart and after running emhass forecast.

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
services-up: info: copying legacy longrun emhass (no readiness notification)
s6-rc: info: service legacy-services successfully started
2024-02-26 20:15:00,867 - web_server - INFO - Launching the emhass webserver at: http://0.0.0.0:5000
2024-02-26 20:15:00,867 - web_server - INFO - Home Assistant data fetch will be performed using url: http://supervisor/core/api
2024-02-26 20:15:00,867 - web_server - INFO - The data path is: /share
2024-02-26 20:15:00,868 - web_server - INFO - Using core emhass version: 0.7.7
waitress   INFO  Serving on http://0.0.0.0:5000
2024-02-26 20:15:05,885 - web_server - INFO - Setting up needed data
2024-02-26 20:15:05,922 - web_server - INFO - Retrieving weather forecast data using method = scrapper
2024-02-26 20:15:06,940 - web_server - INFO - Retrieving data from hass for load forecast using method = mlforecaster
2024-02-26 20:15:06,941 - web_server - INFO - Retrieve hass get data method initiated...
2024-02-26 20:15:10,182 - web_server - INFO -  >> Performing dayahead optimization...
2024-02-26 20:15:10,182 - web_server - INFO - Performing day-ahead forecast optimization
2024-02-26 20:15:10,191 - web_server - INFO - Perform optimization for the day-ahead
2024-02-26 20:15:10,213 - web_server - INFO - Status: Optimal
2024-02-26 20:15:10,213 - web_server - INFO - Total value of the Cost function = -130.68
GeoDerp commented 4 months ago

Mm, this may be because we need to set the data folder to /data to be persistent. The restart may be in some way resetting the files inside the docker. 🤔

I'll see if I can test this today: DATA_PATH=/app/data To: DATA_PATH=/data

davidusb-geek commented 4 months ago

We had some similar problems some time ago. The solution was to write temporary files to the share folder in HA, outside the add-on container.

GeoDerp commented 4 months ago

@overas , this should be fixed in the next version. (0.8.1) 👍

overas commented 4 months ago

Thanks for the fast fix. Closing case