How do I run an ESPHome script from Home Assistant?

Yes, the esp device is allowed to make service calls.
In “Entities”, the script does not appear, nor when trying to build an automation.

Here is the script in ESPHome:

  - id: toggle_play
    then:
      - switch.turn_on: ${device_name}_start
      - delay: 10ms
      - switch.turn_off: ${device_name}_start

I am sure I have overlooked something simple, but nothing comes to mind.

Here is the switch that the script is supposed to control:

switch:
  - platform: gpio
    name: ${device_name} start
    id: ${device_name}_start
    pin: GPIO12

Any help would be appreciated.

I usually create a button to run the script.

HA:

action: button.press
target:
  entity_id: button.abrir_garagem
data: {}

1 Like

By referring to the documentation.

Using a button, switch, select, etc… like mentioned already, they work just as well and are sometimes the better option to use.

1 Like

I can run the ESPHome script from a button in ESPHome. But that was not the question.

I want to run the script from an automation in Home Assistant.

The Native API docs go the other way, running a Home Assistant action from ESPHome, but I need to go the other way.

Never mind- I found the missing piece, but thanks for the feedback.

api:
  services:
    - service: toggle_play
      variables: {}
      then:
        - script.execute: toggle_play

This exposes the sensor to Home Assistant as

esphome.sound_sensor_toggle_play
1 Like

I believe you didn’t understand my answer.
After creating the button to run the script in esphome, you call the press button action in your automation in home assistant.

But I’m glad you found another way.

Thanks again.
I tried this but the button on the ESPHome configuration isn’t anywhere to be found.