Arduino RELAY switches over WiFi, controlled via HASS / MQTT, comes with OTA Support

Hi everyone, the project is fantastic and adapting it worked right away.
I adapted to my purpose by simply trying to automatically return the swich after activating it I modified switch 1 in this way to create a restart

   if (topicStr == "/house/switch1/") 
    {

     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin1, LOW);
       client.publish("/house/switchConfirm1/", "1");
       delay(1000);
       digitalWrite(switchPin1, HIGH);
       client.publish("/house/switchConfirm1/", "0");
       client.publish("/house/switch1/", "0");  //Duplicated for desperate attempt
       Serial.print("ESP RESTART");
       }

later I will replace Serial.print (ā€œESP RESTARTā€); with the ESP.restart () line;
The swich works partly because when the trigger turns on and off correctly, but if I press the reset button or in any case restart the card, after connecting to MQTT it finds a payload at 1 and triggers it.
I discovered this by doing several debugging on the outputs at various stages of the sketch and there I found that immediately after rebooting the board finds a payload 1
After this first click it works correctly, but in my case with a restart it would enter an infinite restart loop.
Why after rebooting it reads a payload at 1 ?? I lost 2 full days with no results
Is there something in what I added or is there an option to clean the payload?
My output serial monitor after reload:

Topic: /house/switch1/
/house/switch1/
49 <----print of payload[0]
1ch4/ <----print of payloadStr created with String payloadStr = String(( char *) payload);

this causes the trigger just started

i there,is it possible with ethernet shield,instead wifi espsss!?

Hello Ady_soft
First of all, excuse my English.
I have the same problem as you on the serial monitor it always appears connecting ā€¦
In addition, the buttons on the HA go crazy, if you turn them on they stay a few seconds in blue and then they turn off by themselves.
So far I couldnā€™t find the error.
could you solve it?
Can you help me?
Thank you and I hope your answer.
Gerardo

Hi , sorry for delay did you solved the problem?

Thanks from everyone from this post, I figured out how to use binary sensor and switch. The hardest part was to make the setup for 2022.7 version.

MQTT borker set with this tutorial: [Home Assistant MQTT Broker Install - YouTube](https://Home Assistant MQTT Broker Install)

on my configuration.yaml

mqtt: !include mqtt.yaml

on my mqtt.yaml

binary_sensor:
  - name: "MQTT button 1"
    state_topic: "/test/button1/"
    payload_on: true
    payload_off: false
    value_template: "{{ value | int > 0 }}
switch:
  - name: "MQTT Switch 1"
    state_topic: "/test/switchConfirm1/"
    command_topic: "/test/switch1/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true

on Arduino

char const* buttonTopic1 = "/test/button1/";
char msg01[20];
  buttonState1 = digitalRead(buttonPin1);
    if (buttonState1 != lastButtonState1)
    {
      snprintf(msg01, 75, "%ld", buttonState1);
      client.publish(buttonTopic1, msg01);
      lastButtonState1 = buttonState1;
    }

I learned in 3 days how to setup the MQTT, using binary sensors and switches just searching and reading. Not all instruction works because of the Hass versions.
I hope this will help beginners, is not a plug and play solution need some knowledge, even me Iā€™m learning every day something new.