Memory leak when using Google Calendar and location in an automation

Hi
Relatively new to HA - about 3 weeks in.

HA Core - v2022.4.6
Synology NAS Docker install : Low-pri CPU + 2048Gb RAM assigned
Nothing too much integrated yet. Still feeing it out before I commit to a Pi4 build.

I had an issue recently during a late night session where my docker container was crashing due to memory full and then auto-restarting.
Initially I thought the issue was due to a typo in the raw config editor (as I shared here), but the next day I had a load of emails waiting for me from my NAS telling me that HA had been crashing every 30 minutes overnight.

Last night it started doing the same. Emails and I could see it fill up the RAM within 15 mins, slowly become unresponsive and then crash at about the 30minute mark. “Why is it only doing this at night?” … and then I realised what it may be.

The pattern seemed to follow an automation I have that triggers every /10 to re-activate my phone’s charger when the battery level dips below 93% but only when my calendar is set to “sleeping”.

As soon as I de-activated that Automation and reset my HA that it stopped. CPU load is back down and RAM back to reasonable numbers.

The only thing different in this automation compared to the others I have for my phone charging on/off is the use of the Google Calendar in this one automation as well as (“is_home”).
It’s weird 'cos this was one of the first automations I’d set up about 10 days in (preventing battery overcharging as my motivation to start HA)

The calendar itself seems to be fine. I followed SlackerLabs YT guide on integrating it into my HA and it updates quickly and the card on my homepage works fine too. I don’t use search or offset features in the google_calendars.yaml as I don’t do all day events there. Just as a trigger for events for tasker to automate different profiles on my phone.

Some screenshots below for reference

No-one likes to see this in the morning :frowning:
Home_Ass_overnight_memory_munching_0

Next night and it’s happening again. RAM full for 10 minutes and counting…

After disabling the automaton and it’s still happy No emails:

Here’s the automation (edited to remove phone ID and real calendar name)

`alias: MyPhone Sleeping Recharge Top Up
description: ''
trigger:
  - platform: time_pattern
    minutes: /10
condition:
  - condition: state
    entity_id: calendar.schedulingcal
    state: 'on'
  - condition: state
    entity_id: calendar.schedulingcal
    state: Sleeping
    attribute: message
  - type: is_battery_level
    condition: device
    device_id: 12345678901234567890123456789012
    entity_id: sensor.myphone_battery_level
    domain: sensor
    below: 93
  - condition: device
    device_id: 12345678901234567890123456789012
    domain: device_tracker
    entity_id: device_tracker.myphone
    type: is_home
action:
  - service: script.myphone_master_recharge_on
    data: {}
mode: single`

Looking at it now and I’m wondering if it’s because there’s no “and” in the conditions.
afaik this is the default logic for conditions that all items need to be true before the action can take place?

Conditions are optional and will prevent the automation from running unless all conditions are satisfied.

I’ll try this and see if it behaves:

alias: MyPhone Sleeping Recharge Top Up
description: ''
trigger:
  - platform: time_pattern
    minutes: /10
condition:
  - condition: state
    entity_id: calendar.schedulingcal
    state: 'on'
  - condition: and
    conditions:
      - condition: state
        entity_id: calendar.schedulingcal
        attribute: message
        state: Sleeping
      - condition: and
        conditions:
          - type: is_battery_level
            condition: device
            device_id: 12345678901234567890123456789012
            entity_id: sensor.myphone_battery_level
            domain: sensor
            below: 93
          - condition: and
            conditions:
              - condition: device
                device_id: 12345678901234567890123456789012
                domain: device_tracker
                entity_id: device_tracker.myphone
                type: is_home
action:
  - service: script.myphone_master_recharge_on
    data: {}
mode: single

Update: Nope… tried that and it’s back to 100% RAM use :frowning:
Was worth a try.

I highly doubt this has anything to do with this automation. What does your script do?

It checks to see if

Phone battery is <93%
Calendar state: on
Calendar message: Sleeping
Phone location: is_home

Action: trigger a script that switches on USB and Chi charger sockets

Right but an automation that runs with a 10 minute time pattern will only execute every 10 minutes. You would see a spike every 10 minutes, not a steady climb. This is why the issue points to your script. If it’s just a basic on/off, it could be that integration that’s turning on/off the USB that’s causing the memory leak.

In regards to your yaml, your trigger is incorrect. Time patterns need to be in quotes. It’s possible that this incorrect configuration is also leading to the problem.

  - platform: time_pattern
    minutes: "/10"

Hi
HA doesn’t seem to allow quotes around the time interval.
I created a new test and this was how it was configured in the raw YAML

alias: _Test
description: ''
trigger:
  - platform: time_pattern
    minutes: /10
condition: []
action:
  - service: script.1234567891012
    data: {}
mode: single

If I try to edit it in the visual editor to be “/10” then I get an error
I also tried single quotes, but got the same error message. Same also for /“10”:

Message malformed: invalid time_pattern value for dictionary value @ data[‘minutes’]

I went to the raw YAML editor and it let me save it that way, but when I transferred back to the visual editor it had stripped the quotes from the code back to: /10

Then the UI must accept the value as is. Which means the memory leak is in the method of turning on and off your USB. Your automation isn’t anything different from many automations on this forum. The only difference is whatever resides in that script you’re calling.

Thanks for the suggestions on this. It is appreciated.

I have 2 automations that call the same “all chargers on” script.
One which triggers the script when the pone dips below 30% and this one which triggers the script when below 93%, but if there’s a calendar entry set to “Sleeping”

The “all chargers on” script calls sub-scripts to activate those devices.
I did it that way in case any of the charging devices change, then I only have to update the one scripted action without having to change all associated scripts or automations.

Now that you’ve said it, I’m wondering if it’s a problem with the script in a script. Or maybe because one of the devices is unplugged/unavailable (problematic Sonoff USB charger).
And the only reason I see it overnight is 'cos I don’t use my phone enough to trigger the < 30% automation.

I’ll change the daily automation to trigger it now to see if you were right.

If you have recursive scripts (a script that calls another script that calls the original script), you will get a memory leak.

Home assistant guards against this if you are on the latest version.

Yes. You’re a genius …

I set the normal one to trigger and it started to consume RAM.
As soon as I started charging the phone it halted. I took the phone off the charger and it started to skyrocket again.

Digging into the subscripts and found the problem.

On one of the charging devices, my first smart plug (sonoff USB) I’d set to start and for some reason I had it set to repeat until the phone was detected as charging.
I’m not sure why I’d done it this way as every other device I’ve configured to switch on only.

I removed the repeat, retested and no more memory consumption.

Thank you.
Getting this solved has been a huge help.

For reference, this was the script with the bad logic:

alias: Mobile Recharge Chi ON
sequence:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_0123456789_1
  - repeat:
      until:
        - type: is_plugged_in
          condition: device
          device_id: 0123456789012345678901234567
          entity_id: binary_sensor.myphone_is_charging
          domain: binary_sensor
      sequence: []
mode: single
icon: mdi:usb