Since this isn’t an Arduino programming forum you on might have better luck passing to an Arduino forum where there will be more people with the expertise to help.
The majority of ESP users on this forum use Tasmota or ESP Easy for a job like this. With Tasmota with the HA mode enabled, the MQTT 2.5 M1 binding can even automatically discover the device and create the Thing automatically.