Http_request taking way too long

Hi, I’m having troubles with http_request. At first it did not work at all, giving an error on the logs, after some fiddling around, I got it working by setting the dns in the wifi settings to 8.8.8.8, but it now takes a long time (no idea how to know if it’s related to the dns, also don’t know if there is a dns cache).

Here the relevant parts of the yaml file:

esp8266:
  board: nodemcuv2
  
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Optional manual IP
  manual_ip:
    static_ip: 192.168.1.204
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 8.8.8.8
    dns2: 1.1.1.1

http_request:
  verify_ssl: false

binary_sensor:
  - platform: gpio
    pin:
      number: D5
      inverted: true
      mode:
        input: true
        pullup: true
    id: botonTimbre
    name: "Boton timbre"
    filters:
      - delayed_on: 10ms
      - delayed_off: 20ms
    on_press:
      then:
        - if:
            condition:
              switch.is_on: timbreEnabled
            then:
              - switch.turn_on: relayTimbre
              - delay: 250ms
              - switch.turn_off: relayTimbre
        - http_request.post:
           url: "https://api.telegram.org/bot<Token>/sendMessage"
           headers:
             Content-Type: application/json
           json:
            chat_id: <MyChatID>
            text: "Riiiiing"
           on_response:
            then:
              - logger.log:
                  format: 'Response status: %d, Duration: %u ms'
                  args:
                    - response->status_code
                    - response->duration_ms
    on_release:
    - switch.turn_off: relayTimbre

Here the relevant logs:

[08:11:35][D][binary_sensor:036]: 'Boton timbre': Sending state ON
[08:11:35][D][switch:012]: 'Relay de Timbre' Turning ON.
[08:11:35][D][switch:055]: 'Relay de Timbre': Sending state ON
[08:11:35][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:35][D][switch:055]: 'Relay de Timbre': Sending state OFF
[08:11:37][D][http_request.arduino:125]: Content-Length: 259
[08:11:37][D][main:620]: Response status: 200, Duration: 2062 ms
[08:11:37][W][component:239]: Component binary_sensor took a long time for an operation (2083 ms).
[08:11:37][W][component:240]: Components should block for at most 30 ms.
[08:11:37][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:37][D][binary_sensor:036]: 'Boton timbre': Sending state OFF
[08:11:37][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:41][D][binary_sensor:036]: 'Boton timbre': Sending state ON
[08:11:41][D][switch:012]: 'Relay de Timbre' Turning ON.
[08:11:41][D][switch:055]: 'Relay de Timbre': Sending state ON
[08:11:41][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:41][D][switch:055]: 'Relay de Timbre': Sending state OFF
[08:11:43][D][http_request.arduino:125]: Content-Length: 259
[08:11:43][D][main:620]: Response status: 200, Duration: 1912 ms
[08:11:43][W][component:239]: Component binary_sensor took a long time for an operation (1929 ms).
[08:11:43][W][component:240]: Components should block for at most 30 ms.
[08:11:43][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:43][D][binary_sensor:036]: 'Boton timbre': Sending state OFF
[08:11:43][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:46][D][binary_sensor:036]: 'Boton timbre': Sending state ON
[08:11:46][D][switch:012]: 'Relay de Timbre' Turning ON.
[08:11:46][D][switch:055]: 'Relay de Timbre': Sending state ON
[08:11:46][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:46][D][switch:055]: 'Relay de Timbre': Sending state OFF
[08:11:49][D][http_request.arduino:125]: Content-Length: 259
[08:11:49][D][main:620]: Response status: 200, Duration: 2962 ms
[08:11:49][W][component:239]: Component binary_sensor took a long time for an operation (2973 ms).
[08:11:49][W][component:240]: Components should block for at most 30 ms.
[08:11:49][D][switch:016]: 'Relay de Timbre' Turning OFF.
[08:11:51][D][binary_sensor:036]: 'Boton timbre': Sending state OFF
[08:11:51][D][switch:016]: 'Relay de Timbre' Turning OFF.

Thanks in advance to anyone more knowledgable that can help figuring this out

The log says it takes 2 to 3 seconds for the response to come and that matches the time stamps in the log. That all appears to be time the thing you are calling is taking. Not seeing an issue.

Wait… You can send Telegram messages with the http integration?? Thats flipping cool! Do you know of any other services that messages can be sent to like this? What about email or how about using the email to sms messaging like where you can send a text message to a phone using your email…

I have seen it take up to 10 seconds… Anything over a second blocking the esp is very bad in my opinion, as I can miss other things happening in other pins.

Also, the telegram api is usually very fast… If I time the same request from my PC it takes ~500ms. I also made an automation on HASS to send a telegram message when the relay turns off, and that message gets way before the one sended from the ESP.

I did the automation this way to avoid giving the 250ms advantage to HASS, but it should be when the button is pressed. To give a little more context, “Timbre” stands for “Ring bell” D5 is wired to the button in my front door, and there is a relay (called relayTimbre in the yaml) that makes the bell sound. Originally, I wanted to send the message from the ESP when the button is pressed, but then the bell would ring for the whole time it takes the ESP to send the message, which is to much for a bell in my opinion.

Here some logs with worse timing:

[11:04:43][D][binary_sensor:036]: 'Boton timbre': Sending state ON
[11:04:43][D][switch:012]: 'Relay de Timbre' Turning ON.
[11:04:43][D][switch:055]: 'Relay de Timbre': Sending state ON
[11:04:43][D][switch:016]: 'Relay de Timbre' Turning OFF.
[11:04:43][D][switch:055]: 'Relay de Timbre': Sending state OFF
[11:04:52][D][http_request.arduino:125]: Content-Length: 259
[11:04:52][D][main:620]: Response status: 200, Duration: 8722 ms
[11:04:52][W][component:239]: Component binary_sensor took a long time for an operation (8738 ms).
[11:04:52][W][component:240]: Components should block for at most 30 ms.
[11:04:52][D][switch:016]: 'Relay de Timbre' Turning OFF.
[11:04:52][D][binary_sensor:036]: 'Boton timbre': Sending state OFF
[11:04:52][D][switch:016]: 'Relay de Timbre' Turning OFF.
[11:05:47][D][binary_sensor:036]: 'Boton timbre': Sending state ON
[11:05:47][D][switch:012]: 'Relay de Timbre' Turning ON.
[11:05:47][D][switch:055]: 'Relay de Timbre': Sending state ON
[11:05:47][D][switch:016]: 'Relay de Timbre' Turning OFF.
[11:05:47][D][switch:055]: 'Relay de Timbre': Sending state OFF
[11:05:52][D][http_request.arduino:125]: Content-Length: 259
[11:05:52][D][main:620]: Response status: 200, Duration: 4963 ms
[11:05:52][W][component:239]: Component binary_sensor took a long time for an operation (4979 ms).
[11:05:52][W][component:240]: Components should block for at most 30 ms.
[11:05:52][D][switch:016]: 'Relay de Timbre' Turning OFF.
[11:05:53][D][binary_sensor:036]: 'Boton timbre': Sending state OFF
[11:05:53][D][switch:016]: 'Relay de Timbre' Turning OFF.

Of course a few seconds delay in the message is not a big deal, it’s probably negligible compared to the time it takes me to take the phone out of my pocket. The thing that concerns me the most is the fact that the whole MCU is completely frozen on that time, thus missing any other input to the device (I also have two PIR sensors and another relay which turns the light in the stairs inside my house on).

Maybe this cheap ESP8266 is just that slow, if that is the case I’d simply put a new one just for the ring bell thing. It does miss a second ring bell if you press it again fast, but also I do not care too much if someone pressed it once or twice.

Well, in theory, you can do anything that could be done with an http request. So yes, the message does arrive, in my case it just takes too long to the the http request, and thus the whole MCU is frozen while that happens.

Give dual core Esp32 a try. I don’t know how much Esphome uses second core, but common approach is that wifi is running on different core from main thread.

Yes, I will try that at some point. But right now I only have a few of these, so I’ll just split the functionality over 2 of these 8266 that I already have instead of ordering some new ones.

I agree with that.