core icon indicating copy to clipboard operation
core copied to clipboard

REST command fails when used in an automation

Open haarts opened this issue 1 year ago • 3 comments

The problem

I have a small automation in which the weather forecast is send to a tiny e-ink display driven by a Raspberry Pi Zero and some Python code.

For this purpose I made a REST command that takes a forecast and POSTs it to my Pi. This REST command works as expected. The weather report shows on my e-ink display.

However, it fails, when used in an automation and only when the automation is automatically triggered. The entire automation works when I manually run it.

You'll notice that the error (pasted down below) is a timeout. This is patently false. The Pi is up and responsive. No request is received on the Pi. Again, if I trigger the automation manually everything works as expected.

I'm a bit at a loss on how to debug this further.

What version of Home Assistant Core has the issue?

core-2024.9.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Automation

Link to integration documentation on our website

https://www.home-assistant.io/integrations/rest_command/

Diagnostics information

No response

Example YAML snippet

This snippet is from the `configuration.yaml`:


rest_command:
  send_forecast:
    url: "http://192.168.178.191:8000/weather"
    method: POST
    headers:
      Content-Type: application/json
    payload: '{{ morning_forecast | to_json }}'
    timeout: 10


This is that automation:

```yaml
alias: Forecast to eink
description: ""
trigger:
  - platform: time
    at: "06:00:00"
condition: []
action:
  - action: weather.get_forecasts
    metadata: {}
    data:
      type: hourly
    response_variable: morning_forecast
    target:
      entity_id: weather.forecast_home
  - action: rest_command.send_forecast
    metadata: {}
    data:
      morning_forecast: "{{ morning_forecast }}"
mode: single


### Anything in the logs that might be useful for us?

```txt
I have this error in the log:


Logger: homeassistant.components.automation.forecast_to_eink
Source: components/automation/__init__.py:776
integration: Automation (documentation, issues)
First occurred: September 14, 2024 at 06:00:10 (3 occurrences)
Last logged: 06:00:10

Error while executing automation automation.forecast_to_eink: Timeout when calling resource "http://192.168.178.191:8000/weather"


### Additional information

There is one more damning piece of evidence pointing to the REST platform; I've created a binary sensor like so:

```yaml
binary_sensor:
  - platform: rest
    name: Check lights
    device_class: light
    resource: http://19.168.178.191:8000/lights
    scan_interval: 300

This sensor never shows up under entities. There's a log entry that is relevant:

Logger: homeassistant.components.binary_sensor
Source: runner.py:189
integration: Binary sensor (documentation, issues)
First occurred: September 13, 2024 at 16:38:30 (1370 occurrences)
Last logged: 16:46:21

Setup of binary_sensor platform rest is taking over 10 seconds.

Somehow the setup of this sensor times out too! And yes, I've checked the server feeding this sensor. It is up and running.

haarts avatar Sep 16 '24 14:09 haarts

Hey there @jpbede, mind taking a look at this issue as it has been labeled with an integration (rest_command) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of rest_command can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign rest_command Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


rest_command documentation rest_command source (message by IssueLinks)

home-assistant[bot] avatar Sep 16 '24 14:09 home-assistant[bot]

The binary sensor wont work as you're pointing to http://19.168.178.191:8000/lights instead of http://192.168.178.191:8000/lights 🙂 Furthermore, REST has nothing to do with the RESTful command, these are two separate integrations.

Could you please enable the debug log for the RESTful command:

logger:
  logs:
    homeassistant.components.rest_command: debug

jpbede avatar Sep 16 '24 15:09 jpbede

The binary sensor wont work as you're pointing to http://19.168.178.191:8000/lights instead of http://192.168.178.191:8000/lights 🙂

Sweet lord... Accept my humble gratitude. This has been vexing me for hours.

I've added the suggested snippet to the configuration.yaml file. I'll report back in 12 hours once the automation ran.

haarts avatar Sep 16 '24 17:09 haarts

I've updated HA prior to finding the log file, to 2024.9.2 and I think that tossed the logs files. I'll try again tomorrow!

haarts avatar Sep 17 '24 14:09 haarts

Found it still! And now I know what the problem is. My now working lights binary sensor actually gave me the hint! They worked fine right up until midnight. And then again after 6 in the morning. Then I remembered, some time ago I configured my network to go down between those hours...

The fault was entirely mine. Again. Sorry for the noise.

haarts avatar Sep 17 '24 14:09 haarts