esphome-docs/esphomeyaml/components/sensor/template.rst

61 lines
1.8 KiB
ReStructuredText
Raw Normal View History

2018-06-01 18:10:00 +02:00
Template Sensor
===============
2018-11-14 22:12:27 +01:00
.. seo::
:description: Instructions for setting up template sensors with esphomelib.
:image: description.svg
2018-06-01 18:10:00 +02:00
The ``template`` sensor platform allows you to create a sensor with templated values
using :ref:`lambdas <config-lambda>`.
.. code:: yaml
# Example configuration entry
sensor:
- platform: template
name: "Template Sensor"
lambda: >-
if (id(some_binary_sensor).state) {
2018-06-01 18:10:00 +02:00
return 42.0;
} else {
return 0.0;
}
update_interval: 15s
Possible return values for the lambda:
- ``return <FLOATING_POINT_NUMBER>;`` the new value for the sensor.
- ``return NAN;`` if the state should be considered invalid to indicate an error (advanced).
- ``return {};`` if you don't want to publish a new state (advanced).
Configuration variables:
2018-08-24 22:44:01 +02:00
------------------------
2018-06-01 18:10:00 +02:00
2018-10-20 15:21:24 +02:00
- **name** (**Required**, string): The name of the sensor.
2018-06-01 18:10:00 +02:00
- **lambda** (*Optional*, :ref:`lambda <config-lambda>`):
Lambda to be evaluated every update interval to get the new value of the sensor
- **update_interval** (*Optional*, :ref:`config-time`): The interval to check the
2018-06-01 18:44:15 +02:00
sensor. Defaults to ``15s``. See :ref:`sensor-default_filter`.
2018-06-01 18:10:00 +02:00
- **id** (*Optional*,:ref:`config-id`): Manually specify the ID used for code generation.
2018-10-20 15:21:24 +02:00
- All other options from :ref:`Sensor <config-sensor>` and :ref:`MQTT Component <config-mqtt-component>`.
2018-06-01 18:10:00 +02:00
2018-11-14 22:12:27 +01:00
.. note::
You can use the ``publish_state()`` method to set the value of a template
sensor from other automations:
.. code:: cpp
id(my_sensor).publish_state(42.0);
2018-06-01 18:10:00 +02:00
See Also
2018-08-24 22:44:01 +02:00
--------
2018-06-01 18:10:00 +02:00
- :ref:`sensor-filters`
- :ref:`automation`
- :doc:`API Reference </api/sensor/template>`
2018-06-04 08:17:22 +02:00
- `Edit this page on GitHub <https://github.com/OttoWinter/esphomedocs/blob/current/esphomeyaml/components/sensor/template.rst>`__
2018-10-12 16:33:22 +02:00
.. disqus::