Memory usage ramping up and then crashing

I have noticed this behaviour in Home Assistant

image

At 7pm the memory usage started to grow very quickly and it crashed when it reached 100%; after restarting it, it started growing again

I have a Prism EV charger and 7pm is the time I started charging the vehicle, I have a script that controls the current to limit the maximum power withdrawn from the grid (script in the next post)

I found the memory use grows only when this script is in execution

alias: Control
sequence:
  - repeat:
      while:
        - condition: state
          entity_id: input_boolean.toggle
          state: 'on'
      sequence:
        - choose:
            - conditions:
                - condition: or
                  conditions:
                    - condition: template
                      value_template: >-
                        {{states("sensor.prism_tensione_attuale") | float <
                        states("input_number.Vmin") | float}}
                    - condition: template
                      value_template: >-
                        {{states("sensor.prism_power_grid") | float >
                        states("input_number.W_max") | float}}
                    - condition: template
                      value_template: >-
                        {{states("sensor.prism_corrente") | float/1000+2 <
                        states("input_number.amp_limit") | float}}
              sequence:
                - service: input_number.decrement
                  target:
                    entity_id: input_number.amp_limit
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 1
                    milliseconds: 0
            - conditions:
                - condition: template
                  value_template: >-
                    {{states("sensor.prism_tensione_attuale") | float >
                    states("input_number.Vmax") | float}}
                - condition: template
                  value_template: >-
                    {{states("sensor.prism_corrente") | float/1000+2 >
                    states("input_number.amp_limit") | float}}
                - condition: template
                  value_template: >-
                    {{states("sensor.prism_power_grid") | float <
                    states("input_number.W_max") | float}}
              sequence:
                - service: input_number.increment
                  target:
                    entity_id: input_number.amp_limit
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 1
                    milliseconds: 0
          default: []
mode: single
``

Endless loop is a bad idea, imo.

This should be executed from an automation, with your various sensors as triggers and a condition on your toggle.

I used to do that, but I thought the script was more elegant and efficient
For example with the script I can check every 1s if I have do increase or decrease the current; by using automations I have to setup two separate automations that each check a condition every 1s

You don’t have to “check every 1s” if you trigger on a sensor change.
The automation will be executed each and every time the state of any of your sensors is changed.

Nothing prevents you to put this in a single automation, though. Basically, the “sequence” part can stay the same. Best is to set the mode to “queued”, though, or you might miss events if the automation is already running.

EDIT: Unless you specifically wants it to run every 1sec? Then, use a “Time pattern” trigger.

yes, I want to trigger it every 1s because I need to control the current until the power and the voltage are within a range, not just when they change

I don’t understand why a script with a while loop would be a problem…

You’re not doing a “simple” while loop. You’re basically doing an endless loop (until you switch it off).

By essence, HA is asynchronous, and you force it to a synchronous job. Not sure why memory explodes, then, but you can assume it is because memory is allocated at each loop, and you don’t give HA a chance to do garbage collection.

Would a time pattern trigger every 1s be better than the endless loop?

Meh. Triggering on state changes or events would be better than an endless loop or time pattern trigger.

Though I suspect you might fall afoul of the lack of a “button release” event in HA for your intended application. You can vote here for its inclusion here.

So I created a new automation that is triggered by power numeric value change (as often as every 1s)

I created this automation 3 days ago, but I haven’t “used it” (not charged the EV), so it’s triggered, but no action is executed
My HA crashed last night after running smoothly for weeks, so I can only assume it has something to do with this automation (which I have now made asynchronous)

However, memory usage wasn’t ramping up before crashing this time
image

- id: '1628239344577'
  alias: Prism Control
  description: ''
  trigger:
  - platform: mqtt
    topic: prism/energy_data/power_grid
  condition: []
  action:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - condition: template
          value_template: '{{states("sensor.prism_tensione_attuale") | float < states("input_number.Vmin")
            | float}}'
        - condition: template
          value_template: '{{states("sensor.prism_power_grid") | float > states("input_number.W_max")
            | float}}'
      - condition: template
        value_template: '{{states("input_number.amp_limit") | float >6 }}'
      sequence:
      - service: input_number.decrement
        target:
          entity_id: input_number.amp_limit
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
    - conditions:
      - condition: or
        conditions:
        - condition: template
          value_template: '{{states("sensor.prism_tensione_attuale") | float > states("input_number.Vmax")
            | float}}'
        - condition: template
          value_template: '{{states("sensor.prism_power_grid") | float < states("input_number.W_min")
            | float}}'
      - condition: template
        value_template: '{{states("input_number.amp_limit") | float <32 }}'
      sequence:
      - service: input_number.increment
        target:
          entity_id: input_number.amp_limit
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
    default: []
  mode: single