Lets talk ESP32 on Home Assistant using ESP Home

I am experiencing small but significantly important issues with an project using an ESP32 and need some advice from the guys who some real understanding of the ESP32’s.

I have read through most of the ESP32 related post on the forum and cant see that someone has done what I am thinking of doing.

Just some background, I have many years of extensive experience in plant automation in the E&I field in a few industries. Worked with multiple PLC brands and spend years on the Honeywell DCS systems. Everything from ladder and function blocks to CL (Control Language) based on Fortran in application modules. In short, all kinds of machine coding.

I find it very difficult to use C, C+ and python. I have this way of thinking, many times in ladder format when i approach home automation and hobby projects. I struggle to find a way to put my thoughts into a program that works. I know exactly what I need and what I want, but cant express it in the languages above. I even struggle to ask the questions on the net, because I dont even know the correct terms used in these languages. Although there is some similarities, the differences are huge. So i am going to ask my questions in simple English so that less experienced hobbyist could also follow us.

In this project I am using an ESP32 Devkit 1 and use ESPhome from within Home Assistant to set it up.

Two issues in the last few days.

1: Lost wifi connection with ESP once and had to reboot ESP.
Question: How do I ensure that the esp reboots once it loses connection to the network. From what I read it does monitor it and will reboot after 15 min or so. What is the best way to ensure that it is set up correctly.

2: On one occasion the Automation in HA called for a Output pin on the Esp to switch on. HA showed that the “Relay” is on but outside in the fishroom the relay was not on. Had to toggle the switch on the dashboard twice to get the relay on. Hasn’t happened again but now I know its possible and want to foolproof this.
Question: How can I receive a “Feedback” from the ESP to see if the output has indeed switched on when requested to, or at least an “State” from the pins used in the program. I can not afford using an input pin for every output pin in the project so it needs to be a message or something from the ESP side that can be monitored on in HA.

3: For critical applications, I feel that it might be better to run the automations on the ESP itself to ensure that it can continue doing what it should even if the WiFi drops out. Running the Automations within HA means nothing if the messages can not reach the ESP. In a critical application the ESp will keep its output on and create a possible dangerous or unwanted situation in the device it is controlling if it solely relies on the HA.
Question: Critical applications in HA or on ESP? IF yes for ESP, who among you are willing to help me set up one sensor so that I can try and understand te flow and then apply that to all my other sensors.

Theoretically I should be able to set up the ESP using an IDE and run it as a “Standalone” unit, only monitoring the entities from HA.

From the other posts it seems like a few guys here truly understand the ESP’s

@justinschoeman , @ebendl , @Sc00bs , @wernerhp , @FXSuper, @_a_a_a (Apologies if i missed another expert, but this is the names that I remember from previous ESP32 posts) . Please if any of you can throw your ideas here. I would even come and sit with any of you at your place and learn a few things if you have the time to teach me a bit more about MCU’s and its coding.

Hi Jaco,

I would try asking ChatGPT as it has given me great answers when stuck with Home Assistant integrations in the past.

Here is the answer I got from your question :smile:

1. Ensuring the ESP32 Reboots After Wi-Fi Loss

The ESP32 in ESPHome should automatically attempt to reconnect to Wi-Fi, but you can implement a more robust solution.

Solution:

  • Wi-Fi Reconnect Configuration in ESPHome: Add the following lines in your ESPHome YAML configuration:

yaml

Copy code

wifi:
  ssid: "YourSSID"
  password: "YourPassword"
  reboot_timeout: 5min
  • reboot_timeout specifies how long the ESP32 will try to reconnect before rebooting. You can set this to a shorter value if 15 minutes is too long.
  • Heartbeat Monitor with Lambda: Use an ESPHome script to monitor connectivity:

yaml

Copy code

sensor:
  - platform: wifi_signal
    name: "WiFi Signal Strength"

interval:
  - interval: 60s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - logger.log: "WiFi is connected"
        else:
          - logger.log: "WiFi disconnected, rebooting..."
          - reboot

2. Verifying Output State in Home Assistant

For critical outputs, verifying the actual state is essential. Unfortunately, GPIO outputs don’t natively provide feedback, but there are workarounds.

Solution:

  • Report State Back to Home Assistant: Use a global variable or template in ESPHome to track the relay’s state and report it to Home Assistant.

yaml

Copy code

globals:
  - id: relay_state
    type: bool
    restore_value: true

switch:
  - platform: gpio
    name: "Relay"
    pin: GPIO5
    on_turn_on:
      - lambda: |-
          id(relay_state) = true;
    on_turn_off:
      - lambda: |-
          id(relay_state) = false;

text_sensor:
  - platform: template
    name: "Relay State"
    lambda: |-
      return id(relay_state) ? "ON" : "OFF";

This setup allows Home Assistant to monitor the actual state via the Relay State sensor.

  • Use Feedback-Enabled Relays: If hardware modifications are possible, you could use relays with built-in state feedback, where the relay’s position can be read back using an input pin.

3. Running Critical Automations on the ESP32

You’re absolutely correct—running critical automations locally on the ESP32 is a safer approach for reliability. Here’s how to set it up:

Solution:

  • Define Automations in ESPHome: Use ESPHome automations for the critical logic:

yaml

Copy code

binary_sensor:
  - platform: gpio
    pin: GPIO14
    name: "Critical Input"
    id: critical_input

switch:
  - platform: gpio
    pin: GPIO5
    name: "Critical Output"
    id: critical_output

automation:
  - alias: "Critical Automation"
    trigger:
      platform: state
      id: critical_input
      to: "ON"
    action:
      - switch.turn_on: critical_output
  - alias: "Critical Automation Off"
    trigger:
      platform: state
      id: critical_input
      to: "OFF"
    action:
      - switch.turn_off: critical_output

This ensures the ESP32 can function independently of Wi-Fi or Home Assistant.


4. Hands-On Assistance with ESPHome Automation

I’d be happy to guide you through setting up one sensor to build your understanding. Here’s a simple example of setting up a temperature sensor with local logic:

yaml

Copy code

sensor:
  - platform: dht
    pin: GPIO4
    temperature:
      name: "Temperature"
      id: temperature_sensor
    update_interval: 30s

automation:
  - alias: "Turn on fan when hot"
    trigger:
      platform: numeric_state
      entity_id: sensor.temperature_sensor
      above: 30.0
    action:
      - switch.turn_on: fan_relay
  - alias: "Turn off fan when cool"
    trigger:
      platform: numeric_state
      entity_id: sensor.temperature_sensor
      below: 25.0
    action:
      - switch.turn_off: fan_relay
1 Like

Firstly, what is it that you want to achieve? And can you achieve it in a way that does not require home assistant to actually do the logic part? So for instance - if you want to control the temperature of a fish tank - you can set the ESP up as a thermostat and do the control on the ESP itself, in case it loses connection to home assistant for whatever reason. (at least you can with tasmota, I’m not that familiar with ESPhome).

As for the WiFi disconnect issue - I’m going to guess that was a wifi signal strength issue, because otherwise it should automatically reconnect when lost. If it’s a critical connection, consider adding another router close to the ESP.

1 Like

I would not call myself an expert here, but I wanted to agree with this part. At least with Tasmota (not ESPHome, but maybe something similar exists?), a lot of the logic can be done on the device itself. So HomeAssistant can still see it, report on it, and control some of it, but the critical stuff is on the device itself. For things like controlling the temperature of a tank, that would be the way to go. You don’t need fish dying every time the Wi-Fi goes down.

I have devices in my own house that disconnects from Wi-Fi at times and needs a reboot to come back. Generally the genuine Sonoff and Shelly devices are quite good at reconnecting, but also not always. Even the ESP32 in the Victron EVSE sometimes (rarely, but sometimes) will just leave the Wi-Fi party. So frankly, I just don’t trust it.

2 Likes

If you are using ESPHome, then Scoobs’ post looks right. ChatGPT does have a nasty habit of making up syntax, and I have never actually used ESPHome, but the solution does look correct.

1 Like

Agree 100% with @plonkster , probably better to do the logic on the ESP32 rather than relying on the Wifi connection to HA.
You could just report back to Home Assistant, using MQTT would probably be the easiest way of doing it.

Also agree with @justinschoeman on ChatGPT, I say it is like have an autistic friend who is a really good coder, you have to be very clear about everything that you want to happen otherwise you can get unexpected results, I am not a coder by any stretch of the imagination and probably just know enough to be able to see if something is going to work or not. I have however had really good success using ChatGPT for writing code for ESPHome and IDE. It is highly unlikely it will get it 100% correct the first time and you will need to do some checking but is worth the effort imo.

1 Like

Hey honey, can you stop at the shops on the way back and buy some milk? And if they have eggs, buy three.

I came home with three bottles of milk.

2 Likes

You said it first … wanted to post earlier today, if it is “mission critical”, forget BT and Wifi, wired only.

1 Like

As most of you know, I don’t have a problem to publicly admit when I stuff up. I have the system incredibly stable for the last 2-3 days. I found the problem. The bridge between the Ground supply and the ground Pin supplying the ESP with power on the development board were badly soldered on one side. This caused a voltage drop to the ESP, not low enough to switch off completely but low enough that relays could not energise and it affected the WiFi as well. I realised that the Led on the board were dim and measured the voltage between 5V and Ground pins and got between 1.7 and 2.2VDC. After resoldering the wire everything is stable.

No excuses, but I need new glasses as well as a proper soldering Iron. Threw away 4 soldering irons last week. Cut them up and broke them into pieces and tossed them in the dustbin. All supposedly good irons bought online from hobby shops, what a load of *@&#.
I dream of a proper weller, but have you seen what they cost. I am now working with the best one I had, its a Gas iron, but it is getting the job done, when I can figure out where I am soldering.

I am still busy setting up another ESP with all the logic on the esp but its taking some time. Will eventually just swop out the two after testing the “Standalone Unit”

2 Likes

Don’t you love the instructions on a shampoo bottle: “Wash. Rinse. Repeat.”

1 Like

Oh I have a similar story. I bought one of these Wyze cameras some time ago. Rather nifty little things, but it’s your usual story, the camera is cheap and they make up for that with the cloud features. And I have endless issues with it rebooting or losing network connectivity, to the point that I contact their helpline.

As it always goes though… the moment you have to verbalise the issue, you also think about all the things you tried, what changed when it started happening, etc… so by the next morning, I realise that the Camera comes with a US-type 120V plug, which means I am using a PSU that didn’t come with it… and the one I grabbed out of a random drawer is an old Sony PSU (when did Sony stop making phones?) that can hardly manage 1 ampere :slight_smile:

I got myself a proper station from RS components. This is the one I wanted (I’ve had really good luck in the past with Ersa), but this is what I ended up settling for. Prices have gone up quite a bit though! I paid less than 3k if memory serves.

I had one of these, or its predecessor at least, while in high school. Really decent, can recommend for hobbyist work, if it is a tad expensive!

yeah, I only got out of the shower once an BottleEmpty exception was raised!

If you are going to spend that much, this one is about 400x better (and seemingly way underpriced at the moment, it should be the same as the rest of the range, ~R5200 excl):

1 Like

I’ve been told that the important part is the ability to get tips for your iron. Which means you should pretty much get the Hakko (beware of knockoffs though!). When I bought that RS thing, it seemed the best in terms of features for price plus the ability to get tips.

Really appreciate it when experienced people recommend good things, thanks!

Thermaltronics was established by people from Metcal that broke away after they were acquired by OKI and the patents expired. So all their tips are compatible with each other. You can find them locally through RS or Test & Rework or otherwise Amazon or Mouser or Digikey. They also last incredibly long if you don’t use them as prying tools.

1 Like

I got myself a basic Magnum from Communica many (many!) years ago. Still going strong, and can still get tips and spares:

[Search Results – Communica South Africa soldering irons](Search Results – Communica South Africa soldering irons)

Was the most affordable semi-decent iron at the time, and still looks well priced.

1 Like

+1 for magnum. Got a Magnum 1220 couple of years ago and very happy with it. One weakness possibly the temp adjuster knobby thing (but you can even buy that part if required - although an allen key works too).

Biggest difference for my soldering results was swapping the “needle” type tip for a chisel tip (they come in smaller sizes but that pic shows the profile/shape best).

If wanting to save a couple of bucks there is this used one (should be about R700 delivered vs the ~R1200 for a nice shiny new one).

Maybe a little different to what you guys use, but I recently got a FNIRSI HS-02 and its great. Mostly like that it doesn’t need to be plugged into the wall.

I would also recommend one of those above a magnum or affordable weller.

@JacoDeJongh you mention you have experience with E&I and plant automation, have you given the Node-Red addon a look in Home assistant?

We love the flow structure, since it is easy to keep track of complicated automations and build them out visually. I believe it has some similarities to Flow based commercial systems.