xTaskCreatePinnedToCore not working on ESPHome

Hello.

I’m trying to run code assyncronously and, therefore, I would like to create some threads. On the Arduino IDE, I used to do xTaskCreatePinnedToCore on the setup() method.

So, to replicate it, I’ve setup a CustomSensor. If I place normal code in the setup() and update() methods, it works flawlessly. However, if I place a call to xTaskCreatePinnedToCore() or xTaskCreate() on the setup() method (or even on the first loop of the update() method), the entire execution of the code is compromised. The ESP32 responds to pings, but it doesn’t update the Home Assistant sensors (by calling ->publish_state()), nor does it send the output to the console through wifi.

Is it possible to use threads or get code to run assyncronously with ESPHome? I also tried setting up multiple customs sensors, but they run sequentially.

Thanks in advance.

I don’t think so. Here’s what OttoWinter said in 2019:

No, esphome runs on one thread and one thread only (especially because threading on ESP8266 is not supported/flakey). There’s no way to have async delay in lambda code.

That’s why the delay action exists, that one is asynchronous. You can use the if/while/etc actions to get full turing complete code.

1 Like

Alright, it’s a shame because I really like the idea that you can have your ESP32 locked inside a box and program it over the network without opening the box and connecting a USB cable.

It seems like I will not be able to use that feature as I need to run tasks asynchronously.

Thanks.

ESHome is very nice, for a great number of reasons, but you can actually do OTA with Arduino too, if you use their IOT Cloud

I tried it out using their Web Editor and it worked fine.