HomeAssistant Memory Problems

Hi guys,
I have a chicken farm where i installed a homeassistant using docker to automate the farm.
Everything was working fine for reading sensor like (temp, relative humidity, CO2, CO, AQI etc) until i added an automation sequence that manages the FANs inside the farm.
That is when my MEMORY USAGE went from 55% to 96.8%.

Automation Yaml code is as follow:

alias: Ventilation - Minimum
description: 5 Minutes Timer
trigger:
  - platform: time_pattern
    minutes: /5
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.basic_control_minimum_ventilation
  - service: timer.start
    data:
      duration: "{{ states('input_number.set_point_minimum_ventilation') }}"
    target:
      entity_id: timer.timer_minimum_ventilation
  - repeat:
      until:
        - condition: state
          entity_id: timer.timer_minimum_ventilation
          state: idle
      sequence: []
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.basic_control_minimum_ventilation
mode: single

I have no experience in coding, i don’t know what’s causing this problem.
Only when i disable this automation everything goes back to normal.

My SoC config is as follow:
Board: Orange PI 3 LTS (2GB Ram)
OS: Ubuntu
Docker Images: Watchtower, HomeAssistant, ESPHome, Glances, MariaDB, Code-Server, Duplicatti
Usually all docker images running with os take about 55% of memory

Log2Ram is also installed with the following settings:

# Size for the ram folder, it defines the size the log folder will reserve into the RAM.
# If it's not enough, log2ram will not be able to use ram. Check you /var/log size folder.
# The default is 40M and is basically enough for a lot of applications.
# You will need to increase it if you have a server and a lot of log for example.
SIZE=50M

# If there are some errors with available RAM space, a system mail will be send
# Change it to false and you will have only a log if there is no place on RAM anymore.
MAIL=false

# Variable for folders to put in RAM. You need to specify the real folder `/path/folder` , the `/path/hdd.folder` will be automatically created. Multiple path can be separeted by `;`. Do>
# example : PATH_DISK="/var/log;/home/test/FolderInRam"
PATH_DISK="/var/log"

# **************** Zram backing conf  *************************************************

# ZL2R Zram Log 2 Ram enables a zram drive when ZL2R=true ZL2R=false is mem only tmpfs
ZL2R=false
# COMP_ALG this is any compression algorithm listed in /proc/crypto
# lz4 is fastest with lightest load but deflate (zlib) and Zstandard (zstd) give far better compression ratios
# lzo is very close to lz4 and may with some binaries have better optimisation
# COMP_ALG=lz4 for speed or Zstd for compression, lzo or zlib if optimisation or availabilty is a problem
COMP_ALG=lz4
# LOG_DISK_SIZE is the uncompressed disk size. Note zram uses about 0.1% of the size of the disk when not in use
# LOG_DISK_SIZE is expected compression ratio of alg chosen multiplied by log SIZE
# lzo/lz4=2.1:1 compression ratio zlib=2.7:1 zstandard=2.9:1
# Really a guestimate of a bit bigger than compression ratio whilst minimising 0.1% mem usage of disk size
LOG_DISK_SIZE=100M

Thank you for your time.

1 Like

Have you tried using a delay instead of repeat until? I tend to favor those in my own automation instead of using timers and repeats and haven’t had any issues with CPU utilization and I have quite a few of them enabled.

Or better yet use a wait template or wait for trigger to wait for the timer to expire.

See: https://www.home-assistant.io/docs/scripts/#wait-for-a-trigger

Or even better write a seperate automation triggered by the timer finishing.

Your repeat loop with no sequence or delay will loop incredibly fast consuming vast amounts of system resources.

Also time pattern triggers are rarely the most efficient way to do things. Can you describe what you are trying to achieve?

This version preserves the functionality of your existing automation without the use of a repeat until.

alias: Ventilation - Minimum
description: 5 Minutes Timer
trigger:
  - id: 'on'
    platform: time_pattern
    minutes: /5
  - id: 'off'
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_minimum_ventilation
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    data: {}
    target:
      entity_id: switch.basic_control_minimum_ventilation
  - condition: trigger
    id: 'on'
  - service: timer.start
    data:
      duration: "{{ states('input_number.set_point_minimum_ventilation') }}"
    target:
      entity_id: timer.timer_minimum_ventilation
mode: single

Be advised that in order for this automation (and yours) to work correctly, the timer’s duration should not meet or exceed the Time Pattern Trigger’s duration (5 minutes).

In other words, the value of input_number.set_point_minimum_ventilation should be less than 300 (seconds).

dear friend, can you point out if delay command is non-blocking function or not ??

It’s non blocking. For lack of a better explanation, the delay creates an internal system “schedule” to resume, whereas the repeat until, as @tom_l pointed out, is going to consume a lot of resources because it is in a constant state of waiting within the code block. This is not a big deal if you are waiting for a light to register that it did, indeed just turn on but it’s much more resource heavy if you are waiting for a long period of time.

thanks for the help, i tried it using 2 methods “wait for a trigger” and “delay”, resources are back to normal and everything seems to be working fine.

thank you all for your time.