ESPHome, automation from HA device

I am trying to create an automation on the state of a Home Assistant Device, Following is my current code:

text_sensor:
  - platform: homeassistant
    name: "Lampe Salon"
    entity_id: switch.lampe_salon
    on_value:
      then:
        #- light_turn_on:
        #    id: compressor_switch1
        - lambda: |-
            ESP_LOGI("main", "The compressor is %s", x.c_str());
       
light:
  platform: binary
  output: ${esp_id}_BuildInLED
  name: Compressor Switch 1    # Comment or uncomment to have visible in Home Assistant
  id: compressor_switch1 
  restore_mode: ALWAYS_ON

output:
  platform: gpio
  id: ${esp_id}_BuildInLED
  pin:
    number: D4
    inverted: true

I wish to turn_on the my “id: compressor_switch1” which is the build-in on my D1Mini based on the value from my Home Assistant Device “entity_id: switch.lampe_salon”, I have try different thing on-value, on_turn_on and other but could not find that right one. Was also unable to find information in the documentation. Could someone help me?

Use a binary sensor component to bring your switch into the ESPHome device instead of a text sensor:

Then you can use the “on press” and “on release” triggers to perform your automation.

Also, why are you using the light component for your compressor switch instead of the gpio switch component?

1 Like

Why am I using light component instead of gpio switch ? Because, it work and I did not know better. I changed it, thank for the suggestion.

I changed, as suggested from a text sensor to a binary sensor using on_press:
Here is my revised code:

#text_sensor:  change to binary_sensor
  - platform: homeassistant
    name: "Lampe Salon"
    entity_id: switch.lampe_salon
    on_press:
      then:
        - switch_turn_on:
            id: build_in_led_switch

switch:
  - platform: gpio
    pin: D4
    name: "Build In LED"
    id: build_in_led_switch

I got an error message when using action “switch_turn_on”

  on_press: 
    then: 
      - 
        Unable to find action with the name 'switch_turn_on'.

I tried “switch_turn_on” for another platform ‘gpio’ and it works but not not for platform 'homeassistant".
Any other idea and suggestion?

It’s switch.turn_on not switch_turn_on, you must have got it right for the other component.

Thank you, got it to work great.

1 Like