#How to trigger a script in ESPHome device FROM HomeAssistant

1 messages · Page 1 of 1 (latest)

molten heron
#

I am finding a bunch of esxamples for the other way around *ESPHome sensor triggers event withint HomeAssistant". But I need the inverse. If something happens within HomeAssistant I want run a script defined in my ESPHome device configuraton: I thought that exposing the script via the api: section would make the scripts available as an entity, something like esphome.<my_service_id> but that does not seem to work.

Here's my config:

substitutions:
  name: <friendly-name>
script:
  - id: error_tone_garage_rear
    then:
      - output.turn_on: buzzer_out
      - delay: 60ms
      - output.turn_off: buzzer_out
      - delay: 50ms
      - output.turn_on: buzzer_out
      - delay: 60ms
      - output.turn_off: buzzer_out
      - delay: 50ms
      - output.turn_on: buzzer_out
      - delay: 60ms
      - output.turn_off: buzzer_out

...

api:
  encryption:
    key: <key>

  services:
    - service: toggle_error
      variables: {}
      then:
        - script.execute: error_tone_garage_rear

Then I looked for esphome.toggle_error or <friendly-name>.toggle_error or just toggle_error but cannot find anything.

I know I must be doing something wrong but I cannot find a good example of how to do this.

Thanks!

drowsy marsh
#

make a dummy button in esphome that triggers the script when its pressed. then you can press the button in home assistant

crude cloud
#

The rename to actions might be throwing off the search

#

When you declare actions in your ESPHome YAML file, they will automatically show up in Home Assistant and you can call them directly.

drowsy marsh
#

look at this guy making good suggestions 😛 instead of my random hacks I did once and never learnt the right way of doing it...

crude cloud
#

Button would work too but it can't pass variables as easily I guess

drowsy marsh
#

yeah, depends on use case entirily

crude cloud
#

there's also register_service() if you drop down into C code.

molten heron
# crude cloud This maybe? https://esphome.io/components/api.html#user-defined-actions

Hadn't seen actions before. Thanks!

I get an error in the ESPHome builder editor - "two or more values in the same group of exclusion 'actions'"

with this:

api:
  actions: 
    - action: error_sound_front_door
      then:
      - output.turn_on: buzzer_out_1
      - delay: 60ms
      - output.turn_off: buzzer_out_1
      - delay: 50ms
      - output.turn_on: buzzer_out_1
      - delay: 60ms
      - output.turn_off: buzzer_out_1
      - delay: 50ms
      - output.turn_on: buzzer_out_!
      - delay: 60ms
      - output.turn_off: buzzer_out_1

and this...

api:
  actions: 
    - action: error_sound_front_door
      then:
        - script.execute: error_tone_front_door
#

Okay, got it. Had to remove everything but the encryption key in the api section. Its compiling now.