Trying to run a script on ESP from HASSIO

Back again for a little help.

I’m trying to run a script on the ESP from HASSIO. There’s hopefully a simple way of doing this as I can’t get it to work.

My idea was to create a (template) switch visible from HASSIO that when I turn it on, it will run the script in ESP. I’ve done that but it won’t run the script.

The script runs fine as I can trigger it from a remote control (IR)

here is the switch

  - platform: template
    name: "Bedroom TV Remote"
    id: bedroomtv
    on_turn_on:
    - script.execute: mainscript

Thanks for the help.

I guess you have to use turn_on_action: instead of on_turn_on:
As is documented, the former means what will happen when some thing (like HA) tries to turn on the switch, and the later is an action that will be executed if the switch turns on.

I’m giving it a go but I’m having wifi issues occasionally.

Strange happenings, the Wifi went wonky when I changed that line … just would not connect. So I changed the switch to this and it works fine.

D4 isn’t connected to anything btw.

  - platform: gpio
    name: "Bedroom TV Remote"
    pin:
      number: D4
    id: bedroomtv
    on_turn_on:
      then:
        - script.execute: mainscript
        - delay: 1s
        - switch.turn_off: bedroomtv

Zombie thread, but having found this whilst trying to do the same, I thought you could have used template instead of faking a gpio, with your last sketch as follows:

  - platform: template
    name: "Bedroom TV Remote"
    id: bedroomtv
    on_turn_on:
      then:
        - script.execute: mainscript
        - delay: 1s
        - switch.turn_off: bedroomtv

Then I rolled one of my own, and found template didn’t seem to process any of the actions correctly, or call the logger outputs I was adding. So I followed your approach and faked a GPIO, and now it works.

Strange…

edit: realised proposal didn’t work. Rolled back on approach entirely and now agree with you :smiley: )

Yet I have no idea why this works.

Lol. Me either. The tinkerer in me says understanding why it works is just as important as whether it works. The pragmatist says, “it works. Move along…”

:slight_smile:

Alternate solution to faking a GPIO: Use an API

On your ESP:

api:
  services: 
    - service: bedroom_tv_remote    
      then:
      - script.execute: mainscript
      - delay: 1s
      - switch.turn_off: bedroomtv

Within Home Assistant, just call

service: esphome.your_device_name_bedroom_tv_remote

I’ve been using this to pass arguments to ESPHome, which is really neat:

ESPHome:

api:
  services: 
    - service: variable_squirt
      variables:
        duration: int
    
      then:
      - logger.log: "STARTING VARIABLE SQUIRT"
      - switch.turn_on: built_in_led
      - switch.turn_on: solenoid
      - delay: !lambda 'return duration;'
      - switch.turn_off: built_in_led
      - switch.turn_off: solenoid
      - logger.log: "VARIABLE SQUIRT ENDS"

Home Assistant:

service: esphome.ha_wemo_variable_squirt
data:
  duration: 10000

The argument is the number of milliseconds I want to delay between turning the solenoid on and off.

2 Likes

This may no longer be important, but I have a template switch with a similar-ish purpose. To get it to work i had to use the optimistic identifier in the switch yaml.

switch:
  platform: template
  name: "Upstairs Night Light Timer"
  id: motion_switch
  optimistic: true

Using the optimistic option meant that when my switch was ‘flicked’ in home assistant it would force the switch to ‘flick’ in the esp. In your case i guess it would force on_turn_on to run?
Hope this helps somehow :smiley: