M0AGX / LB9MG

Amateur radio and embedded systems

Reducing Home Assistant RAM usage

I tried running Home Assistant with zigbee2mqtt on an A20 OLinuXino board that has only 1 GB of RAM. Startup and initial operation was okay but after a random while (days) the system locked up and become unreachable over the network. Without Home Assistant the board was running okay for weeks so I guessed that it must have been RAM usage.

The recommended amount of RAM for Home Assistant (around 2023/2024) is around 2 or 4 GB. Home Assistant is a huge codebase so it is not easy to find out which part is using resources. However, thanks to its modular design you can enable and disable individual modules to see how much RAM is used overall. Maybe I don't need all of the modules?

Modules are of course enabled in configuration.yaml. The "official" (and the only supported) way to configure Home Assistant is to start configuration.yaml with default_config:. This is a module that only pulls in other modules. But what does it actually contain? This depends on the exact Home Assistant version (and may change in the future). Of course github and the official documentation have the latest version. On my system I found it in /usr/local/lib/python3.9/dist-packages/homeassistant/components/default_config.

Because the default config may change in the future your configuration may stop working after an update. Some module may be optional now but will become necessary in the future so a Home Assistant update may break a customized configuration. That is why the default config is the only supported way. On the other hand, if the whole setup is simple and easy to test after an update then what can go wrong?

In my case the manifest.json of the default config looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
  "domain": "default_config",
  "name": "Default Config",
  "documentation": "https://www.home-assistant.io/integrations/default_config",
  "dependencies": [
    "application_credentials",
    "automation",
    "bluetooth",
    "cloud",
    "counter",
    "dhcp",
    "energy",
    "frontend",
    "hardware",
    "history",
    "homeassistant_alerts",
    "input_boolean",
    "input_button",
    "input_datetime",
    "input_number",
    "input_select",
    "input_text",
    "logbook",
    "logger",
    "map",
    "media_source",
    "mobile_app",
    "my",
    "network",
    "person",
    "scene",
    "schedule",
    "script",
    "ssdp",
    "sun",
    "system_health",
    "tag",
    "timer",
    "usb",
    "webhook",
    "zeroconf",
    "zone"
  ],
  "codeowners": ["@home-assistant/core"],
  "quality_scale": "internal",
  "integration_type": "system"
}

That is quite a lot. I can also immediately spot some modules that I know I am 100% not using for anything. The meaning of every module can be easily looked up in Home Assistant's documentation. Example for application_credentials. My home setup is very simple. I have some Zigbee devices connected using zigbee2mqtt, mosquitto as the broker, an electricity meter. I use Home Assistant (via the app and the browser) for control of some light switches. I don't have any special hardware and I don't have any complex automations.

Let's go one by one through the list to see which modules can be potentially disabled to save RAM.

  • application_credentials - Adds OAuth2 and cloud support, definitely not used.
  • automation - It runs the automations, I am using it. Let's keep it.
  • bluetooth - Self-explanatory. I do not even have Bluetooth hardware in the machine I am running HA on so I am definitely not using it.
  • cloud - I self-host my own remote access so I am not using it.
  • counter - May be useful for automations. Let's keep it.
  • dhcp - Self-explanatory. I don't have any IP-networked equipment (only Zigbee) so I am not using it.
  • energy - It tracks house energy usage. I am using it so I will keep it.
  • frontend - This is the web and app interface. Obviously I have to keep it.
  • hardware - It provides information about the hardware HA is running on. I don't need it.
  • history - Keeps history of various things (like sensors). I will keep it.
  • homeassistant_alerts - It provides "news" from HA developers. I will keep it.
  • input_boolean - I will keep all the input modules. They provide various input widgets.
  • input_button
  • input_datetime
  • input_number
  • input_select
  • input_text
  • logbook - Keeps yet another view of house history. I will keep it.
  • logger - Provides low-level logging to syslog. I will keep it.
  • map - I am not using the map in any way.
  • media_source - I don't use HA for any kind of playback so I don't need it.
  • mobile_app - I am using the app so I need it.
  • my - Provides links from documentation pages to your HA instance. I don't need it.
  • network - Provides network information to other integrations. I don't need it.
  • person - Provides people tracking. I definitely don't need it.
  • scene - I may use it in automations. I will keep it.
  • schedule - I will keep it to schedule automations.
  • script - I will keep it to script automations.
  • ssdp - I don't have any equipment that would use SSDP so I don't need this module.
  • sun - Might be useful to schedule day/night automations. I will keep it.
  • system_health - I don't use it.
  • tag - I don't use any tags so I don't need this module.
  • timer - I may use it in automations so I will keep it.
  • usb - I don't plug any USB hardware to the HA machine while it is running so I don't need it.
  • webhook - This module can expose some automations and actions through the web API. I don't need it.
  • zeroconf - I have a trivial networking setup so I don't need it.
  • zone - Allows to trigger actions based on where a person is located. I don't need it.

How did it work out?

I removed default_config: from my configuration.yaml, then I added the integrations that I think I need. After restarting Home Assistant... everything seemed to work exactly the same. 🙂 Of course some features like the map disappeared but apart from that nothing obvious has changed. What did change is the RAM usage. htop showed lower RAM usage by around 200 MB. I know that this is not an exactly scientific methodology. I simply looked at the used RAM in htop using both configs after Home Assistant has started and the CPU load went down. Still, after couple of hours the usage stayed roughly the same so disabling unnecessary modules is a good strategy to bring down the RAM consumption of Home Assistant.

With this optimization my A20 OLinuXino has been running stable for weeks and I am still able to use all HA functionality that I need. 🙂