Sometime ago I developed an add-on called EMHASS that can be used to optimize your home energy bill by providing an optimal schedule of the home controllable loads. For this optimization to work some forecasts are needed. Among those are the home load power consumption. To improve the precision of the optimization I developed a new class within the EMHASS module that uses machine learning model to forecast the load consumption time series.
This works well and I’m very satisfied with the results.
But I realized that this module can be actually used to forecast any Home Assistant sensor, as long as it is stationary and you have enough data to train the ML model.
This is a quick procedure to get started and to use the new machine learning feature:
1.- Install the EMHASS add-on by using the repository: GitHub - davidusb-geek/emhass-add-on: The Home Assistant Add-on for EMHASS: Energy Management Optimization for Home Assistant
2.- Open the add-on webui and fit the default model with the default parameters by using the buttons there:
3.- After clicking on “ML forecast model fit” you can check the add-on logs to see the fit metrics results. In my case the logs show:
And refreshing the webui now shows the graph with the results:
Zooming in:
This used the default parameters which are these:
runtimeparams = {
"days_to_retrieve": 30,
"model_type": "load_forecast",
"var_model": "sensor.power_load_no_var_loads",
"sklearn_model": "KNeighborsRegressor",
"num_lags": 48,
"split_date_delta": '48h',
"perform_backtest": False
}
As we can see this is using a default sensor name that is useful for energy optimization purpose of the add-on. But in fact you can change any of these parameters by using a data dictionary during the curl
call.
For example we can change to another sensor and extend the quantity of data that will be used to train the ML model. Like this:
curl -i -H "Content-Type:application/json" -X POST -d '{"days_to_retrieve": 150, "var_model": "sensor.home_temperature"}' http://localhost:5000/action/forecast-model-fit
This will fetch 150 days of data for that sensor from Home Assistant. Try to provide as much data as you can. Check and change your recorder settings if you don’t have enough data. The recorder will retain only 7 days of data by default.
4.- The “ML forecast model tune” button can be used to launch a hyperparameter optimization using bayesian optimization. Again check the logs, in my case it shows:
We have better metric and we used backtest to validate this.
Refreshing the webui will give us the graphic results after this model tuning:
5.- You can automate all this by defining the curl
commands using the shell_command
service and some custom automations in Home Assistant.
The add-on exposes these endpoints for ML forecasting:
-
forecast-model-fit
: to fit the ML model using train/test split and backteting. -
forecast-model-predict
: to obtain predictions with the previously trained model. There is an option to publish the forecasted series to a new Home Assistant sensor. -
forecast-model-tune
: to tune the model hyperparameters using bayesian optimization.
Check the complete documentation of this feature here: The machine learning forecaster — emhass 0.4.8 documentation
Refit the model often (once a week?) to update to any changes on your consumption dynamic. Careful with the tuning routine that can be computation intensive for RPi devices. Limit the days_to_retrieve
parameter if the optimization takes too long.