2018-10-17 20:44:37 +02:00
|
|
|
|
Text Sensor Component
|
|
|
|
|
=====================
|
|
|
|
|
|
2018-11-14 22:12:27 +01:00
|
|
|
|
.. seo::
|
|
|
|
|
:description: Instructions for setting up text sensors that represent their state as a string of text.
|
2021-11-16 03:19:33 +01:00
|
|
|
|
:image: folder-open.svg
|
2018-11-14 22:12:27 +01:00
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
|
Text sensors are a lot like normal :doc:`sensors </components/sensor/index>`.
|
2018-10-17 20:44:37 +02:00
|
|
|
|
But where the "normal" sensors only represent sensors that output **numbers**, this
|
|
|
|
|
component can represent any *text*.
|
|
|
|
|
|
|
|
|
|
.. _config-text_sensor:
|
|
|
|
|
|
|
|
|
|
Base Text Sensor Configuration
|
|
|
|
|
------------------------------
|
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
|
.. code-block:: yaml
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
# Example sensor configuration
|
|
|
|
|
name: Livingroom Temperature
|
|
|
|
|
|
|
|
|
|
# Optional variables:
|
|
|
|
|
icon: "mdi:water-percent"
|
|
|
|
|
|
|
|
|
|
Configuration variables:
|
|
|
|
|
|
|
|
|
|
- **name** (**Required**, string): The name for the sensor.
|
2023-03-27 04:35:26 +02:00
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
If you have a :ref:`friendly_name <esphome-configuration_variables>` set for your device and
|
|
|
|
|
you want the text sensor to use that name, you can set ``name: None``.
|
|
|
|
|
|
2018-10-17 20:44:37 +02:00
|
|
|
|
- **icon** (*Optional*, icon): Manually set the icon to use for the sensor in the frontend.
|
2019-02-17 12:28:17 +01:00
|
|
|
|
- **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.
|
2021-08-10 03:45:41 +02:00
|
|
|
|
- **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``.
|
2021-11-07 19:24:55 +01:00
|
|
|
|
- **entity_category** (*Optional*, string): The category of the entity.
|
|
|
|
|
See https://developers.home-assistant.io/docs/core/entity/#generic-properties
|
|
|
|
|
for a list of available options. Requires Home Assistant 2021.11 or newer.
|
|
|
|
|
Set to ``""`` to remove the default entity category.
|
2019-02-17 12:28:17 +01:00
|
|
|
|
- If MQTT enabled, All other options from :ref:`MQTT Component <config-mqtt-component>`.
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
Automations:
|
|
|
|
|
|
|
|
|
|
- **on_value** (*Optional*, :ref:`Automation <automation>`): An automation to perform
|
|
|
|
|
when a new value is published. See :ref:`text_sensor-on_value`.
|
2021-09-29 23:25:11 +02:00
|
|
|
|
- **on_raw_value** (*Optional*, :ref:`Automation <automation>`): An automation to perform
|
|
|
|
|
when a new value is received that hasn't passed through any filters. See :ref:`text_sensor-on_raw_value`.
|
|
|
|
|
|
|
|
|
|
.. _text_sensor-filters:
|
|
|
|
|
|
|
|
|
|
Text Sensor Filters
|
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
|
|
ESPHome allows you to do some basic pre-processing of
|
|
|
|
|
text_sensor values before they’re sent to Home Assistant. This is for example
|
|
|
|
|
useful if you want to manipulate the text_sensor string in some fashion.
|
|
|
|
|
|
|
|
|
|
There are a lot of filters that sensors support. You define them by adding a ``filters``
|
|
|
|
|
block in the text_sensor configuration (at the same level as ``platform``; or inside each text_sensor block
|
|
|
|
|
for platforms with multiple sensors).
|
|
|
|
|
|
|
|
|
|
Filters are processed in the order they are defined in your configuration.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example filters:
|
|
|
|
|
filters:
|
|
|
|
|
- to_upper:
|
|
|
|
|
- to_lower:
|
2021-10-21 11:53:08 +02:00
|
|
|
|
- append: "_suffix"
|
|
|
|
|
- prepend: "prefix_"
|
2021-09-29 23:25:11 +02:00
|
|
|
|
- substitute:
|
|
|
|
|
- "suf -> foo"
|
|
|
|
|
- "pre -> bar"
|
|
|
|
|
- lambda: return {"Hello World"};
|
|
|
|
|
|
|
|
|
|
``to_upper``
|
|
|
|
|
************
|
|
|
|
|
|
|
|
|
|
Converts all characters within a string to uppercase (only the English alphabet is supported at this time).
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
|
- platform: template
|
|
|
|
|
# ...
|
|
|
|
|
filters:
|
|
|
|
|
- to_upper:
|
|
|
|
|
|
|
|
|
|
``to_lower``
|
|
|
|
|
************
|
|
|
|
|
|
|
|
|
|
Converts all characters within a string to lowercase (only the English alphabet is supported at this time).
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
|
- platform: template
|
|
|
|
|
# ...
|
|
|
|
|
filters:
|
|
|
|
|
- to_lower:
|
|
|
|
|
|
|
|
|
|
``append``
|
|
|
|
|
**********
|
|
|
|
|
|
|
|
|
|
Adds a string to the end of the current string.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
|
- platform: template
|
|
|
|
|
# ...
|
|
|
|
|
filters:
|
2021-10-21 11:53:08 +02:00
|
|
|
|
- append: "_suffix"
|
2021-09-29 23:25:11 +02:00
|
|
|
|
|
|
|
|
|
``prepend``
|
|
|
|
|
***********
|
|
|
|
|
|
|
|
|
|
Adds a string to the start of the current string.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
|
- platform: template
|
|
|
|
|
# ...
|
|
|
|
|
filters:
|
2021-10-21 11:53:08 +02:00
|
|
|
|
- prepend: "prefix_"
|
2021-09-29 23:25:11 +02:00
|
|
|
|
|
|
|
|
|
``substitute``
|
|
|
|
|
**************
|
|
|
|
|
|
|
|
|
|
Search the current value of the text sensor for a string, and replace it with another string.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
|
- platform: template
|
|
|
|
|
# ...
|
|
|
|
|
filters:
|
|
|
|
|
- substitute:
|
|
|
|
|
- "suf -> foo"
|
|
|
|
|
- "pre -> bar"
|
|
|
|
|
|
|
|
|
|
The arguments are a list of substitutions, each in the form ``TO_FIND -> REPLACEMENT``.
|
|
|
|
|
|
2021-11-25 21:35:38 +01:00
|
|
|
|
``map``
|
|
|
|
|
*******
|
|
|
|
|
|
2023-03-27 04:35:26 +02:00
|
|
|
|
Lookup the current value of the text sensor in a list, and return the matching item if found.
|
2021-11-25 21:35:38 +01:00
|
|
|
|
Does not change the value of the text sensor if the current value wasn't found.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
|
- platform: template
|
|
|
|
|
# ...
|
|
|
|
|
filters:
|
|
|
|
|
- map:
|
|
|
|
|
- high -> On
|
|
|
|
|
- low -> Off
|
|
|
|
|
|
|
|
|
|
The arguments are a list of substitutions, each in the form ``LOOKUP -> REPLACEMENT``.
|
|
|
|
|
|
2021-09-29 23:25:11 +02:00
|
|
|
|
``lambda``
|
|
|
|
|
**********
|
|
|
|
|
|
|
|
|
|
Perform a advanced operations on the text sensor value. The input string is ``x`` and
|
|
|
|
|
the result of the lambda is used as the output (use ``return``).
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
filters:
|
|
|
|
|
- lambda: |-
|
|
|
|
|
if (x == "Hello") {
|
|
|
|
|
return x + "bar";
|
|
|
|
|
} else {
|
|
|
|
|
return x + "foo";
|
|
|
|
|
}
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
Text Sensor Automation
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
You can access the most recent state of the sensor in :ref:`lambdas <config-lambda>` using
|
2019-01-13 15:49:06 +01:00
|
|
|
|
``id(sensor_id).state``.
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
.. _text_sensor-on_value:
|
|
|
|
|
|
|
|
|
|
``on_value``
|
|
|
|
|
************
|
|
|
|
|
|
|
|
|
|
This automation will be triggered when a new value is published.
|
|
|
|
|
In :ref:`Lambdas <config-lambda>` you can get the value from the trigger with ``x``.
|
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
|
.. code-block:: yaml
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
text_sensor:
|
|
|
|
|
- platform: version
|
|
|
|
|
# ...
|
|
|
|
|
on_value:
|
|
|
|
|
then:
|
|
|
|
|
- lambda: |-
|
|
|
|
|
ESP_LOGD("main", "The current version is %s", x.c_str());
|
|
|
|
|
|
|
|
|
|
Configuration variables: See :ref:`Automation <automation>`.
|
|
|
|
|
|
2021-09-29 23:25:11 +02:00
|
|
|
|
.. _text_sensor-on_raw_value:
|
|
|
|
|
|
|
|
|
|
``on_raw_value``
|
|
|
|
|
****************
|
|
|
|
|
|
|
|
|
|
This automation will be triggered when a new value is received that hasn't passed
|
|
|
|
|
through any filters. In :ref:`Lambdas <config-lambda>` you can get the value from the trigger with ``x``.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
text_sensor:
|
|
|
|
|
- platform: version
|
|
|
|
|
# ...
|
|
|
|
|
on_raw_value:
|
|
|
|
|
then:
|
|
|
|
|
- lambda: |-
|
|
|
|
|
ESP_LOGD("main", "The current version is %s", x.c_str());
|
|
|
|
|
|
|
|
|
|
Configuration variables: See :ref:`Automation <automation>`.
|
|
|
|
|
|
2019-05-15 11:49:05 +02:00
|
|
|
|
.. _text_sensor-state_condition:
|
|
|
|
|
|
|
|
|
|
``text_sensor.state`` Condition
|
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
|
|
This :ref:`Condition <config-condition>` allows you to check if a given text sensor
|
|
|
|
|
has a specific state.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
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**, :ref:`config-id`): The text sensor ID.
|
|
|
|
|
- **state** (**Required**, :ref:`templatable <config-templatable>`, string): The state to compare
|
|
|
|
|
to.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
This condition can also be expressed in :ref:`lambdas <config-lambda>`:
|
|
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
|
|
if (id(my_text_sensor).state == "Hello World") {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 22:06:35 +02:00
|
|
|
|
.. _text_sensor-lambda_calls:
|
|
|
|
|
|
2018-10-17 20:44:37 +02:00
|
|
|
|
lambda calls
|
|
|
|
|
************
|
|
|
|
|
|
|
|
|
|
From :ref:`lambdas <config-lambda>`, you can call several methods on all text sensors to do some
|
2019-02-07 13:54:45 +01:00
|
|
|
|
advanced stuff (see the full API Reference for more info).
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
2018-10-26 22:27:01 +02:00
|
|
|
|
- ``publish_state()``: Manually cause the sensor to push out a value.
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
|
.. code-block:: cpp
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
// Within lambda, push a value of "Hello World"
|
2018-10-26 22:27:01 +02:00
|
|
|
|
id(my_sensor).publish_state("Hello World");
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
2018-10-26 22:27:01 +02:00
|
|
|
|
- ``.state``: Retrieve the current value of the sensor as an ``std::string`` object.
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
|
.. code-block:: cpp
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
// For example, create a custom log message when a value is received:
|
2018-10-26 22:27:01 +02:00
|
|
|
|
std::string val = id(my_sensor).state;
|
2018-10-17 20:44:37 +02:00
|
|
|
|
ESP_LOGI("main", "Value of my sensor: %s", val.c_str());
|
|
|
|
|
|
|
|
|
|
See Also
|
|
|
|
|
--------
|
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
|
- :apiref:`text_sensor/text_sensor.h`
|
|
|
|
|
- :ghedit:`Edit`
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
|
:maxdepth: 1
|
2018-12-05 10:19:48 +01:00
|
|
|
|
:glob:
|
2018-10-17 20:44:37 +02:00
|
|
|
|
|
2018-12-05 10:19:48 +01:00
|
|
|
|
*
|