Textsensor on value : on | off

hello ,

Hello, I have imported an entity in ESPHome and want to create an if statement to log different messages based on the entity’s value (“on” or “off”). I’ll be using the log as a test environment.

i tried many hours , lambdas , if then , google , chat gpt , but nothing worked
are there examples how to do this , so i can use it to learn and adapt it to my goal ?

text_sensor:
  - platform: homeassistant
    id: poortfdc
    name: poortopenIswaakhondstil
    entity_id: binary_sensor.poortfdc
    internal: False
    on_value: 
      then:
        - logger.log: "recieve ON"
      else :
          logger.log : "recieved off"


Import it as a binary_sensor instead of a text_sensor.

binary_sensor:
  - platform: homeassistant
    id: poortfdc
    name: poortopenIswaakhondstil
    entity_id: binary_sensor.poortfdc
    internal: true
    on_press: 
      then:
        - logger.log: "recieve ON" 
    on_release:
      then:
        - logger.log : "recieved off"

Btw. you should set sensors imported from HA as internal: true, to avoid exporting them back to Home Assistant.

2 Likes

been there done that to , then i got error : can’t convert ‘off’ to binary state

How did you create the binary_sensor in HA?

the original entity in HA is via mqtt

but i dont want to spend processorpower and programmemory to set up a mqtt in my esp32

This works for me.

Home Assistant:

template:
 - binary_sensor:
    - name: "Is Night"
      unique_id: is_night
      state: >-
        {% if now().hour <= 6 %}
        {{ on }}
        {% else %}
        {{ off }}
        {% endif %}

ESPHome:

binary_sensor:
  - platform: homeassistant
    id: ha_is_night
    entity_id: binary_sensor.is_night

If you can’t get it to work importing it as a binary_sensor, you could use a text_sensor, like this:

text_sensor:
  - platform: homeassistant
    id: poortfdc
    name: poortopenIswaakhondstil
    entity_id: binary_sensor.poortfdc
    internal: true
    on_value:
      - if:
          condition:
            text_sensor.state:
              id: poortfdc
              state: 'on'
          then:
            - logger.log: "recieved on"
          else:
            - logger.log: "recieved off"

hello ,
i had it almost correct in my many hours of testing of code in the esp
i lost many time , and even google had no examples how to do this

i had a wrong condition :

          condition:
              id: text_sensor.poortfdc
              state: 'on'

`text_sensor:

  • platform: homeassistant
    id: poortfdc
    name: poortopenIswaakhondstil
    entity_id: binary_sensor.poortfdc
    internal: true
    on_value:
    • if:
      condition:
      text_sensor.state:
      id: poortfdc
      state: ‘on’
      then:
      - logger.log: “recieved on”
      else:
      - logger.log: “recieved off”`

thx , good night

Sounds like you spent hours trying and looking everywhere except going to the official esphome documentation where there are examples for exactly what you were trying to do…

You should always make this your first stop in the future, FYI.