2018-05-13 11:37:02 +02:00
Sensor Component
================
2018-11-14 22:12:27 +01:00
.. seo ::
2019-02-16 23:25:23 +01:00
:description: Instructions for setting up sensor components in ESPHome.
2021-11-16 03:19:33 +01:00
:image: folder-open.svg
2018-11-14 22:12:27 +01:00
2019-02-16 23:25:23 +01:00
ESPHome has support for many different sensors. Each of them is a
2018-05-13 11:37:02 +02:00
platform of the `` sensor `` domain and each sensor has several base
configuration options.
2018-06-01 18:10:00 +02:00
.. _config-sensor:
2018-05-13 11:37:02 +02:00
Base Sensor Configuration
2018-08-24 22:44:01 +02:00
-------------------------
2018-05-13 11:37:02 +02:00
2019-02-16 23:25:23 +01:00
All sensors in ESPHome have a name and some other
2018-05-13 11:37:02 +02:00
optional configuration options. By default, the sensor platform will
chose appropriate values for all of these by default, but you can always
override them if you want to.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-05-13 11:37:02 +02:00
# Example sensor configuration
name: Livingroom Temperature
# Optional variables:
unit_of_measurement: "°C"
icon: "mdi:water-percent"
2021-02-15 17:01:55 +01:00
device_class: "temperature"
2021-06-03 03:50:14 +02:00
state_class: "measurement"
2018-05-13 11:37:02 +02:00
accuracy_decimals: 1
expire_after: 30s
filters:
- sliding_window_moving_average:
window_size: 15
send_every: 15
Configuration variables:
2022-02-19 12:40:26 +01:00
- **id** (*Optional* , string): Manually specify the ID for code generation. At least one of **id** and **name** must be specified.
- **name** (*Optional* , string): The name for the sensor. At least one of **id** and **name** must be specified.
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 sensor to use that name, you can set `` name: None `` .
2018-06-01 18:10:00 +02:00
- **unit_of_measurement** (*Optional* , string): Manually set the unit
of measurement the sensor should advertise its values with. This does
not actually do any maths (conversion between units).
2021-02-15 17:01:55 +01:00
- **device_class** (*Optional* , string): The device class for the
2021-11-30 05:04:52 +01:00
sensor. See https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
2021-02-15 17:01:55 +01:00
for a list of available options. Set to `` "" `` to remove the default device class of a sensor.
2021-06-03 03:50:14 +02:00
- **state_class** (*Optional* , string): The state class for the
sensor. See https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes
for a list of available options. Set to `` "" `` to remove the default state class of a sensor.
2021-10-10 10:39:50 +02:00
- **icon** (*Optional* , icon): Manually set the icon to use for the sensor in the frontend.
2022-06-30 23:57:19 +02:00
- **accuracy_decimals** (*Optional* , int): Manually set the number of decimals to use when reporting values. This does not impact the actual value reported to Home Assistant, it just sets the number of decimals to use when displaying it.
2018-06-01 18:10:00 +02:00
- **filters** (*Optional* ): Specify filters to use for some basic
2019-02-28 09:37:37 +01:00
transforming of values. See :ref: `Sensor Filters <sensor-filters>` for more information.
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.
2019-10-21 19:41:18 +02:00
- **force_update** (*Optional* , boolean): If true, this option will force the frontend (usually Home
Assistant) to create a state changed event when the sensor updates even if the value stayed the same.
Some applications like Grafana require this when working with Home Assistant, but beware it can
significantly increase the database size. Defaults to `` false `` .
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.
2018-06-13 22:38:49 +02:00
Automations:
2018-06-01 18:10:00 +02:00
- **on_value** (*Optional* , :ref: `Automation <automation>` ): An automation to perform
when a new value is published. See :ref: `sensor-on_value` .
- **on_value_range** (*Optional* , :ref: `Automation <automation>` ): An automation to perform
when a published value transition from outside to a range to inside. See :ref: `sensor-on_value_range` .
- **on_raw_value** (*Optional* , :ref: `Automation <automation>` ): An automation to perform
when a raw value is received that hasn't passed through any filters. See :ref: `sensor-on_raw_value` .
2019-02-17 12:28:17 +01:00
MQTT Options:
- **expire_after** (*Optional* , :ref: `config-time` ): Manually set the time in which
the sensor values should be marked as “expired”/“unknown”. Not providing any value means no expiry.
- All other options from :ref: `MQTT Component <config-mqtt-component>` .
2018-06-01 18:10:00 +02:00
2018-08-22 22:05:28 +02:00
.. note ::
If you're trying to setup filters for a sensor that has multiple outputs - for example a DHT22 which
reports temperature *and* humidity - put the `` filters `` option into each sensor output like this:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-08-22 22:05:28 +02:00
sensor:
- platform: dht
# ...
temperature:
filters:
# ...
humidity:
filters:
# ...
2018-06-01 18:10:00 +02:00
.. _sensor-filters:
2018-05-13 11:37:02 +02:00
Sensor Filters
2018-08-24 22:44:01 +02:00
--------------
2018-05-13 11:37:02 +02:00
2019-02-16 23:25:23 +01:00
ESPHome allows you to do some basic pre-processing of
2018-05-13 11:37:02 +02:00
sensor values before they’ re sent to Home Assistant. This is for example
2019-02-16 23:25:23 +01:00
useful if you want to apply some average over the last few values.
2018-05-13 11:37:02 +02:00
2019-02-26 19:33:50 +01:00
There are a lot of filters that sensors support. You define them by adding a `` filters ``
block in the sensor configuration (at the same level as `` platform `` ; or inside each sensor block
for platforms with multiple sensors)
2021-09-19 01:02:45 +02:00
Filters are processed in the order they are defined in your configuration.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-05-13 11:37:02 +02:00
# Example filters:
filters:
- offset: 2.0
- multiply: 1.2
2019-02-26 19:33:50 +01:00
- calibrate_linear:
- 0.0 -> 0.0
- 40.0 -> 45.0
- 100.0 -> 102.5
2018-05-13 11:37:02 +02:00
- filter_out: 42.0
2019-08-27 19:39:32 +02:00
- median:
window_size: 5
send_every: 5
send_first_at: 1
2021-12-14 19:43:48 +01:00
- quantile:
window_size: 5
send_every: 5
send_first_at: 1
quantile: .9
2018-05-13 11:37:02 +02:00
- sliding_window_moving_average:
window_size: 15
send_every: 15
- exponential_moving_average:
alpha: 0.1
send_every: 15
2018-05-17 21:39:46 +02:00
- throttle: 1s
2021-10-13 02:04:33 +02:00
- throttle_average: 1s
2018-05-18 08:21:52 +02:00
- heartbeat: 5s
- debounce: 0.1s
2023-07-16 23:17:34 +02:00
- timeout: 1min
2018-05-17 21:39:46 +02:00
- delta: 5.0
- or:
- throttle: 1s
- delta: 5.0
2018-06-05 21:07:40 +02:00
- lambda: return x * (9.0/5.0) + 32.0;
2018-05-13 11:37:02 +02:00
2021-03-11 19:31:31 +01:00
`` offset ``
***** *****
Adds a constant value to each sensor value.
2019-02-26 19:33:50 +01:00
.. code-block :: yaml
# Example configuration entry
- platform: adc
# ...
filters:
- offset: 2.0
- multiply: 1.2
2021-03-11 19:31:31 +01:00
`` multiply ``
***** ***** **
Multiplies each value by a constant value.
2019-02-26 19:33:50 +01:00
2019-03-13 18:57:49 +01:00
.. _sensor-filter-calibrate_linear:
2019-02-26 19:33:50 +01:00
`` calibrate_linear ``
***** ***** ***** *****
Calibrate your sensor values by using values you measured with an accurate "truth" source.
2023-07-30 23:09:15 +02:00
Configuration variables:
- **method** (*Optional* , string): The method for calculating the linear function(s).
One of `` least_squares `` or `` exact `` . Defaults to `` least_squares `` .
- **datapoints** (**Required** ): The list of datapoints.
2019-02-26 19:33:50 +01:00
First, collect a bunch of values of what the sensor shows and what the real value should be.
For temperature, this can for example be achieved by using an accurate thermometer. For other
sensors like power sensor this can be done by connecting a known load and then writing down
the value the sensor shows.
.. code-block :: yaml
# Example configuration entry
- platform: dht
# ...
2019-03-11 13:21:21 +01:00
temperature:
2019-07-28 12:41:15 +02:00
name: "DHT22 Temperature"
2019-02-26 19:33:50 +01:00
filters:
- calibrate_linear:
2023-07-30 23:09:15 +02:00
method: least_squares
datapoints:
# Map 0.0 (from sensor) to 1.0 (true value)
- 0.0 -> 1.0
2019-02-26 19:33:50 +01:00
- 10.0 -> 12.1
2023-07-30 23:09:15 +02:00
The arguments are a list of data points, each in the form `` MEASURED -> TRUTH `` . Depending on
the `` method `` ESPHome will then either fit a linear equation to the values (using least squares)
or connect the values exactly using multiple linear equations. You need to supply at least two
values. When using `` least_squares `` and more than two values are given a linear solution will be
calculated and may not represent each value exactly.
.. figure :: images/sensor_filter_calibrate_linear.png
:align: center
:width: 50.0%
2019-02-26 19:33:50 +01:00
2019-11-01 18:28:26 +01:00
.. _sensor-calibrate_polynomial:
2019-07-28 12:41:15 +02:00
`` calibrate_polynomial ``
***** ***** ***** ***** *** *
Calibrate your sensor values by fitting them to a polynomial functions. This is similar to
the `` calibrate_linear `` filter, but also allows for higher-order functions like quadratic polynomials.
.. code-block :: yaml
# Example configuration entry
- platform: adc
# ...
filters:
- calibrate_polynomial:
degree: 2
datapoints:
# Map 0.0 (from sensor) to 0.0 (true value)
- 0.0 -> 0.0
- 10.0 -> 12.1
- 13.0 -> 14.0
The arguments are a list of data points, each in the form `` MEASURED -> TRUTH `` . Additionally, you need
to specify the degree of the resulting polynomial, the datapoints will then be fitted to the given
degree with a least squares solver.
2019-02-26 19:33:50 +01:00
`` filter_out ``
***** ***** *** *
2021-03-11 19:31:31 +01:00
(**Required** , number): Filter out specific values to be displayed. For example to filter out the value `` 85.0 ``
2019-02-26 19:33:50 +01:00
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- filter_out: 85.0
2023-07-30 23:09:15 +02:00
`` clamp ``
***** *** *
2023-10-25 22:30:25 +02:00
Limits the value to the range between `` min_value `` and `` max_value `` . By default, sensor values outside these bounds will be set to `` min_value `` or `` max_value `` , respectively. If `` ignore_out_of_range `` is true, then sensor values outside those bounds will be ignored. If `` min_value `` is not set, there is no lower bound; if `` max_value `` is not set there is no upper bound.
2023-07-30 23:09:15 +02:00
Configuration variables:
- **min_value** (*Optional* , float): The lower bound of the range.
- **max_value** (*Optional* , float): The upper bound of the range.
2023-10-25 22:30:25 +02:00
- **ignore_out_of_range** (*Optional* , bool): If true, ignores all sensor values out of the range. Defaults to `` false `` .
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- clamp:
min_value: 10
max_value: 75
ignore_out_of_range: true
2023-07-30 23:09:15 +02:00
2023-10-14 02:07:27 +02:00
`` round ``
***** *** *
Rounds the value to the given decimal places.
.. code-block :: yaml
- platform: ...
filters:
- round: 1 # will round to 1 decimal place
2021-12-14 19:43:48 +01:00
`` quantile ``
***** ***** **
2023-10-18 14:21:12 +02:00
A `simple moving quantile <https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/quantile.htm> `__
2021-12-14 19:43:48 +01:00
over the last few values. This can be used to filter outliers from the received sensor data. A large
window size will make the filter slow to react to input changes.
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- quantile:
window_size: 7
send_every: 4
send_first_at: 3
quantile: .9
Configuration variables:
- **window_size** (*Optional* , int): The number of values over which to calculate the quantile
when pushing out a value.
Defaults to `` 5 `` .
- **send_every** (*Optional* , int): How often a sensor value should be pushed out. For
example, in above configuration the quantile is calculated after every 4th
received sensor value, over the last 7 received values.
Defaults to `` 5 `` .
- **send_first_at** (*Optional* , int): By default, the very first raw value on boot is immediately
published. With this parameter you can specify when the very first value is to be sent.
Must be smaller than or equal to `` send_every ``
Defaults to `` 1 `` .
- **quantile** (*Optional* , float): value from 0 to 1 to determine which quantile to pick.
Defaults to `` .9 `` .
2019-08-27 19:39:32 +02:00
`` median ``
***** *****
2021-03-11 19:31:31 +01:00
A `simple moving median <https://en.wikipedia.org/wiki/Median_filter#Worked_1D_example> `__
over the last few values. This can be used to filter outliers from the received sensor data. A large
window size will make the filter slow to react to input changes.
2019-08-27 19:39:32 +02:00
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- median:
window_size: 7
send_every: 4
send_first_at: 3
2021-03-11 19:31:31 +01:00
Configuration variables:
2021-11-28 19:57:01 +01:00
- **window_size** (*Optional* , int): The number of values over which to calculate the median
2021-03-11 19:31:31 +01:00
when pushing out a value. This number should
be odd if you want an actual received value pushed out.
Defaults to `` 5 `` .
2021-11-28 19:57:01 +01:00
- **send_every** (*Optional* , int): How often a sensor value should be pushed out. For
2021-03-11 19:31:31 +01:00
example, in above configuration the median is calculated after every 4th
received sensor value, over the last 7 received values.
Defaults to `` 5 `` .
2021-11-28 19:57:01 +01:00
- **send_first_at** (*Optional* , int): By default, the very first raw value on boot is immediately
2021-03-11 19:31:31 +01:00
published. With this parameter you can specify when the very first value is to be sent.
Must be smaller than or equal to `` send_every ``
Defaults to `` 1 `` .
`` min ``
***** **
A moving minimum over the last few values. A large window size will make the filter slow to
2021-03-05 14:10:31 +01:00
react to input changes.
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- min:
window_size: 7
send_every: 4
send_first_at: 3
2021-03-11 19:31:31 +01:00
Configuration variables:
2021-11-28 19:57:01 +01:00
- **window_size** (*Optional* , int): The number of values over which to calculate the min/max when pushing out a
2021-03-11 19:31:31 +01:00
value. Defaults to `` 5 `` .
2021-11-28 19:57:01 +01:00
- **send_every** (*Optional* , int): How often a sensor value should be pushed out. For
2021-03-11 19:31:31 +01:00
example, in above configuration the min is calculated after every 4th
received sensor value, over the last 7 received values.
Defaults to `` 5 `` .
2021-11-28 19:57:01 +01:00
- **send_first_at** (*Optional* , int): By default, the very first raw value on boot is immediately
2021-03-11 19:31:31 +01:00
published. With this parameter you can specify when the very first value is to be sent.
Must be smaller than or equal to `` send_every ``
Defaults to `` 1 `` .
`` max ``
***** **
A moving maximum over the last few values. A large window size will make the filter slow to
react to input changes.
2021-03-05 14:10:31 +01:00
2021-03-11 19:31:31 +01:00
Configuration variables:
2021-03-05 14:10:31 +01:00
2021-11-28 19:57:01 +01:00
- **window_size** (*Optional* , int): The number of values over which to calculate the min/max
2021-03-11 19:31:31 +01:00
when pushing out a value.
Defaults to `` 5 `` .
2021-11-28 19:57:01 +01:00
- **send_every** (*Optional* , int): How often a sensor value should be pushed out. For
2023-03-07 09:19:07 +01:00
example, in above configuration the max is calculated after every 4th
2021-03-11 19:31:31 +01:00
received sensor value, over the last 7 received values.
Defaults to `` 5 `` .
2021-11-28 19:57:01 +01:00
- **send_first_at** (*Optional* , int): By default, the very first raw value on boot is immediately
2021-03-11 19:31:31 +01:00
published. With this parameter you can specify when the very first value is to be sent.
Must be smaller than or equal to `` send_every ``
Defaults to `` 1 `` .
2021-03-05 14:10:31 +01:00
2021-03-11 19:31:31 +01:00
`` sliding_window_moving_average ``
***** ***** ***** ***** ***** ***** ***
2019-02-26 19:33:50 +01:00
2021-03-11 19:31:31 +01:00
A `simple moving average <https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average> `__
over the last few values. It can be used to have a short update interval on the sensor but only push
out an average on a specific interval (thus increasing resolution).
2019-02-26 19:33:50 +01:00
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- sliding_window_moving_average:
window_size: 15
send_every: 15
2018-05-13 11:37:02 +02:00
2021-03-11 19:31:31 +01:00
Configuration variables:
2021-11-28 19:57:01 +01:00
- **window_size** (*Optional* , int): The number of values over which to perform an
2021-03-11 19:31:31 +01:00
average when pushing out a value.
2021-11-28 19:57:01 +01:00
- **send_every** (*Optional* , int): How often a sensor value should be pushed out. For
2021-03-11 19:31:31 +01:00
example, in above configuration the weighted average is only
pushed out on every 15th received sensor value.
2021-11-28 19:57:01 +01:00
- **send_first_at** (*Optional* , int): By default, the very first raw value on boot is immediately
2021-03-11 19:31:31 +01:00
published. With this parameter you can specify when the very first value is to be sent.
Defaults to `` 1 `` .
2018-05-13 11:37:02 +02:00
2022-01-09 23:55:29 +01:00
.. _sensor-filter-exponential_moving_average:
2021-03-11 19:31:31 +01:00
`` exponential_moving_average ``
***** ***** ***** ***** ***** *****
2018-05-13 11:37:02 +02:00
2021-03-11 19:31:31 +01:00
A simple `exponential moving average
2023-10-18 14:21:12 +02:00
<https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average> `__ over the last few
2021-03-11 19:31:31 +01:00
values. It can be used to have a short update interval on the sensor but only push
out an average on a specific interval (thus increasing resolution).
2018-05-13 11:37:02 +02:00
2021-03-11 19:31:31 +01:00
Configuration variables:
2022-04-14 06:33:09 +02:00
- **alpha** (*Optional* , float): The forget factor/alpha value of the filter.
A higher value includes more details in the output while a lower value removes more noise.
Defaults to `` 0.1 `` .
2021-11-28 19:57:01 +01:00
- **send_every** (*Optional* , int): How often a sensor value should be pushed out. Defaults to `` 15 `` .
2022-02-21 01:13:12 +01:00
- **send_first_at** (*Optional* , int): By default, the very first raw value on boot is immediately
published. With this parameter you can specify when the very first value is to be sent.
Defaults to `` 1 `` .
2021-03-11 19:31:31 +01:00
2023-03-19 20:50:21 +01:00
`` skip_initial ``
***** ***** ***** *
2023-05-31 15:31:55 +02:00
A simple skip filter; `` skip_initial: N `` skips the first `` N `` sensor readings and passes on the
2023-03-19 20:50:21 +01:00
rest. This can be used when the sensor needs a few readings to 'warm up'. After the initial
readings have been skipped, this filter does nothing.
.. code-block :: yaml
# Example configuration entry
- platform: wifi_signal
# ...
filters:
- skip_initial: 3
2021-03-11 19:31:31 +01:00
`` throttle ``
***** ***** **
2018-05-13 11:37:02 +02:00
2021-03-11 19:31:31 +01:00
Throttle the incoming values. When this filter gets an incoming value,
it checks if the last incoming value is at least `` specified time period `` old.
If it is not older than the configured value, the value is not passed forward.
2019-02-26 19:33:50 +01:00
.. code-block :: yaml
# Example filters:
filters:
- throttle: 1s
- heartbeat: 5s
- debounce: 0.1s
- delta: 5.0
- lambda: return x * (9.0/5.0) + 32.0;
2021-10-13 02:04:33 +02:00
`` throttle_average ``
***** ***** ***** *****
An average over the `` specified time period `` , potentially throttling incoming values. When this filter gets incoming values, it sums up all values and pushes out the average after the `` specified time period `` passed. There are two edge cases to consider within the `` specified time period `` :
* no value(s) received: `` NaN `` is returned - add the `` heartbeat `` filter if periodical pushes are required and/or `` filter_out: nan `` if required
* one value received: the value is pushed out after the `` specified time period `` passed, without calculating an average
For example a `` throttle_average: 60s `` will push out a value every 60 seconds, in case at least one sensor value is received within these 60 seconds.
2023-02-02 01:24:37 +01:00
In comparison to the `` throttle `` filter, it won't discard any values. In comparison to the `` sliding_window_moving_average `` filter, it supports variable sensor reporting rates without influencing the filter reporting interval (except for the first edge case).
2018-05-18 08:21:52 +02:00
2021-03-11 19:31:31 +01:00
`` heartbeat ``
***** ***** ***
2022-01-04 19:44:11 +01:00
Send the value periodically with the specified time interval.
If the sensor value changes during the interval the interval will not reset.
The last value of the sensor will be sent.
2021-03-11 19:31:31 +01:00
So a value of `` 10s `` will cause the filter to output values every 10s regardless
of the input values.
2023-07-16 23:17:34 +02:00
`` timeout ``
***** ***** **
After the first value has been sent, if no subsequent value is published within the
2023-08-10 07:11:07 +02:00
`` specified time period `` , send a value which defaults to `` NaN `` .
2023-07-16 23:17:34 +02:00
Especially useful when data is derived from some other communication
channel, e.g. a serial port, which can potentially be interrupted.
2023-08-10 07:11:07 +02:00
.. code-block :: yaml
# Example filters:
filters:
- timeout: 10s # sent value will be NaN
- timeout:
timeout: 10s
value: 0
2021-03-11 19:31:31 +01:00
`` debounce ``
***** ***** **
2018-05-18 08:21:52 +02:00
2021-03-11 19:31:31 +01:00
Only send values if the last incoming value is at least `` specified time period ``
old. For example if two values come in at almost the same time, this filter will only output
the last value and only after the specified time period has passed without any new incoming
values.
2018-05-17 21:39:46 +02:00
2021-03-11 19:31:31 +01:00
`` delta ``
***** *** *
2018-05-17 21:39:46 +02:00
2023-03-15 23:48:44 +01:00
This filter stores the last value passed through this filter and only passes incoming values through
if incoming value is sufficiently different from the previously passed one.
This difference can be calculated in two ways an absolute difference or a percentage difference.
If a number is specified, it will be used as the absolute difference required.
For example if the filter were configured with a value of 2 and the last value passed through was 10,
only values greater than 12 or less than 8 would be passed through.
.. code-block :: yaml
filters:
- delta: 2.0
If a percentage is specified a percentage of the last value will be used as the required difference.
For example if the filter were configured with a value of 20% and the last value passed through was 10,
only values greater than 12 or less than 8 would be passed through.
However, if the last value passed through was 100 only values greater than 120 or less than 80 would be passed through.
.. code-block :: yaml
filters:
- delta: 20%
2021-03-11 19:31:31 +01:00
`` or ``
***** *
Pass forward a value with the first child filter that returns. Above example
will only pass forward values that are *either* at least 1s old or are if the absolute
difference is at least 5.0.
2019-02-26 19:33:50 +01:00
.. code-block :: yaml
# Example filters:
filters:
- or:
- throttle: 1s
- delta: 5.0
2018-05-17 21:39:46 +02:00
2021-03-11 19:31:31 +01:00
`` lambda ``
***** *****
2019-02-26 19:33:50 +01:00
2021-03-11 19:31:31 +01:00
Perform a simple mathematical operation over the sensor values. The input value is `` x `` and
the result of the lambda is used as the output (use `` return `` ).
2019-02-26 19:33:50 +01:00
.. code-block :: yaml
filters:
- lambda: return x * (9.0/5.0) + 32.0;
Make sure to add `` .0 `` to all values in the lambda, otherwise divisions of integers will
result in integers (not floating point values).
2018-05-13 11:37:02 +02:00
2023-03-18 22:39:41 +01:00
To prevent values from being published, return `` {} `` :
.. code-block :: yaml
filters:
- lambda: !lambda |-
if (x < 10) return {};
return x-10;
2018-05-13 11:37:02 +02:00
Example: Converting Celsius to Fahrenheit
2018-08-24 22:44:01 +02:00
-----------------------------------------
2018-05-13 11:37:02 +02:00
While I personally don’ t like the Fahrenheit temperature scale, I do
2020-05-10 21:27:59 +02:00
understand that having temperature values appear in the Fahrenheit unit
is quite useful to some users. ESPHome uses the Celsius temperature
2018-05-13 11:37:02 +02:00
unit internally, and I’ m not planning on making converting between the
2020-05-10 21:27:59 +02:00
two simple (😉), but you can use this filter to convert Celsius values to
Fahrenheit.
2018-05-13 11:37:02 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-05-13 11:37:02 +02:00
filters:
2018-06-05 21:07:40 +02:00
- lambda: return x * (9.0/5.0) + 32.0;
2018-05-13 11:37:02 +02:00
unit_of_measurement: "°F"
2018-06-01 18:10:00 +02:00
2023-07-12 22:48:21 +02:00
.. _sensor-automations:
2018-06-01 18:10:00 +02:00
Sensor Automation
2018-08-24 22:44:01 +02:00
-----------------
2018-06-01 18:10:00 +02:00
You can access the most recent state of the sensor in :ref: `lambdas <config-lambda>` using
2018-12-16 11:08:54 +01:00
`` id(sensor_id).state `` and the most recent raw state using `` id(sensor_id).raw_state `` .
2018-06-01 18:10:00 +02:00
.. _sensor-on_value:
`` on_value ``
2018-08-24 22:44:01 +02:00
***** ***** **
2018-06-01 18:10:00 +02:00
This automation will be triggered when a new value that has passed through all filters
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-06-01 18:10:00 +02:00
sensor:
- platform: dallas
# ...
on_value:
then:
- light.turn_on:
id: light_1
red: !lambda "return x/255;"
Configuration variables: See :ref: `Automation <automation>` .
.. _sensor-on_value_range:
`` on_value_range ``
2018-08-24 22:44:01 +02:00
***** ***** ***** ***
2018-06-01 18:10:00 +02:00
With this automation you can observe if a sensor value passes from outside
a defined range of values to inside a range. For example you can have an
automation that triggers when a humidity crosses a threshold, and then turns on a dehumidifier.
This trigger will only trigger when the new value is inside the range and the previous value
2019-02-16 10:21:13 +01:00
was outside the range. On startup, the last state before reboot is restored and if the value crossed
the boundary during the boot process, the trigger is also executed.
2018-06-01 18:10:00 +02:00
Define the range with `` above `` and `` below `` . If only one of them is defined, the interval is half-open.
So for example `` above: 5 `` with no below would mean the range from 5 to positive infinity.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
sensor:
- platform: dallas
# ...
on_value_range:
2022-08-06 16:28:15 +02:00
- below: 5.0
then:
- switch.turn_on: relay_1
- above: 5.0
below: 10.0
then:
- switch.turn_on: relay_2
- above: 10.0
then:
- switch.turn_on: relay_3
2018-06-01 18:10:00 +02:00
Configuration variables:
- **above** (*Optional* , float): The minimum for the trigger.
- **below** (*Optional* , float): The maximum for the trigger.
- See :ref: `Automation <automation>` .
.. _sensor-on_raw_value:
`` on_raw_value ``
2018-08-24 22:44:01 +02:00
***** ***** ***** *
2018-06-01 18:10:00 +02:00
2019-05-27 11:29:26 +02:00
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 `` .
2018-06-01 18:10:00 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
sensor:
- platform: dallas
# ...
2019-05-27 11:29:26 +02:00
on_raw_value:
2018-06-01 18:10:00 +02:00
then:
- light.turn_on:
id: light_1
red: !lambda "return x/255;"
Configuration variables: See :ref: `Automation <automation>` .
2018-12-05 10:19:48 +01:00
.. _sensor-in_range_condition:
`` sensor.in_range `` Condition
***** ***** ***** ***** ***** *** *
This condition passes if the state of the given sensor is inside a range.
Define the range with `` above `` and `` below `` . If only one of them is defined, the interval is half-open.
So for example `` above: 5 `` with no below would mean the range from 5 to positive infinity.
.. code-block :: yaml
# in a trigger:
on_...:
if:
condition:
sensor.in_range:
id: my_sensor
above: 50.0
then:
- script.execute: my_script
Configuration variables:
- **above** (*Optional* , float): The minimum for the condition.
- **below** (*Optional* , float): The maximum for the condition.
2019-04-15 22:06:35 +02:00
.. _sensor-lambda_calls:
2018-06-07 15:55:31 +02:00
lambda calls
2018-08-24 22:44:01 +02:00
***** ***** **
2018-06-07 15:55:31 +02:00
From :ref: `lambdas <config-lambda>` , you can call several methods on all sensors to do some
2019-02-07 13:54:45 +01:00
advanced stuff (see the full API Reference for more info).
2018-06-07 15:55:31 +02:00
2018-10-20 14:53:27 +02:00
- `` publish_state() `` : Manually cause the sensor to push out a value. It will then
2023-03-19 20:50:21 +01:00
be processed by the sensor filters, and once filtered will propagate though ESPHome and though the API to Home Assistant or out via MQTT if configured.
2018-06-07 15:55:31 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: cpp
2018-06-07 15:55:31 +02:00
// Within lambda, push a value of 42.0
2018-10-20 14:53:27 +02:00
id(my_sensor).publish_state(42.0);
2018-06-07 15:55:31 +02:00
2018-10-20 14:53:27 +02:00
- `` .state `` : Retrieve the current value of the sensor that has passed through all sensor filters.
2018-06-07 15:55:31 +02:00
Is `` NAN `` if no value has gotten through all filters yet.
2018-11-19 18:32:16 +01:00
.. code-block :: cpp
2018-06-07 15:55:31 +02:00
// For example, create a custom log message when a value is received:
2018-10-20 14:53:27 +02:00
ESP_LOGI("main", "Value of my sensor: %f", id(my_sensor).state);
2018-06-07 15:55:31 +02:00
2023-02-04 21:22:50 +01:00
- `` raw_state `` : Retrieve the current value of the sensor that has not passed through any filters.
2020-09-15 17:55:07 +02:00
Is `` NAN `` if no value has been pushed by the sensor itself yet.
2018-06-07 15:55:31 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: cpp
2018-06-07 15:55:31 +02:00
// For example, create a custom log message when a value is received:
2018-10-20 14:53:27 +02:00
ESP_LOGI("main", "Raw Value of my sensor: %f", id(my_sensor).raw_state);
2018-06-07 15:55:31 +02:00
2018-06-01 18:10:00 +02:00
See Also
2018-10-17 20:44:37 +02:00
--------
2018-06-01 18:10:00 +02:00
2019-02-07 13:54:45 +01:00
- :apiref: `sensor/sensor.h`
- :ghedit: `Edit`
2018-06-01 18:10:00 +02:00
.. toctree ::
:maxdepth: 1
2018-10-20 14:53:27 +02:00
:glob:
2018-06-01 18:10:00 +02:00
2018-10-20 14:53:27 +02:00
*