2018-06-01 18:10:00 +02:00
|
|
|
Template Switch
|
|
|
|
===============
|
|
|
|
|
2018-11-14 22:12:27 +01:00
|
|
|
.. seo::
|
|
|
|
:description: Instructions for setting up template switches that can execute arbitrary actions when turned on or off.
|
2021-11-16 03:19:33 +01:00
|
|
|
:image: description.svg
|
2018-11-14 22:12:27 +01:00
|
|
|
|
2018-06-01 18:10:00 +02:00
|
|
|
The ``template`` switch platform allows you to create simple switches out of just actions and
|
|
|
|
an optional value lambda. Once defined, it will automatically appear in Home Assistant
|
|
|
|
as a switch and can be controlled through the frontend.
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
.. code-block:: yaml
|
2018-06-01 18:10:00 +02:00
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
switch:
|
|
|
|
- platform: template
|
|
|
|
name: "Template Switch"
|
2018-11-14 22:12:27 +01:00
|
|
|
lambda: |-
|
2018-10-20 14:53:27 +02:00
|
|
|
if (id(some_binary_sensor).state) {
|
2018-06-01 18:10:00 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
turn_on_action:
|
2018-11-10 14:33:24 +01:00
|
|
|
- switch.turn_on: switch2
|
2018-06-01 18:10:00 +02:00
|
|
|
turn_off_action:
|
2018-11-10 14:33:24 +01:00
|
|
|
- switch.turn_on: switch1
|
2018-06-01 18:10:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
Possible return values for the optional lambda:
|
|
|
|
|
|
|
|
- ``return true;`` if the switch should be reported as ON.
|
|
|
|
- ``return false;`` if the switch should be reported as OFF.
|
|
|
|
- ``return {};`` if the last state should be repeated.
|
|
|
|
|
|
|
|
Configuration variables:
|
2018-08-24 22:44:01 +02:00
|
|
|
------------------------
|
2018-06-01 18:10:00 +02:00
|
|
|
|
|
|
|
- **lambda** (*Optional*, :ref:`lambda <config-lambda>`):
|
|
|
|
Lambda to be evaluated repeatedly to get the current state of the switch.
|
|
|
|
- **turn_on_action** (*Optional*, :ref:`Action <config-action>`): The action that should
|
|
|
|
be performed when the remote (like Home Assistant's frontend) requests the switch to be turned on.
|
|
|
|
- **turn_off_action** (*Optional*, :ref:`Action <config-action>`): The action that should
|
2019-01-02 15:39:46 +01:00
|
|
|
be performed when the remote (like Home Assistant's frontend) requests the switch to be turned off.
|
2019-02-16 23:25:23 +01:00
|
|
|
- **optimistic** (*Optional*, boolean): Whether to operate in optimistic mode - when in this mode,
|
2021-07-01 00:49:14 +02:00
|
|
|
any command sent to the template switch will immediately update the reported state.
|
2019-02-16 23:25:23 +01:00
|
|
|
Defaults to ``false``.
|
|
|
|
- **assumed_state** (*Optional*, boolean): Whether the true state of the switch is not known.
|
|
|
|
This will make the Home Assistant frontend show buttons for both ON and OFF actions, instead
|
|
|
|
of hiding one of them when the switch is ON/OFF. Defaults to ``false``.
|
2019-02-17 12:28:17 +01:00
|
|
|
- All other options from :ref:`Switch <config-switch>`.
|
2018-06-01 18:10:00 +02:00
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
.. _switch-template-publish_action:
|
|
|
|
|
|
|
|
``switch.template.publish`` Action
|
|
|
|
----------------------------------
|
|
|
|
|
|
|
|
You can also publish a state to a template switch from elsewhere in your YAML file
|
|
|
|
with the ``switch.template.publish`` action.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
switch:
|
|
|
|
- platform: template
|
|
|
|
name: "Template Switch"
|
|
|
|
id: template_swi
|
|
|
|
|
|
|
|
# in some trigger
|
|
|
|
on_...:
|
|
|
|
- switch.template.publish:
|
|
|
|
id: template_swi
|
|
|
|
state: ON
|
|
|
|
|
|
|
|
# Templated
|
|
|
|
- switch.template.publish:
|
|
|
|
id: template_swi
|
|
|
|
state: !lambda 'return true;'
|
|
|
|
|
|
|
|
Configuration options:
|
|
|
|
|
|
|
|
- **id** (**Required**, :ref:`config-id`): The ID of the template switch.
|
|
|
|
- **state** (**Required**, boolean, :ref:`templatable <config-templatable>`):
|
|
|
|
The state to publish.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2023-01-14 23:24:17 +01:00
|
|
|
This action can also be written in lambdas, the parameter of the `publish_state` method denotes if
|
2021-08-09 10:02:43 +02:00
|
|
|
the switch is currently on or off:
|
2019-02-16 23:25:23 +01:00
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2021-08-09 10:02:43 +02:00
|
|
|
id(template_swi).publish_state(false);
|
2019-02-16 23:25:23 +01:00
|
|
|
|
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
|
|
|
|
2024-06-27 01:55:41 +02:00
|
|
|
- :doc:`/automations/index`
|
2019-02-07 13:54:45 +01:00
|
|
|
- :doc:`/components/switch/index`
|
|
|
|
- :doc:`/components/binary_sensor/index`
|
2019-05-12 22:44:59 +02:00
|
|
|
- :apiref:`template/switch/template_switch.h`
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|