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

66 lines
2.7 KiB
ReStructuredText
Raw Normal View History

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.
:image: description.png
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.
.. 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: |-
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
optimistic: true
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
- **name** (**Required**, string): The name of the switch.
- **lambda** (*Optional*, :ref:`lambda <config-lambda>`):
Lambda to be evaluated repeatedly to get the current state of the switch.
Only state *changes* will be published to MQTT.
- **optimistic** (*Optional*, boolean): Whether to operate in optimistic mode - when in this mode,
any command sent to the template cover will immediately update the reported state and no lambda
needs to be used. Defaults to ``false``.
- **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
be performed when the remote (like Home Assistant's frontend) requests the switch to be turned on.
- **restore_state** (*Optional*, boolean): Sets whether esphomelib should attempt to restore the
state on boot-up and call the turn on/off actions with the recovered values. Defaults to ``yes``.
2018-06-01 18:10:00 +02:00
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
- All other options from :ref:`Binary Sensor <config-binary_sensor>` and :ref:`MQTT Component <config-mqtt-component>`.
See Also
2018-08-24 22:44:01 +02:00
--------
2018-06-01 18:10:00 +02:00
- :doc:`/esphomeyaml/guides/automations`
- :doc:`/esphomeyaml/components/switch/index`
- :doc:`/esphomeyaml/components/binary_sensor/index`
- :doc:`API Reference </api/switch/template>`
2018-06-04 08:17:22 +02:00
- `Edit this page on GitHub <https://github.com/OttoWinter/esphomedocs/blob/current/esphomeyaml/components/switch/template.rst>`__
2018-10-12 16:33:22 +02:00
.. disqus::