EMHASS: An Energy Management for Home Assistant

Dear community!

I have created a new project called EMHASS, written in Python, to implement an optimized energy management for a household and with easy integration to Home Assistant. The main contribution of this project is that the energy management strategy is… well… optimized! I’ve done this using a real mathematical optimization technique called Linear Programming.

The main use case for this project will be for someone who has installed some PV solar panels in his house and is already using Home Assistant to control (on/off) some crucial power consumption in his home. For example the water heater, the pool pump, a dispatchable dishwasher, and so on. We can also imagine that he has installed an optional battery like a PowerWall, in order to maximize the PV self-consumption. With Home Assistant he also has sensors that can measure the power produced by the PV plant, the global power consumption of the house and hopefully the power consumed by the controllable loads. Home Assistant has just released the Energy Dashboard where we can visualize all these variables in some really good looking graphics. See: https://www.home-assistant.io/blog/2021/08/04/home-energy-management/.

Now the questions are, how can we be certain of the good and optimal management of these devices? If we define a fixed schedule for our deferrable loads, is this the best solution? When we can indicate or force a charge or discharge on the battery? When should I optimally start my water heater? This is a well known academic problem for an Energy Management System.

The first and most basic approach could be to define some basic rules or heuristics, this is the so called rule-based approach. The rules could be some fixed schedules for the deferrable loads, or some threshold based triggering of the battery charge/discharge, and so on. The rule-based approach has the advantage of being simple to implement and robust. However, the main disadvantage is that optimality is not guaranteed.

The goal of this work is to provide an easy to implement framework where anyone using Home Assistant can apply the best and optimal set of instructions to control the energy flow in a household.

When I was designing and testing this package in my own house I estimated a daily gain between 5% and 8% when using the optimized approach versus a rule-based one. In my house I have a 5 kWp PV installation with a contractual grid supply of 9 kVA. I have a grid contract with two tariffs for power consumption for the grid (peak and non-peak hours) and one tariff for the excess PV energy injected to the grid. I have no battery installed, but I suppose that the margin of gain would be even bigger with a battery, adding flexibility to the energy management. Of course the disadvantage is the initial capital cost of the battery stack. In my case the gain comes from the fact that the EMS is helping me to decide when to turn on my water heater and the pool pump. If we have a good clear sky day the results of the optimization will normally be to turn them on during the day where solar production is present. But if the day is going to be really clouded, then is possible that the best solution will be to turn them on during the non-peak tariff hours, for my case this is during the night from 9pm to 2am. All these decisions are made automatically by the EMS using forecasts of both the PV production and the house power consumption.

Some other good packages and projects offer similar approaches as EMHASS. I can cite for example the good work done by my friends at the G2ELab in Grenoble, France. They have implemented the OMEGAlpes package that can also be used as an optimized EMS using LP and MILP (see: (omegalpes) [OMEGAlpes / OMEGAlpes · GitLab]). But here in EMHASS the first goal was to keep it simple to implement using configuration files and the second goal was that it should be easy to integrate to Home Assistant. I am sure that there will be a lot of room for optimize the code and the package implementation as this solution will be used and tested in the future. I hope that this project come as and can be used as a perfect complement for the just released Energy Management and Energy Dashboard by Home Assistant.

So where to begin?

You will find all the needed instructions to install and use the package, but if you have any questions I will happily try to help anyone interested in this project. Leave a message on this thread or if you find any bugs open new issues on those github repositories.

There is a complete documentation that can be found here: https://emhass.readthedocs.io/

And there is a separate thread presenting the add-on: https://community.home-assistant.io/t/emhass-add-on-an-energy-management-optimization-add-on-for-home-assistant-os-and-supervised/405649

If you like this work please consider buying a coffee :wink:

Buy Me A Coffee

47 Likes

Hi,

I just came over your project and it sounds very interesting.

Might it be possible to create a HACS integration or an Add-On for Home Assistant out of it so it can also be used by people running on Home Assistant OS?

Is it possible to use hourly variable pricing for consumption and return to grid if the prices are available for the following day?

Would it be possible to set a time-frame in which a specific consumer gets turned on (like if you don’t want the pool filter to run during the night)?

For heatpumps (especially for water heating), it would be cool to set an average time it takes to heat up to the desired temperature, while it should then only be turned off based on the measured temperature and not on a fixed time.

Thanks

Hi, thanks for the input.

I will try to answer your questions.

Might it be possible to create a HACS integration or an Add-On for Home Assistant out of it so it can also be used by people running on Home Assistant OS?

Of course, it is the ultimate goal. I just realeased the first version to work with the bare Home Assistant Core, as this is the same version that I have been using for a long time now. But I’m completely aware that the vast majority of users use HA OS. This is in my TODO list, for now I just had the time to read the HA developers site to understand how to do this. If you or anyone can help me to port this as an add-on it could also be grat.

Is it possible to use hourly variable pricing for consumption and return to grid if the prices are available for the following day?

For now this is not possible but again in the TODO list. I will add support for this soon as the easy solution would be to let the user upload a CSV file with their hourly consumption and selling prices.

Would it be possible to set a time-frame in which a specific consumer gets turned on (like if you don’t want the pool filter to run during the night)?

There is no a direct parameter to control this. I have to think of a way to add this in the future. However, for now you can achieve this by fixing the high and low consumption periods and set a really high price to the undesired period of operation. This way the optimization will never converge to a solution on this period.

For heatpumps (especially for water heating), it would be cool to set an average time it takes to heat up to the desired temperature, while it should then only be turned off based on the measured temperature and not on a fixed time.

Again in the list to improve the software. This will need some sort of thermal modeling which I intent to add in the future. You could however add your own model in HA with automations on measured temperature and interact with EMHASS using only the average time.

I will keep updates on future improvements.

2 Likes

Hi, just released a new version of EMHASS where it is now possible to use hourly variable pricing for consumption and return to grid from a custom CSV file.

The new version is here: emhass · PyPI

4 Likes

Perfect @davidusb !

Exactly what I was searching. Sadly I have to wait for HACS or Addon integration since I’m on Raspi.

One more addition: I have my excess PV currently powering a 3 kW heating rod in my hot water system.
To follow the PV curve I have attached a Tasmota Dimmer (Robotdyn Zero Cross) which i regulate from 0-100%.
Maybe this can also be implemented in the future?

Thank you!
Looking forward when this hits HA

1 Like

Hi there. Yes the Add-on is a work in progress.
For the Dimmer device it looks nice. In my case however it will not stand the rated current of my hot water rod. Otherwise, the package that I proposed is intended for optimization (planning) of the home energy. The load following using excess power from PV can be directly implemented using automations in HA. You can see my package as a higher level energy management, but still low level management strategies as the load following are needed. If I put it differently, you can automate your system to use the excess PV as you said and my package can tell you in advance (at the beginning of the day) at which times of the day you should operate your hot water rod as such in order to optimize your energy management (minimize the electricity bill for example).

nice!

currently i have running:

  • Pool Pump
  • Electric Water Heater
  • Heat Pump (Air)
  • Solar Water Heater
  • Oil Central Heating
  • PV Power
  • Wind Power
  • (in Winter: Hot Water from a wood fired oven)
  • Wallbox/Electric Car

so plenty of optimization needed :wink:

Is there a planning for a HACS/Add-On release?

Hi. I’m working on it whenever I get some spare time, so I can’t take the engagement of a release date. But it will be anytime soon in the following 3 to 4 weeks

2 Likes

Amazing looking forward. Let me know if you need testers.

Hi David, i’m very exited if you would release an HACS Release. Could you provide us a little Update of the development? I’d like to test the Add-on :slight_smile:

Hi, yes just found some spare time lately. I’m working on it.
Here is my repo: GitHub - davidusb-geek/emhass-add-on: The Home Assistant Add-on for EMHASS: Energy Management Optimization for Home Assistant
However is totally a work in progress. I’m currently stuck on my add-on installation and using pip3 on the Dockerfile… Right now looking for a solution.

2 Likes

Hi everyone, I just released the first version of this add-on.

The add-on repository and installation instructions are here: https://github.com/davidusb-geek/emhass-add-on

I am presenting this add-on on this new topic:

Feel free to test it and give me any feedback.
If you encounter any problem you can directly open an issue on the add-on repository: Issues · davidusb-geek/emhass-add-on · GitHub

Cheers!!!

1 Like

Thank you for this.

I get a “Invalid Add-on repository!” when adding the Github repository “GitHub - davidusb-geek/emhass: emhass: Energy Management for Home Assistant

Any suggestions please?

Hi, yes you should be adding the add-on repository and not the emhass module repository.

Add this: GitHub - davidusb-geek/emhass-add-on: The Home Assistant Add-on for EMHASS: Energy Management Optimization for Home Assistant

I got the error: The command ‘/bin/sh -c apt-get update && apt-get install -y --no-install-recommends git && pip3 install --no-cache-dir -r requirements.txt’ returned a non-zero code: 1

Mmmm a quick search talked about this error when the “-y” option is missing, however this option is there in that command. What is your setup?

Raspberry with home assistant HAOS on raspberry 3 (32bit)

Any chance of more details from your supervisor logs?

          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/setup.py", line 443, in <module>
            setup_package()
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/setup.py", line 435, in setup_package
            setup(**metadata)
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/core.py", line 171, in setup
            return old_setup(**new_attr)
          File "/usr/local/lib/python3.8/site-packages/setuptools/__init__.py", line 153, in setup
            return distutils.core.setup(**attrs)
          File "/usr/local/lib/python3.8/distutils/core.py", line 148, in setup
            dist.run_commands()
          File "/usr/local/lib/python3.8/distutils/dist.py", line 966, in run_commands
            self.run_command(cmd)
          File "/usr/local/lib/python3.8/distutils/dist.py", line 985, in run_command
            cmd_obj.run()
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/install.py", line 62, in run
            r = self.setuptools_run()
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/install.py", line 36, in setuptools_run
            return distutils_install.run(self)
          File "/usr/local/lib/python3.8/distutils/command/install.py", line 545, in run
            self.run_command('build')
          File "/usr/local/lib/python3.8/distutils/cmd.py", line 313, in run_command
            self.distribution.run_command(command)
          File "/usr/local/lib/python3.8/distutils/dist.py", line 985, in run_command
            cmd_obj.run()
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/build.py", line 47, in run
            old_build.run(self)
          File "/usr/local/lib/python3.8/distutils/command/build.py", line 135, in run
            self.run_command(cmd_name)
          File "/usr/local/lib/python3.8/distutils/cmd.py", line 313, in run_command
            self.distribution.run_command(command)
          File "/usr/local/lib/python3.8/distutils/dist.py", line 985, in run_command
            cmd_obj.run()
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/build_src.py", line 142, in run
            self.build_sources()
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/build_src.py", line 153, in build_sources
            self.build_library_sources(*libname_info)
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/build_src.py", line 286, in build_library_sources
            sources = self.generate_sources(sources, (lib_name, build_info))
          File "/tmp/pip-install-2gih2rmj/numpy_1441dd882a91498892b69d0ea99d79cc/numpy/distutils/command/build_src.py", line 369, in generate_sources
            source = func(extension, build_dir)
          File "numpy/core/setup.py", line 669, in get_mathlib_info
            raise RuntimeError("Broken toolchain: cannot link a simple C program")
        RuntimeError: Broken toolchain: cannot link a simple C program
        [end of output]
  
    note: This error originates from a subprocess, and is likely not a problem with pip.
  error: legacy-install-failure
  
  × Encountered error while trying to install package.
  ╰─> numpy
  
  note: This is an issue with the package mentioned above, not pip.
  hint: See above for output from the failure.
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.

error: subprocess-exited-with-error
× pip subprocess to install build dependencies did not run successfully.
│ exit code: 1
– See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.

Removing intermediate container 89876dbb4803