How to script actions?

I use ESPHome on many of my switches (Sonoff mostly), they are driven by Home Assistant.

OTOH I have one NodeMCU that provides a simple service:

  • it exposes a HTTP endpoint
  • upon receiving a call it
    • sets a GPIO pin
    • waits two seconds
    • unsets the pin

This is coded in “arduino C” and the NodeMCU is flashed with the code. It works fine.

I would like to move this service to ESPHome for a variety of reasons:

  • simpler integration with HA
  • homogeneity in my IoT mess
  • what not :slight_smile:

First question: is it possible in the first place?

If so: what would be the typical approach to configure this?

  • via HA where an automation would send an MQTT (or native API) message which will be understood by ESPHome to “set the pin”, wait in the automation 2s and then send another message to ESPHome that would be understood as “unset the pin”
  • or would the whole service be contained in the ESPHome code (set, wait, unset) - a code that would be launched upon receiving a MQTT/API trigger? (← I would prefer this, I think)
  • or something else?

Both is possible…but what you prefer is easier :stuck_out_tongue:

It this example GPIO_Switch, the switch with the ID “relay” will be available in HomeAssistant as switch.gate_remote

1 Like

After some RTFMing I think there is a straightforward solution: GPIO Switch — ESPHome

1 Like

Ah, we posted at the same moment :slight_smile: Thank you!

What confuses me a bit with ESPHome is the actions on the pins, I think that it will be much easier to let HA do the heavy lifting (via the example we both pointed to).

HA will operated the switch switch.gate_remote:

ESPHome will activate the GPIO pin 25 with ID ‘relay’ defined as a switch,
ESPHome will de-actovite the internal switch with id ‘relay’ after 500 ms (and thus switch the pin 25)
ESPHome will update the status of the switch.gate_remote on HA