2018-11-27 17:05:23 +01:00
|
|
|
Custom Text Sensor
|
|
|
|
==================
|
|
|
|
|
2023-11-01 06:47:57 +01:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Custom components are deprecated, not recommended for new configurations
|
|
|
|
and will be removed from ESPHome in a future release.
|
|
|
|
Please look at creating a real ESPHome component and "importing" it into your
|
|
|
|
configuration with :doc:`/components/external_components`.
|
|
|
|
|
|
|
|
You can find some basic documentation on creating your own components
|
|
|
|
at :ref:`contributing_to_esphome`.
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
This integration can be used to create custom text sensors in ESPHome
|
2018-11-27 17:05:23 +01:00
|
|
|
using the C++ (Arduino) API.
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
Please first read :doc:`/components/sensor/custom` guide,
|
2018-11-27 17:05:23 +01:00
|
|
|
the same principles apply here and text sensors are very similar
|
|
|
|
to sensors internally.
|
|
|
|
|
|
|
|
The example below is an example of a custom text sensor which constantly publishes
|
|
|
|
the message "Hello World!".
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
#include "esphome.h"
|
2018-11-27 17:05:23 +01:00
|
|
|
|
2019-05-12 22:44:59 +02:00
|
|
|
class MyCustomTextSensor : public PollingComponent, public TextSensor {
|
2018-11-27 17:05:23 +01:00
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
MyCustomTextSensor() : PollingComponent(15000) {}
|
|
|
|
|
|
|
|
void setup() override {
|
|
|
|
// This will be called by App.setup()
|
|
|
|
}
|
|
|
|
void update() override {
|
|
|
|
// This will be called every "update_interval" milliseconds.
|
|
|
|
// Publish state
|
|
|
|
publish_state("Hello World!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(Store this file in your configuration directory, for example ``my_text_sensor.h``)
|
|
|
|
|
|
|
|
And in YAML:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example configuration entry
|
2019-02-16 23:25:23 +01:00
|
|
|
esphome:
|
2018-11-27 17:05:23 +01:00
|
|
|
includes:
|
|
|
|
- my_text_sensor.h
|
|
|
|
|
|
|
|
text_sensor:
|
|
|
|
- platform: custom
|
|
|
|
lambda: |-
|
|
|
|
auto my_custom_sensor = new MyCustomTextSensor();
|
|
|
|
App.register_component(my_custom_sensor);
|
|
|
|
return {my_custom_sensor};
|
|
|
|
|
|
|
|
text_sensors:
|
|
|
|
name: "My Custom Text Sensor"
|
|
|
|
|
|
|
|
Configuration variables:
|
|
|
|
|
|
|
|
- **lambda** (**Required**, :ref:`lambda <config-lambda>`): The lambda to run for instantiating the
|
|
|
|
text sensor(s).
|
|
|
|
- **text_sensors** (**Required**, list): A list of text sensors to initialize. The length here
|
|
|
|
must equal the number of items in the ``return`` statement of the ``lambda``.
|
|
|
|
|
2019-02-17 12:28:17 +01:00
|
|
|
- All options from :ref:`Text Sensor <config-text_sensor>`.
|
2018-11-27 17:05:23 +01:00
|
|
|
|
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
2021-12-13 06:25:17 +01:00
|
|
|
- :doc:`/components/text_sensor/index`
|
2021-12-08 09:20:06 +01:00
|
|
|
- :apiclass:`API Reference <text_sensor::TextSensor>`
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|