Can I trigger a HA script from ESPHome?

I have a script in Home Assistant that I would like to execute from ESPHome.

In ESPHome I have a binary sensor which is connected to a button in my device, but when pressed I want to run a script defined in Home Assistant.

If it’s not directly possible, what if I use a helper entity that the physical button on the binary_sensor toggles, and an automation in HA that triggers on the helper entity changing, then that automation would run the script?

Many thanks- works perfectly.

for example…
when i press and hold my dimmer switch for 2sec, it runs a script that turn on or off my ac

binary_sensor:
  - platform: gpio
    pin:
      number: $rotary1_sw
      inverted: true
      mode:
        input: true
        pullup: true 
    name: "Rotary1 Switch"
    device_class: light
    on_click:
      min_length: 2000ms
      max_length: 3000ms
      then:
        - homeassistant.service:
            service: script.office_ac
1 Like

The above example should work fine, however it would help if you posted what you have already, then we might find out where the confusion lies.

Please check THIS post to see how to post code.