Having an esphome device update a Home Assistant helper (toggle)

Hello.

I have a very basic question about how to make an esphome device (esp32) update a Home Assistant Helper (toggle).
The helper is working fine from Home Assistant and will determine if the esphome device will stay awake so that it can be OTA flashed at a later stage.
The esphome device is reading the value of the helper from Home Assistant fine (on boot) and any changes to the toggle I make in Home Assistant will instantly reflect in the esphome devices’ log and it will stay awake (i.e. not sleep) if the toggle is ON/true.
But I have not found a way to update this Home Assistant helper from the esphome device. An optimal thing would be to have the esphome device set the helper to OFF/false whenever a successful OTA flashing has just taken place, so that I don’t have to do this myself manually in Home Assistant.

The esphome (esp32) yaml code for the device is:

binary_sensor:
- platform: homeassistant
    id: prevent_sleep
    entity_id: input_boolean.prevent_sleep

I have tried with the following esphome (esp32) yaml code on the device (for updating the Home Assistant helper/toggle from the esphome device):

 lambda: id(prevent_sleep).publish_state(false);

The code is executed as I get the following from the esp32 log:

[D][binary_sensor:036]: 'prevent_sleep': Sending state OFF

However the helper (toggle) in Home Assistant is not updated.
I have tried searching the forum and the rest of the web and even spent a few hours with chatGPT trying different solutions without any luck.
Obviously I don’t really know what I am doing here and my experience messaging back and forth between esphome devices and Home Assistant is very limited (e.g. I know almost nothing about scripts and services).

So I would appreciate that any help on this topic is delivered in a way where you don’t expect me to instantly understand all the details :slight_smile:

Kind regards,
Thomas.

you should use prevent _sleep is a global defined thing or device specific ? aka try to be specific as you can be, if it needs to apply to all esp’s (or some that use it) do esp32_prevent_sleep for a test this is of course fine

also use text_sensor → binary sensor = numeric only Home Assistant Text Sensor — ESPHome

You need to use a homeassistant.service call.

Beat me to it.

1 Like

Thanks a lot for your kind input.
I managed to get it to work with using a homeassistant.service.

It appears that the publish_state code I used only updated the ESPHome device’s version of the toggle, which did not propagate back to the Home Assistant helper.

 lambda: id(prevent_sleep).publish_state(false);

But by using homeassistant.service it propagated back to the Home Assistant helper, which is what I wanted.

Below I list some code with a working test example of what I ended up with in case others are interested.
One important detail is that this will not work until you go into Home Assistant Settings/Devices & Services/Integrations/ESPHome and then choose Configure for the espdevice you are working with and then tick the box Allow the device to make Home Assistant service calls. The default setting (for me at least) was off.
In this example, the Home Assistant helper is called helper1 (which must be created in Home Assistant) and Settings/Devices & Services/Helpers
In the example, I call the script my_script when the ESPHome device has connected with the Home Assistant API (otherwise the value of the Home Assistant helper is not ‘visible’ to the ESPHome device).
The script toggles helper1 every 5 seconds forever, which can then be viewed from Home Assistant (I put helper1 in a dashboard and viewed it from there).
I hope this is of use to someone who was as lost as I was for many days.

esp32:
  board: esp32dev
  framework:
    type: arduino

esphome:
  name: esp32mini2
  friendly_name: ESP32 Mini 2

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  # This is only executed when the esphome device has connected to the Home Assistant API
  on_client_connected:
    - script.execute: my_script

binary_sensor:
  - platform: homeassistant
    id: helper1
    entity_id: input_boolean.helper1

script:
  - id: my_script
    mode: queued
    then:
      - homeassistant.service:
          service: input_boolean.turn_on
          data:
            entity_id: input_boolean.helper1
      - logger.log: 'helper1 was turned on'
      - delay: 5s

      - homeassistant.service:
          service: input_boolean.turn_off
          data:
            entity_id: input_boolean.helper1
      - logger.log: 'helper1 was turned off'
      - delay: 5s

      # Repeat over and over again
      - script.execute: my_script
2 Likes

Thomas, you saved me a lot. I had two weeks long to save this problem until I saw the above answers.

Thanks!

@sendcard
Great - I am glad you got it working :slight_smile: