esphome-docs/components/text_sensor/template.rst

88 lines
2.6 KiB
ReStructuredText
Raw Normal View History

Template Text Sensor
====================
2018-11-14 22:12:27 +01:00
.. seo::
2019-02-16 23:25:23 +01:00
:description: Instructions for setting up template text sensors in ESPHome
:image: description.svg
2018-11-14 22:12:27 +01:00
The ``template`` text sensor platform allows you to create a text sensor with templated values
using :ref:`lambdas <config-lambda>`.
.. code-block:: yaml
# Example configuration entry
text_sensor:
- platform: template
name: "Template Text Sensor"
lambda: |-
2018-10-17 21:13:52 +02:00
return {"Hello World"};
2019-01-06 18:56:14 +01:00
update_interval: 60s
Possible return values for the lambda:
2018-10-17 21:13:52 +02:00
- ``return {"STRING LITERAL"};`` the new value for the sensor of type ``std::string``. **Has to be** in
brackets ``{}``!
- ``return {};`` if you don't want to publish a new state (advanced).
Configuration variables:
------------------------
- **name** (**Required**, string): The name of the text sensor.
- **lambda** (*Optional*, :ref:`lambda <config-lambda>`):
Lambda to be evaluated every update interval to get the new value of the text sensor
- **update_interval** (*Optional*, :ref:`config-time`): The interval to publish the value of the
text sensor, either the result of the lambda function or if no lambda function the last value
published using the publish action. Defaults to ``60s``.
2019-02-07 18:07:16 +01:00
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
2019-02-17 12:28:17 +01:00
- All other options from :ref:`Text Sensor <config-text_sensor>`.
2019-02-16 23:25:23 +01:00
.. _text_sensor-template-publish_action:
``text_sensor.template.publish`` Action
---------------------------------------
You can also publish a state to a template text sensor from elsewhere in your YAML file
with the ``text_sensor.template.publish`` action.
.. code-block:: yaml
# Example configuration entry
text_sensor:
- platform: template
name: "Template Text Sensor"
id: template_text
# in some trigger
on_...:
- text_sensor.template.publish:
id: template_text
state: "Hello World"
# Templated
- text_sensor.template.publish:
id: template_text
state: !lambda 'return "Hello World";'
Configuration options:
- **id** (**Required**, :ref:`config-id`): The ID of the template text sensor.
- **state** (**Required**, string, :ref:`templatable <config-templatable>`):
The state to publish.
.. note::
This action can also be written in lambdas:
.. code-block:: cpp
id(template_text).publish_state("Hello World");
See Also
--------
2021-12-13 06:26:36 +01:00
- :doc:`/components/text_sensor/index`
- :ref:`automation`
2019-05-12 22:44:59 +02:00
- :apiref:`template/text_sensor/template_text_sensor.h`
- :ghedit:`Edit`