502 Bad Gateway response to curl shell-command

I have two Home Assistant systems. They are an energy management system at my daughter’s house and a home automation system in my flat. Both are hosted on RPI4 processors. My daughter’s is called ‘Tasty Pi’ and mine is ‘Another Tasty Pi’.

Tasty Pi periodically sends heartbeat messages to Another Tasty Pi through a curl statement in a shell-command action.

Intermittently Tasty Pi receives ‘502 Bad Gateway’ in the response variable (response.stdout). The logs seem to show that after the failed transmission the execution of the shell-command action is automatically repeated successfully. However, Another Tasty Pi’s logs show no message was received. Tasty Pi will repeat the shell command up to 5 times. So far, 4 iterations have been sufficient.

Is there anything in what I’m doing that is causing the ‘502 Bad Gateway’ response?

Is the shell-command action really repeated automatically but to no effect?

alias: heartbeat tx
description: ""
triggers:
  - trigger: time_pattern
    minutes: /5
conditions: []
actions:
  - repeat:
      sequence:
        - action: shell_command.heartbeat
          metadata: {}
          data:
            repeats: "{{repeat.index}}"
          response_variable: response
        - action: logbook.log
          metadata: {}
          data:
            entity_id: automation.heartbeat_tx
            name: heartbeat response stdout
            message: >-
              ... "{{ response.stdout }}" ... Count -
              "{{response.stdout|count}}" ...
        - action: logbook.log
          metadata: {}
          data:
            entity_id: automation.heartbeat_tx
            name: heartbeat response stderr
            message: ... "{{ response.stderr }}" ...
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
      until:
        - condition: template
          value_template: |-
            {{ (response.stdout|count == 0
                and
                response.returncode == 0)
                or
                repeat.index >= 5 }}
trace:
  stored_traces: 20
mode: single
shell_command:
  heartbeat: >
    curl -X PUT -H "Content-Type: application/json" -d '{ "sent": "{{ now().strftime('%Y-%m-%d %H:%M:%S.%f') }}", "attempts": "{{repeats}}" }' https://hooks.nabu.casa/gAAAAABn5...EnHLAXYOJ5JU=
2025-03-30 19:00:00.496 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Running automation actions
2025-03-30 19:00:00.505 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Repeating sequence: Iteration 1
2025-03-30 19:00:00.506 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Repeat at step 1: Running automation actions
2025-03-30 19:00:00.507 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Repeat at step 1: Executing step call service
2025-03-30 19:00:00.689 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `curl -X PUT -H "Content-Type: application/json" -d '{ "sent": "{{ now().strftime('%Y-%m-%d %H:%M:%S.%f') }}", "attempts": "{{repeats}}" }' https://hooks.nabu.casa/gAAAAABn5...EnHLAXYOJ5JU=
`, return code: 0:
b'<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body>\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.21.6</center>\r\n</body>\r\n</html>\r\n'
2025-03-30 19:00:00.690 DEBUG (MainThread) [homeassistant.components.shell_command] Stderr of command: `curl -X PUT -H "Content-Type: application/json" -d '{ "sent": "{{ now().strftime('%Y-%m-%d %H:%M:%S.%f') }}", "attempts": "{{repeats}}" }' https://hooks.nabu.casa/gAAAAABn5...EnHLAXYOJ5JU=
`, return code: 0:
b'  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   214  100   157  100    57   1040    377 --:--:-- --:--:-- --:--:--  1426\n'
2025-03-30 19:00:00.690 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Repeat at step 1: Executing step call service
2025-03-30 19:00:00.691 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Repeat at step 1: Executing step call service
2025-03-30 19:00:00.692 INFO (MainThread) [homeassistant.components.automation.heartbeat_tx] heartbeat tx: Repeat at step 1: Executing step delay 0:00:00.500000