Need help with triggers and conditions

I’m having an issue with an automation…

- id: 'dockercontainers'
  alias: Restart Docker Containers when memory > 50%
  trigger:
  - platform: numeric_state
    entity_id: sensor.memory_use_percent
    above: 50
    for:
      hours: 2
      minutes: 0
      seconds: 0    
  condition:
    - condition: template
      value_template: "{{ now().strftime('%H:%M') != '0:00' and now().strftime('%H:%M') != '6:00' and now().strftime('%H:%M') != '12:00' and now().strftime('%H:%M') != '18:00' and (states('sensor.sonarr_queue') == '0') }}"
  action:
  - service: switch.turn_off
    entity_id: switch.ha_dockermon_radarr, switch.ha_dockermon_sonarr, switch.ha_dockermon_sabnzbd
  - delay: '00:01:00'
  - service: switch.turn_on
    entity_id: switch.ha_dockermon_radarr, switch.ha_dockermon_sonarr, switch.ha_dockermon_sabnzbd

So I have a shell scrpt (running every 6 hours as a cronjob) that cleans up docker and deletes unused images.
In HA with the above automation, I make it so if a container is out of control memory wise for 2 hours it stops and then restarts those containers.
So the inevitable happened and it stopped those containers and coincidentally, the shell script ran and deleted the images/containers so they never were restarted.
So I added the above condition so that it won’t run at 0,6,12 or 18 or if sonarr has something in the queue which means something is downloading/processing. So far so good.
BUT how can I make it trigger again as the original trigger won’t run it again.

Maybe like this:

- id: 'dockercontainers'
  alias: Restart Docker Containers when memory > 50%
  trigger:
  - platform: numeric_state
    entity_id: sensor.memory_use_percent
    above: 50
    for:
      hours: 2
  - platform: state
    entity_id: input_boolean.retrigger
    to: 'on'
    for:
      minutes: 5
  action:
  - service: input_boolean.turn_off # required for 2nd and subsequent re-triggers.
    entity_id: input_boolean.retrigger
  - delay: '00:00:01' # probably not required, just being safe.
  - service: input_boolean.turn_on
    entity_id: input_boolean.retrigger
  - condition: template
    value_template: "{{ now().strftime('%H:%M') != '0:00' and now().strftime('%H:%M') != '6:00' and now().strftime('%H:%M') != '12:00' and now().strftime('%H:%M') != '18:00' and (states('sensor.sonarr_queue') == '0') }}"
  - service: input_boolean.turn_off
    entity_id: input_boolean.retrigger
  - service: switch.turn_off
    entity_id: switch.ha_dockermon_radarr, switch.ha_dockermon_sonarr, switch.ha_dockermon_sabnzbd
  - delay: '00:01:00'
  - service: switch.turn_on
    entity_id: switch.ha_dockermon_radarr, switch.ha_dockermon_sonarr, switch.ha_dockermon_sabnzbd

this should trigger once on your memory use then keep re-triggering every 5 minutes until the condition in the action block is true.

1 Like

And I need to create the input_boolean as well of course. Thanks Tom. I’ll have a play with that tomorrow.

Yep, figured a clever bloke like you would know that.

1 Like

ok so you get the cigar but I did change the condition.

- id: 'dockercontainers'
  alias: Restart Docker Containers when memory > 35%
  trigger:
  - platform: numeric_state
    entity_id: sensor.memory_use_percent
    above: 35
    for:
      hours: 2
  - platform: state
    entity_id: input_boolean.retrigger
    to: 'on'
    for:
      minutes: 10
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.retrigger
  - delay: '00:00:01'
  - service: input_boolean.turn_on
    entity_id: input_boolean.retrigger
  - condition: template
    value_template: "{{ now().strftime('%M') < '58' and now().strftime('%M') > '02' and (states('sensor.sonarr_queue') == '0') }}"
  - service: input_boolean.turn_off
    entity_id: input_boolean.retrigger
  - service: switch.turn_off
    entity_id: switch.ha_dockermon_radarr, switch.ha_dockermon_sonarr, switch.ha_dockermon_sabnzbd
  - delay: '00:01:00'
  - service: switch.turn_on
    entity_id: switch.ha_dockermon_radarr, switch.ha_dockermon_sonarr, switch.ha_dockermon_sabnzbd

This way it will never fire between x:58 and x:02 With the original, as an edge case it could have fired the clean script between x:00 and x:01 when the containers could be down. Now, if the clean is running, (only a few seconds) every 6 hours on the hour it can’t fire.

I also found that the latest VScode addon update has dropped the memory usage way back so I am using 35% again… last VScode must have been a bit rogue with memory usage even when not using the addon.

1 Like

Hey @DavidFW1960

Can you please share you configuration for your docker containers that you have made them as a switch in HA? I am trying to do the same for my docker containers and restart them when any problem arises.

Thanks.

Yeah I can but I’m away at the moment so it will be a few days. I’m pretty much just using hadockermon Phil Hawthorne docker image and the endpoints it provides

1 Like

Doing mathematical comparisons of strings can have unexpected consequences. For example, your condition would fail between 6 and 9 minutes past any hour because, as a string, ‘6’, ‘7’, ‘8’, and ‘9’ are greater than ‘58’.

Instead use:

  - condition: template
    value_template: "{{ (2 < now().minute < 58) and (states('sensor.sonarr_queue') == '0') }}"

Yeah will wait once you are back. I would like to see your configuration etc. Thanks

Sure. You can also check my GitHub which has my full configuration in it.

Sorry but what is your github?

1 Like