Using HA input_button with ESPHome

I have an input button in HA and would like to use it as an input to ESPhome.

In ESPHome it’s defined as:

binary_sensor:
  - platform: homeassistant
    name: "Manual drum wash"
    entity_id: input_button.manual_drum_wash
    id: manual_drum_wash
    on_press:
      then:
        - script.execute: do_drum_wash 

In the logs, I see:

[09:25:46][W][homeassistant.binary_sensor:017]: Can't convert '2023-04-13T08:25:46.736193+00:00' to binary state!

Clearly, binary_sensor is not the way to go.

I’ve created a template sensor in HA:

- sensor:
    - name: Manual drum wash
      unique_id: manual_drum_wash
      state: >
        {{ states('input_button.manual_drum_wash') }}

And then used this in ESPHome:

sensor:
  - platform: homeassistant
    name: "Manual drum wash"
    entity_id: sensor.manual_drum_wash
    id: manual_drum_wash
    on_value:
      then:
        - script.execute: do_drum_wash 

This approach works, however, is this the best way to achieve this, or am I missing something simple?

To be tested, but I think just having, in esphome

sensor:
  - platform: homeassistant
    name: "Manual drum wash"
    entity_id: input_button.manual_drum_wash
    id: manual_drum_wash
    on_value:
      then:
        - script.execute: do_drum_wash 

would work.

Fact is, the “state” of a HA button is the timestamp of the last time it was “triggered”

1 Like

Thank you.

I knew there had to be a simpler way.

Yes, it does work.

Whilst this approach does work, it does throw the following warning in the log:

[16:19:08][W][homeassistant.sensor:015]: Can't convert '2023-04-16T06:19:59.118272+00:00' to number! which is understandable.

If there is a ‘cleaner’ solution, it’d be nice to know.

Use an ESPhome text_sensor. Should work the same.

Thank you, I’ll check that out.

Using the previous solution, at boot, ESPHome sees it as a value change and unwantedly triggers the action.

EDIT: That works! Thank you for suggesting that. No warning in the log and no unwanted triggering.