Skip to content

Commit 469bcee

Browse files
authored
[binary_sensor] Document invalidate_state (#4974)
1 parent 0ddd837 commit 469bcee

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

components/binary_sensor/index.rst

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ Advanced options:
6868
- **disabled_by_default** (*Optional*, boolean): If true, then this entity should not be added to any client's frontend,
6969
(usually Home Assistant) without the user manually enabling it (via the Home Assistant UI).
7070
Defaults to ``false``.
71-
- **publish_initial_state** (*Optional*, boolean): If true, then the sensor will publish its initial state at boot or when
72-
HA first connects, depending on the platform. This means that any applicable triggers will be run. Defaults to ``false``.
71+
- **trigger_on_initial_state** (*Optional*, boolean): If true, any applicable triggers will be fired when the binary sensor
72+
state changes from ``unknown`` to a valid state. This applies to the first valid state set, and any valid state set after
73+
a ``binary_sensor.invalidate_state`` action has been excuted. The default is ``false``.
74+
**publish_initial_state** (*Optional*, boolean): A deprecated equivalent to ``trigger_on_initial_state``.
7375
- **entity_category** (*Optional*, string): The category of the entity.
7476
See https://developers.home-assistant.io/docs/core/entity/#generic-properties
7577
for a list of available options.
@@ -79,6 +81,27 @@ Advanced options:
7981

8082
.. _binary_sensor-filters:
8183

84+
85+
Actions
86+
-------
87+
88+
.. _binary_sensor-invalidate_state-action:
89+
90+
``binary_sensor.invalidate_state`` Action
91+
*****************************************
92+
93+
This action will invalidate the current state of the sensor. It is most useful with the Template binary sensor.
94+
After the state is invalidated, it will be reported to Home Assistant as ``unknown``. Example:
95+
96+
.. code-block:: yaml
97+
98+
on_...:
99+
binary_sensor.invalidate_state: my_binary_sensor_id
100+
101+
102+
The state may also be invalidated by an API call in a lambda - see the API reference linked below.
103+
104+
82105
Binary Sensor Filters
83106
---------------------
84107

@@ -274,7 +297,8 @@ Configuration variables: See :ref:`Automation <automation>`.
274297

275298
This automation will be triggered when a new state is received (and thus combines ``on_press``
276299
and ``on_release`` into one trigger). The new state will be given as the variable ``x`` as a boolean
277-
and can be used in :ref:`lambdas <config-lambda>`.
300+
and can be used in :ref:`lambdas <config-lambda>`. It will not be called when the state is invalidated; it will be called when
301+
the state initially becomes valid only if ``trigger_on_initial_state`` is true.
278302

279303
.. code-block:: yaml
280304
@@ -287,6 +311,31 @@ and can be used in :ref:`lambdas <config-lambda>`.
287311
288312
Configuration variables: See :ref:`Automation <automation>`.
289313

314+
.. _binary_sensor-on_state_change:
315+
316+
``on_state_change``
317+
*******************
318+
319+
An alternative to ``on_state`` that is also triggered when the binary sensor state is invalidated. It is passed two parameters, ``x`` as for ``on_change``
320+
will be the new value, and ``x_previous`` is the value immediately prior to the change. Both these parameters are of type ``optional<bool>`` so also indicate
321+
if the values were valid. Note that this is called on all state changes, including initial states.
322+
323+
.. code-block:: yaml
324+
325+
binary_sensor:
326+
- platform: gpio
327+
# ...
328+
on_state_change:
329+
then:
330+
- logger.log:
331+
format: "Old state was %s"
332+
args: ['x_previous.has_value() ? ONOFF(x_previous) : "Unknown"']
333+
- logger.log:
334+
format: "New state is %s"
335+
args: ['x.has_value() ? ONOFF(x) : "Unknown"']
336+
337+
Configuration variables: See :ref:`Automation <automation>`.
338+
290339
.. _binary_sensor-on_click:
291340

292341
``on_click``

0 commit comments

Comments
 (0)