Restarting a docker container on a remote machine from Home Assistant

Restarting a docker container on a remote machine from Home Assistant
Photo by Chung Yee Tsang / Unsplash

I have a Raspberry Pi Zero W that runs a docker image of rtl_433, a popular data receiver for decoding data from popular sensors broadcasting on frequencies such as 433.92 Mhz or 915 MHz. For reasons unknown to me, thing stop working. I haven't been able to find out what causes the issue, but I did find that restarting the docker container seems to get things back on track. Setting up an automation that automatically reboots the container is not very elegant, but it works and ended seemingly endless troubleshooting.

The automation runs when Home Assistant does not see data from sensors that normally send an update every minute or so. It runs after 15 minutes of non-activity, and after 30 minutes of inactivity for good measure.

The automation informs my uptime tracking service and status page Uptime Kuma and then uses SSH to restart the docker container on the Pi.

alias: Restart rtl_433 container on rpizw1
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.refrigirator_left_temperature
      - sensor.refrigirator_right_temperature
      - sensor.chest_freezer_temperature
      - sensor.freezer_temperature
    attribute: time
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - platform: state
    entity_id:
      - sensor.refrigirator_left_temperature
      - sensor.refrigirator_right_temperature
      - sensor.chest_freezer_temperature
      - sensor.freezer_temperature
    attribute: time
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition: []
action:
  - service: notify.ntfy
    data:
      title: rtl_433 on rpizw1 in attic
      data:
        priority: 3
        topic: uptime
        tags:
          - red_circle
      message: has not reported data
  - service: shell_command.rtl_433_on_rpizw1_is_down
    data: {}
  - service: shell_command.restart_rtl433_rpizw1
    data: {}
mode: single
shell_command:
  restart_rtl433_rpizw1: ssh -i ./ssh/id_rsa -o 'StrictHostKeyChecking=no' pi@rpizw1 docker restart ecstatic_babbage