Text sensor value update in code

I wanted to create a text sensor similar to the Wi-Fi status for ESPHome, so it takes a text value that is then displayed in HA.
Adapting code and function from here.

I created the following looking at the documentation:

text_sensor:
  - platform: template
    name: "Door Position Sensor"
    id: ${id_name}_door_position_sensor

Now I want to update the value in other areas of the code:

stop_action:
      - lambda: |-
          if (id(${id_name}_door).current_operation ==  esphome::cover::COVER_OPERATION_CLOSING || id(${id_name}_door).current_operation ==  esphome::cover::COVER_OPERATION_OPENING )
          //${id_name}_door_position_sensor = 'Stopped';
          {
            id(${id_name}_door).current_operation =  esphome::cover::COVER_OPERATION_IDLE;
            //Stop the door if it is moving
            id(performing_last_movement) = false;
            //Pulse the SBC button to trigger the motion
            id(${id_name}_sbc).turn_on();
            delay(500);
            id(${id_name}_sbc).turn_off();
          }

The issue is with this line. When I uncomment it, it throws an error

 //${id_name}_door_position_sensor = 'Stopped';

I wasn’t able to figure it out from the documentation that mainly includes lambda based updates.
Any references that can help?

You have to call text_sensor.template.publish.

1 Like

I feel stupid for missing the last line on the page. Thank you.
Had to use a pointer (->) to get it to compile, and now it works:

stop_action:
      - lambda: |-
          if (id(${id_name}_door).current_operation ==  esphome::cover::COVER_OPERATION_CLOSING || id(${id_name}_door).current_operation ==  esphome::cover::COVER_OPERATION_OPENING )
          ${id_name}_door_position_sensor->publish_state("Stopped");
          {
            id(${id_name}_door).current_operation =  esphome::cover::COVER_OPERATION_IDLE;
            //Stop the door if it is moving
            id(performing_last_movement) = false;
            //Pulse the SBC button to trigger the motion
            id(${id_name}_sbc).turn_on();
            delay(500);
            id(${id_name}_sbc).turn_off();
          }
1 Like