Text Sensor Component

Text sensors are a lot like normal sensors. But where the “normal” sensors only represent sensors that output numbers, this component can represent any text.

Base Text Sensor Configuration

# Example sensor configuration
name: Livingroom Temperature

# Optional variables:
icon: "mdi:water-percent"

Configuration variables:

  • name (Required, string): The name for the sensor.

  • icon (Optional, icon): Manually set the icon to use for the sensor in the frontend.

  • internal (Optional, boolean): Mark this component as internal. Internal components will not be exposed to the frontend (like Home Assistant). Only specifying an id without a name will implicitly set this to true.

  • disabled_by_default (Optional, boolean): If true, then this entity should not be added to any client’s frontend, (usually Home Assistant) without the user manually enabling it (via the Home Assistant UI). Requires Home Assistant 2021.9 or newer. Defaults to false.

  • If MQTT enabled, All other options from MQTT Component.

Automations:

  • on_value (Optional, Automation): An automation to perform when a new value is published. See on_value.

Text Sensor Automation

You can access the most recent state of the sensor in lambdas using id(sensor_id).state.

on_value

This automation will be triggered when a new value is published. In Lambdas you can get the value from the trigger with x.

text_sensor:
  - platform: version
    # ...
    on_value:
      then:
        - lambda: |-
            ESP_LOGD("main", "The current version is %s", x.c_str());

Configuration variables: See Automation.

text_sensor.state Condition

This Condition allows you to check if a given text sensor has a specific state.

on_...:
  - if:
      condition:
        # Checks if "my_text_sensor" has state "Hello World"
        text_sensor.state:
          id: my_text_sensor
          state: 'Hello World'

Configuration variables:

  • id (Required, ID): The text sensor ID.

  • state (Required, templatable, string): The state to compare to.

Note

This condition can also be expressed in lambdas:

if (id(my_text_sensor).state == "Hello World") {
  // do something
}

lambda calls

From lambdas, you can call several methods on all text sensors to do some advanced stuff (see the full API Reference for more info).

  • publish_state(): Manually cause the sensor to push out a value.

    // Within lambda, push a value of "Hello World"
    id(my_sensor).publish_state("Hello World");
    
  • .state: Retrieve the current value of the sensor as an std::string object.

    // For example, create a custom log message when a value is received:
    std::string val = id(my_sensor).state;
    ESP_LOGI("main", "Value of my sensor: %s", val.c_str());
    

See Also