How do I turn on a Home Assistant switch entity?

I am banging my head on the wall with this- it should be simple.
Basically, I just want to toggle a switch from an ESPHome device.
Sounds simple, so why is it not working?

Here’s my test configuration:

substitutions:
  device_name: test
  friendly_name: test
  my_ssid: iot

esphome:
  name: ${device_name}
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: ${my_ssid}
  password: !secret wifi_password

sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi Level
    update_interval: 10s

text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP
    ssid:
      name: ${friendly_name} SSID
    mac_address:
      name: ${friendly_name} Mac Address    

ota:
  platform: esphome

web_server:
  port: 80

api:

logger:
  level: DEBUG

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: True
    name: "Button"
    on_press:
      then:
        - homeassistant.service:
            service: script.scene1_toggle_2

The script is in Home Assistant and works when I run it.

I have also tried toggling the switch entity directly:

switch:
  - platform: gpio
    pin: D1
    name: "My Switch"
    on_turn_on:
      then:
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.scene1

Again, in Home Assistant, I can turn on/off the entity in “entities”, but I can’t control it from the ESPHome device.

Any clues what I am doing wrong would be appreciated.

Is your esp device allowed to make service calls?

DOH!

All these years of using ESPHome and I never knew this?
I even looked at that docs page… :flushed:

But thanks, you just saved me hours more of frustration. Now my project can move forward.

1 Like

Dont feel bad, I routinely forget about that too. I know back in the day, you didn’t have to enable service calls. I think this was added sometime last year and it seems like it slid under a lot of people’s radar as well.

What confounded me even more was that I have a currently-working 4-ch relay device that is working. I added this device some three or four years ago, probably before the change.

It probably saves some resources by not enabling it for every device, but it would be nice if the editor or the compile could warn that I have service calls in my code that require the option be turned on.

Ya, i had some older devices still working too. HA service calls aren’t something I use a lot so, it never fails that when I do, I usually forget to enable them for that node.

Im not really sure what the reasoning is for doing it. I always assumed it was for “security” so anyone with a tech savy 16 year old neighbor cant prank you all night by sending malicious service calls but, you could be right too, it may be a resource reason.

Oops- there is such a warning…
warning

It is more to stop bad actors accessing your physical device and re-flashing to open your door

Ya, that makes sense. I could absolutely see some $hit head’s gaining access to an esp node and then causing as much chaos as possible. To be honest if my neighbor ran HA too, it would certainly be tempting to play some pranks.