All httprequests get "connection refused" error

I’m experimenting with repeated actions and with httprequests, since the system I’m working on would be checking status on another ESPHome device with an httprequest about once a second. So I have, to test this cycle, a 30 second repeating action and a 60 second repeating action. That’s worked fine:

http_request:
  verify_ssl: false
  follow_redirects: false

time:
  - platform: sntp
    on_time:
      - seconds: /30
        then:
          - logger.log: "DEBUG: Logging synced and 30 second repeated action."
      - seconds: /60
        then:
          - logger.log: "DEBUG: Logging synced and 60 second repeated action."

Here’s a screenshot of the logs to show it works fine:

(There are more sections, like the name and friendly name and such, but not, in any way I can see, related to httprequest.)

That showed me that I could do different repeated actions at different intervals. Then I set up a GET request:

http_request:
  verify_ssl: false
  follow_redirects: false

time:
  - platform: sntp
    on_time:
      - seconds: /30
        then:
          - logger.log: "DEBUG: Logging synced and 30 second repeated action."
      - seconds: /60
        then:
          - http_request.get:
              url: https://esphome.io
              headers:
                Content-Type: application/json
              on_response:
                then:
                  - logger.log:
                      format: 'Response status: %d, Duration: %u ms'
                      args:
                        - response->status_code
                        - response->duration_ms

I pulled this http_request.get from the httprequest docs. I’ve also replace the url with the local non-secure url for my other ESPHome device, the one that, in the future, this one will be doing a status check every second. All I change in that is the url.

To read from my local system, I’m using http://durin.local/binary_sensor/cnc_system_master. The system name is durin. The Binary Sensor is named “CNC System Master” in the name field, and this url gives me json output: {"id":"binary_sensor-cnc_system_master","value":false,"state":"OFF"} So the url for the local system works and we know the one for the ESPHome site works.

But here are my logs when I try this with the ESPHome site:


So my requests fail because the connection is refused. I’m guessing the unspecified error flag is due to the refused connection.

While doing this test, sometimes I edit the YAML file, save it and compile and install it on the ESP32, and the system is not consistently reachable, either by the ESPHome add-on in HA, or by the website. It’ll go offline and come back, as if that system’s need to use the network connection for the httprequest is somehow blocking me communicating with it to read the logs. I’ve also seen it reboot multiple times during a session without being able to detect the reason. (Yeah, I goofed - should have had screenshots of those logs!)

What do I need to do so my httprequests are not continually refused by my other ESPHome system or the ESPHome website? Is this part of a bigger issue, or just some simple setting I’m not using?

Instead of blablabla, why don’t you just post what you really have on your code.

Did you try with ip address instead of .local?

I didn’t think of trying the IP address because I could easily get the same page with Chrome with the url and figured if it’s not a problem for Chrome, it shouldn’t be an issue for anything else. So I recompiled it for the IP address instead and it seems to be working - the content length seems about right:

What doesn’t make sense to me is why it would get a “connection refused” error with the name and not the IP address. I don’t see anything in the httprequest docs about DNS, but that seems to be the problem. Maybe the connection is refused because, when using DNS, it’s not pointing getting the right system?

The rest was pretty much the standard and default stuff - I didn’t want to post a long file when it didn’t seem necessary. Here it is:

substitutions:
  name: esp32x
  friendly_name: ESP32X

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: dev

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  # Set up a wifi access point
  ap: {}

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# To have a "next url" for improv serial
web_server:
  version: 3

http_request:
  verify_ssl: false
  follow_redirects: false

time:
  - platform: sntp
    on_time:
      - seconds: /30
        then:
          - logger.log: "DEBUG: Logging synced and 30 second repeated action."
      - seconds: /15
        then:
          - logger.log: "DEBUG: Preparing HTTP request."
          - http_request.get:
              # url: https://esphome.io
              # url: http://durin/binary_sensor/cnc_system_master
              # url: http://durin
              url: http://172.16.4.99
              # headers:
              #   Content-Type: application/json
              # on_response:
              #   then:
              #     - logger.log:
              #         format: 'Response status: %d, Duration: %u ms'
              #         args:
              #           - response->status_code
              #           - response->duration_ms
          - logger.log: "DEBUG: Completed http request"
              # url: http://durin/binary_sensor/cnc_system_master
              # capture_response: true
              # on_response:
              #   then:
              #     - lambda: |-
              #         json::parse_json(body, [](JsonObject root) -> bool {
              #             if (root["vol"]) {
              #                 id(player_volume).publish_state(root["vol"]);
              #                 return true;
              #             }
              #             else {
              #               ESP_LOGD(TAG,"No 'vol' key in this json!");
              #               return false;
              #             }
              #         });
      # - seconds: /60
      #   then:
      #     - http_request.post:
      #         url: http://durin.local/binary_sensor/cnc_system_master

# time:
#   - platform: sntp
#     on_time:
#       - seconds: /60
#         then:
#           - http_request.post:
#               url: http://durin.local/binary_sensor/cnc_system_master

hostname.local never worked reliably for me. I use static ip-addresses on all my esphome-devices and now everything 100% reliable.

I’ve seen that with parts of Home Assistant. I find it frustrating. I use a local DNS because I can remember names and don’t have to remember numbers (which I can’t keep straight). That’s also going to be an issue if I want ESPHome to talk to external websites when the IP addy might change. Are you using addresses only for local systems or are you using them for external websites on the main internet?

I think I’ll do some more testing. I use a fully qualified domain locally. Using “.local” works and using just the system name works, too - except I have to be sure to add “http://” in front of system names. For instance, this one is trying to reach “durin.” If I just type that in Chrome, it automatically starts searching for it. Maybe using my full LAN domain will make a difference.

Just for local.
And because I’m not handy with network settings, so got tired of trying to understand if something depends on my router settings, esphome, operating system, browser etc.

I tested it using my full local domain and it works.

Which leaves a puzzle for me: Why is it I can use just the system name in Chrome, for ssh, and pretty much everything else I can think of, but for HA (mainly the companion app) and ESPHome, it’s a problem? I do think I need to make a separate thread about that - do one in the ESPHome topic and another for HA overall.

I’ve started another thread about this issue - why, for me, I have to use the FQDN in ESPHome, but don’t have to with other programs. At least, for now, it’s taken care of, but odd that I have the issue with systems on my LAN and on the internet.

For me it is very annoying the way chrome is inconsistent with whether it recognizes a string as a hostname or as a search term.

In my case, I get it. Since I name my systems with names from fiction I like, I get that it would look like a searchable term - but Chrome could first see if the name resolves before searching. (I wonder if it’d be worth writing a plugin to do that?)

Yes that’s why dns was invented. Around 1984 :slight_smile:

Yes I was happy when I could stop keeping a list of server addresses by my monitor, but I don’t think that was for 5-6 years later. And it still frustrates me when something like the Home Assistant mobile app or some other apps here and there ask for an IP address of a server on my LAN and won’t accept a system name or even the FQDN for my LAN.

But, on this subject, I’m still stuck with trying to get my ESPHome device to connect to an external website and to reach local systems with only the host name. This sounds like it’s just not listening to the DNS. I can even reach the website for the ESPHome devices by just using “http://“ and the device name on my mobile devices - can’t see why it won’t work on ESPHome, even after I set the domain. (And I’m still wondering if that works the way i suspect and the wording in the docs is wrong, or if it does something else.)

Even if the issue about setting the domain for my LAN is a problem, the ESPHome device should still read an external website, but it doesn’t. And I’m not sure how i can get a closer look at the process to see what’s going wrong with the httprequest function.