Changing state of HA relay

I have relay in HA, and i want switch it from esphome.
Trying this, but nothing changes.

binary_sensor:
  - platform: gpio
    name: "Screen action"
    pin:
      number: 14
      inverted: true
      mode:
        input: true
        pullup: true
    on_press:
      then:
        if:
          condition:
            display.is_displaying_page: page7
          then:
              - logger.log: "Lights Page"
              - homeassistant.service:
                  service: switch.toggle
                  data:
                    entity_id: switch.external_lights_switch_1
  - platform: homeassistant
    name: "House lights"
    id: house_lights
    entity_id: switch.external_lights_switch_1
    internal: true

Can somebody advise me?

you defined a sensor on pin 14 (button?)
then you defined a switch " switch.external_lights_switch_1"

but when “on_press” you do “switch.toggle” → should be “switch.external_lights_switch_1”

This looks fine, note however that ESPHome compiler and editor won’t complain if you have the name of the entity wrong in either your homeassistant binary sensory or service call.

  1. double check the entity id is correct
  2. post the logs from ESPHome

Yes i’m define physical button on pin 14, and want by pressing this button turn on or off switch in HA depend of its state. Defining switch from HA it’s for displaying state from it on screen.

Entity ids seems to be ok, in logs nothing, except message from trigger.

[00:22:12][D][binary_sensor:036]: 'Switch screen': Sending state ON
[00:22:12][D][binary_sensor:036]: 'Switch screen': Sending state OFF
[00:22:13][D][binary_sensor:036]: 'Screen action': Sending state ON
[00:22:13][D][binary_sensor:036]: 'Screen action': Sending state OFF
[00:22:18][D][binary_sensor:036]: 'Switch screen': Sending state ON
[00:22:18][D][binary_sensor:036]: 'Switch screen': Sending state OFF
[00:22:19][D][binary_sensor:036]: 'Switch screen': Sending state ON
[00:22:19][D][binary_sensor:036]: 'Switch screen': Sending state OFF
[00:22:19][D][binary_sensor:036]: 'Switch screen': Sending state ON
[00:22:20][D][binary_sensor:036]: 'Switch screen': Sending state OFF
[00:22:20][D][binary_sensor:036]: 'Switch screen': Sending state ON
[00:22:21][D][binary_sensor:036]: 'Switch screen': Sending state OFF
[00:22:22][D][binary_sensor:036]: 'Switch screen': Sending state ON
[00:22:22][D][binary_sensor:036]: 'Switch screen': Sending state OFF
[00:22:24][D][binary_sensor:036]: 'Screen action': Sending state ON
[00:22:24][D][main:166]: Lights Page
[00:22:24][D][binary_sensor:036]: 'Screen action': Sending state OFF

Hmm - then I assume the issue is on the HA side. Unfortunately the ESPHome logger does not print any messages when it makes the service call.

I think you will need to check the Home Assistant logs for errors.

Amazing. My device was not granted for making HA service calls. So just one option in settings resolved my problem.

1 Like

Ah - I should have remembered that gotcha!