2018-06-01 18:10:00 +02:00
.. _automation:
2019-10-18 09:22:48 +02:00
Automations and Templates
2018-06-01 18:10:00 +02:00
=========================
2018-11-14 22:12:27 +01:00
.. seo ::
2019-02-16 23:25:23 +01:00
:description: Getting started guide for automations in ESPHome.
2021-11-16 03:19:33 +01:00
:image: auto-fix.svg
2018-11-14 22:12:27 +01:00
2019-10-18 09:22:48 +02:00
Automations and templates are two very powerful aspects of ESPHome. Automations
2018-10-20 15:10:26 +02:00
allow you to perform actions under certain conditions and templates are a way to easily
2019-02-16 23:25:23 +01:00
customize everything about your node without having to dive into the full ESPHome C++
2018-06-01 18:10:00 +02:00
API.
Let's begin with an example to explain these concepts. Suppose you have this configuration file:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
switch:
- platform: gpio
pin: GPIO3
2018-10-20 15:10:26 +02:00
name: "Living Room Dehumidifier"
2018-06-01 18:10:00 +02:00
binary_sensor:
- platform: gpio
pin: GPIO4
2018-10-20 15:10:26 +02:00
name: "Living Room Dehumidifier Toggle Button"
2018-06-01 18:10:00 +02:00
With this file you can already perform some basic tasks. You can control the ON/OFF state
2018-10-20 15:10:26 +02:00
of the dehumidifier in your living room from Home Assistant's front-end. But in many cases,
2018-06-01 18:10:00 +02:00
controlling everything strictly from the frontend is quite a pain. That's why you have
2018-10-20 15:10:26 +02:00
decided to also install a simple push button next to the dehumidifier on pin GPIO4.
A simple push on this button should toggle the state of the dehumidifier.
2018-06-01 18:10:00 +02:00
You *could* write an automation to do this task in Home Assistant's automation engine, but
2019-01-09 14:43:18 +01:00
ideally the IoT should work without an internet connection and should not break with
the MQTT server being offline.
2018-06-01 18:10:00 +02:00
2019-02-16 23:25:23 +01:00
That's why, starting with ESPHome 1.7.0, there's a new automation engine. With it, you
2018-06-01 18:10:00 +02:00
can write some basic (and also some more advanced) automations using a syntax that is
hopefully a bit easier to read and understand than Home Assistant's.
For example, this configuration would achieve your desired behavior:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
switch:
- platform: gpio
pin: GPIO3
name: "Living Room Dehumidifier"
id: dehumidifier1
binary_sensor:
- platform: gpio
pin: GPIO4
2018-10-20 15:10:26 +02:00
name: "Living Room Dehumidifier Toggle Button"
2018-06-01 18:10:00 +02:00
on_press:
then:
2018-11-10 14:33:24 +01:00
- switch.toggle: dehumidifier1
2018-06-01 18:10:00 +02:00
Woah, hold on there. Please explain what's going on here! Sure :) Let's step through what's happening here.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
switch:
- platform: gpio
# ...
id: dehumidifier1
2019-10-21 20:35:56 +02:00
First, we have to give the dehumidifier a :ref: `config-id` so that we can
2018-06-01 18:10:00 +02:00
later use it inside our awesome automation.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
binary_sensor:
- platform: gpio
# ...
on_press:
We now attach a special attribute `` on_press `` to the toggle button. This part is called a "trigger". In this example,
the automation in the next few lines will execute whenever someone *begins* to press the button. Note the terminology
follows what you would call these events on mouse buttons. A *press* happens when you begin pressing the button/mouse.
There are also other triggers like `` on_release `` , `` on_click `` or `` on_double_click `` available.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
# ...
on_press:
then:
2018-11-10 14:31:27 +01:00
- switch.toggle: dehumidifier1
2018-06-01 18:10:00 +02:00
.. _config-action:
Actions
2018-10-12 16:33:22 +02:00
-------
2018-06-01 18:10:00 +02:00
2019-02-16 23:25:23 +01:00
Now comes the actual automation block. With `` then `` , you tell ESPHome what should happen when the press happens.
2018-06-01 18:10:00 +02:00
Within this block, you can define several "actions". For example, `` switch.toggle `` and the line after that form an
action. Each action is separated by a dash and multiple actions can be executed in series by just adding another `` - ``
like so:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
# ...
on_press:
then:
2018-11-10 14:33:24 +01:00
- switch.toggle: dehumidifier1
2018-06-01 18:10:00 +02:00
- delay: 2s
2018-11-10 14:33:24 +01:00
- switch.toggle: dehumidifier1
2018-06-01 18:10:00 +02:00
With this automation, a press on the push button would cause the dehumidifier to turn on/off for 2 seconds, and then
cycle back to its original state. Similarly you can have a single trigger with multiple automations:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
# ...
on_press:
- then:
2018-11-10 14:33:24 +01:00
- switch.toggle: dehumidifier1
2018-06-01 18:10:00 +02:00
- then:
2018-11-10 14:33:24 +01:00
- light.toggle: dehumidifier_indicator_light
2018-06-01 18:10:00 +02:00
# Same as:
on_press:
then:
2018-11-10 14:33:24 +01:00
- switch.toggle: dehumidifier1
- light.toggle: dehumidifier_indicator_light
2018-06-01 18:10:00 +02:00
2019-10-18 09:22:48 +02:00
As a last example, let's make our dehumidifier smart: Let's make it turn on automatically when the humidity reported by a sensor
is above 65%, and make it turn off again when it falls below 50%:
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: dht
humidity:
name: "Living Room Humidity"
on_value_range:
- above: 65.0
then:
2018-11-10 14:33:24 +01:00
- switch.turn_on: dehumidifier1
2018-06-01 18:10:00 +02:00
- below: 50.0
then:
2018-11-10 14:33:24 +01:00
- switch.turn_off: dehumidifier1
2018-06-01 18:10:00 +02:00
temperature:
name: "Living Room Temperature"
2019-10-18 09:22:48 +02:00
That's a lot of indentation 😉 `` on_value_range `` is a special trigger for sensors that trigger when the value output
2018-06-01 18:10:00 +02:00
of the sensor is within a certain range. In the first example, this range is defined as "any value above or including
65.0", and the second one refers to once the humidity reaches 50% or below.
2019-10-18 09:22:48 +02:00
Now that concludes the introduction to automations in ESPHome. They're a powerful tool to automate almost
2018-06-01 18:10:00 +02:00
everything on your device with an easy-to-use syntax. For the cases where the "pure" YAML automations don't work,
2019-02-16 23:25:23 +01:00
ESPHome has another extremely powerful tool to offer: Templates.
2018-06-01 18:10:00 +02:00
.. _config-lambda:
Templates (Lambdas)
2018-10-12 16:33:22 +02:00
-------------------
2018-06-01 18:10:00 +02:00
2019-02-16 23:25:23 +01:00
With templates inside ESPHome, you can do almost *everything* . If for example you want to only perform a certain
2018-06-01 18:10:00 +02:00
automation if a certain complex formula evaluates to true, you can do that with templates. Let's look at an example
first:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
binary_sensor:
- platform: gpio
name: "Cover End Stop"
id: top_end_stop
cover:
- platform: template
name: Living Room Cover
2019-01-06 18:56:14 +01:00
lambda: !lambda |-
2018-10-20 14:53:27 +02:00
if (id(top_end_stop).state) {
2019-05-12 22:44:59 +02:00
return COVER_OPEN;
2018-06-01 18:10:00 +02:00
} else {
2019-05-12 22:44:59 +02:00
return COVER_CLOSED;
2018-06-01 18:10:00 +02:00
}
What's happening here? First, we define a binary sensor (with the id `` top_end_stop `` ) and then a
2019-10-18 09:22:48 +02:00
:doc: `template cover </components/cover/template>` . (If you're new to Home Assistant, a 'cover' is
something like a window blind, a roller shutter, or a garage door.) The *state* of the template cover is
2018-06-01 18:10:00 +02:00
controlled by a template, or "lambda". In lambdas you're effectively writing C++ code and therefore the
name lambda is used instead of Home Assistant's "template" lingo to avoid confusion. But before you go
shy away from using lambdas because you just hear C++ and think oh noes, I'm not going down *that* road:
Writing lambdas is not that hard! Here's a bit of a primer:
2019-01-06 18:56:14 +01:00
First, you might have already wondered what the `` lambda: !lambda |- `` part is supposed to mean. `` !lambda ``
2019-02-16 23:25:23 +01:00
tells ESPHome that the following block is supposed to be interpreted as a lambda, or C++ code. Note that
2018-06-01 18:10:00 +02:00
here, the `` lambda: `` key would actually implicitly make the following block a lambda so in this context,
2019-01-06 18:56:14 +01:00
you could have just written `` lambda: |- `` .
2018-06-01 18:10:00 +02:00
2019-01-06 18:56:14 +01:00
Next, there's the weird `` |- `` character combination. This effectively tells the YAML parser to treat the following
2018-06-01 18:10:00 +02:00
**indented** (!) block as plaintext. Without it, the YAML parser would attempt to read the following block as if
2019-01-06 18:56:14 +01:00
it were made up of YAML keys like `` cover: `` for example. (You may also have seen variations of this like `` >- ``
2018-06-01 18:10:00 +02:00
or just `` | `` or `` > `` . There's a slight difference in how these different styles deal with whitespace, but for our
purposes we can ignore that).
With `` if (...) { ... } else { ... } `` we create a *condition* . What this effectively says that if the thing inside
2021-05-05 15:55:47 +02:00
the first parentheses evaluates to `` true `` then execute the first block (in this case `` return COVER_OPEN; `` ,
2018-06-01 18:10:00 +02:00
or else evaluate the second block. `` return ...; `` makes the code block give back a value to the template. In this case,
2019-05-12 22:44:59 +02:00
we're either *returning* `` COVER_OPEN `` or `` COVER_CLOSED `` to indicate that the cover is closed or open.
2018-06-01 18:10:00 +02:00
2019-02-16 23:25:23 +01:00
Finally, `` id(...) `` is a helper function that makes ESPHome fetch an object with the supplied ID (which you defined
2021-05-05 15:55:47 +02:00
somewhere else, like `` top_end_stop `` ) and lets you call any of ESPHome's many APIs directly. For example, here
2018-10-20 14:53:27 +02:00
we're retrieving the current state of the end stop using `` .state `` and using it to construct our cover state.
2018-06-01 18:10:00 +02:00
.. note ::
2019-02-16 23:25:23 +01:00
ESPHome does not check the validity of lambda expressions you enter and will blindly copy
2018-06-01 18:10:00 +02:00
them into the generated C++ code. If compilation fails or something else is not working as expected
with lambdas, it's always best to look at the generated C++ source file under `` <NODE_NAME>/src/main.cpp `` .
2018-08-22 22:05:28 +02:00
.. tip ::
To store local variables inside lambdas that retain their value across executions, you can create `` static ``
variables like so. In this example the variable `` num_executions `` is incremented by one each time the
lambda is executed and the current value is logged.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-08-22 22:05:28 +02:00
lambda: |-
static int num_executions = 0;
ESP_LOGD("main", "I am at execution number %d", num_executions);
num_executions += 1;
2018-06-01 18:10:00 +02:00
.. _config-templatable:
Bonus: Templating Actions
2018-10-12 16:33:22 +02:00
***** ***** ***** ***** *****
2018-06-01 18:10:00 +02:00
2019-02-16 23:25:23 +01:00
Another feature of ESPHome is that you can template almost every parameter for actions in automations. For example
2018-06-01 18:10:00 +02:00
if you have a light and want to set it to a pre-defined color when a button is pressed, you can do this:
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
on_press:
then:
- light.turn_on:
id: some_light_id
transition_length: 0.5s
red: 0.8
green: 1.0
2019-01-06 18:56:14 +01:00
blue: !lambda |-
2018-11-19 18:32:16 +01:00
// The sensor outputs values from 0 to 100. The blue
// part of the light color will be determined by the sensor value.
2018-10-20 14:53:27 +02:00
return id(some_sensor).state / 100.0;
2018-06-01 18:10:00 +02:00
Every parameter in actions that has the label "templatable" in the docs can be templated like above, using
all of the usual lambda syntax.
2018-11-12 23:31:22 +01:00
.. _config-globals:
2021-02-20 22:02:46 +01:00
Global Variables
----------------
2018-11-12 23:31:22 +01:00
In some cases you might require to share a global variable across multiple lambdas. For example,
global variables can be used to store the state of a garage door.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-11-12 23:31:22 +01:00
# Example configuration entry
globals:
- id: my_global_int
type: int
2019-01-27 12:28:01 +01:00
restore_value: no
2018-11-12 23:31:22 +01:00
initial_value: '0'
2022-04-14 05:57:29 +02:00
# Example for global string variable
- id: my_global_string
type: std::string
2023-09-11 23:26:03 +02:00
restore_value: yes
max_restore_data_length: 24
2022-10-18 21:05:18 +02:00
initial_value: '"Global value is"'
2018-11-12 23:31:22 +01:00
# In an automation
on_press:
then:
- lambda: |-
if (id(my_global_int) > 5) {
// global value is greater than 5
id(my_global_int) += 1;
} else {
id(my_global_int) += 10;
}
2023-07-18 07:00:50 +02:00
ESP_LOGD(TAG, "%s: %d", id(my_global_string).c_str(), id(my_global_int));
2018-11-12 23:31:22 +01:00
2021-02-20 22:02:46 +01:00
Configuration variables:
2018-11-12 23:31:22 +01:00
- **id** (**Required** , :ref: `config-id` ): Give the global variable an ID so that you can refer
to it later in :ref: `lambdas <config-lambda>` .
- **type** (**Required** , string): The C++ type of the global variable, for example `` bool `` (for `` true `` /`` false `` ),
`` int `` (for integers), `` float `` (for decimal numbers), `` int[50] `` for an array of 50 integers, etc.
2019-01-27 12:28:01 +01:00
- **restore_value** (*Optional* , boolean): Whether to try to restore the state on boot up.
2018-11-12 23:31:22 +01:00
Be careful: on the ESP8266, you only have a total of 96 bytes available for this! Defaults to `` no `` .
2020-12-21 01:55:44 +01:00
This will use storage in "RTC memory", so it won't survive a power-cycle unless you use the `` esp8266_restore_from_flash `` option to save to flash. See :doc: `esp8266_restore_from_flash </components/esphome>` for details.
2023-09-11 23:26:03 +02:00
- **max_restore_data_length** (*Optional* , integer): Only applies to variables of type `` std::string `` . ESPHome will allocate enough space for this many characters,
plus single character of overhead. Strings longer than this will not be saved. The max value of this variable is 254 characters, and the default is 63 characters.
2018-11-12 23:31:22 +01:00
- **initial_value** (*Optional* , string): The value with which to initialize this variable if the state
can not be restored or if state restoration is not enabled. This needs to be wrapped in quotes! Defaults to
the C++ default value for this type (for example `` 0 `` for integers).
2018-11-13 17:17:54 +01:00
.. _automation-networkless:
Do Automations Work Without a Network Connection
2021-02-20 22:02:46 +01:00
------------------------------------------------
2018-11-13 17:17:54 +01:00
2021-10-12 00:18:52 +02:00
YES! All automations you define in ESPHome are executed on the ESP itself and will continue to
2018-11-13 17:17:54 +01:00
work even if the WiFi network is down or the MQTT server is not reachable.
2019-02-16 23:25:23 +01:00
There is one caveat though: ESPHome automatically reboots if no connection to the MQTT broker can be
2018-11-13 17:17:54 +01:00
made. This is because the ESPs typically have issues in their network stacks that require a reboot to fix.
You can adjust this behavior (or even disable automatic rebooting) using the `` reboot_timeout `` option
2019-02-07 13:54:45 +01:00
in the :doc: `wifi component </components/wifi>` and :doc: `mqtt component </components/mqtt>` .
2019-07-28 12:41:15 +02:00
(Beware that effectively disables the reboot watchdog, so you will need to power cycle the device
if it fails to connect to the network without a reboot)
2018-11-13 17:17:54 +01:00
2018-06-01 18:10:00 +02:00
All Triggers
2018-10-12 16:33:22 +02:00
------------
2018-06-01 18:10:00 +02:00
2023-10-30 08:48:24 +01:00
- :ref: `api.services <api-services>` / :ref: `api.on_client_connected <api-on_client_connected_trigger>` / :ref: `api.on_client_disconnected <api-on_client_disconnected_trigger>`
2019-01-06 18:56:14 +01:00
- :ref: `sensor.on_value <sensor-on_value>` / :ref: `sensor.on_raw_value <sensor-on_raw_value>` / :ref: `sensor.on_value_range <sensor-on_value_range>`
2019-02-27 18:32:47 +01:00
- :ref: `binary_sensor.on_press <binary_sensor-on_press>` / :ref: `binary_sensor.on_release <binary_sensor-on_release>` /
:ref: `binary_sensor.on_state <binary_sensor-on_state>`
- :ref: `binary_sensor.on_click <binary_sensor-on_click>` / :ref: `binary_sensor.on_double_click <binary_sensor-on_double_click>` /
:ref: `binary_sensor.on_multi_click <binary_sensor-on_multi_click>`
2019-02-16 23:25:23 +01:00
- :ref: `esphome.on_boot <esphome-on_boot>` / :ref: `esphome.on_shutdown <esphome-on_shutdown>` / :ref: `esphome.on_loop <esphome-on_loop>`
2020-08-24 15:47:03 +02:00
- :ref: `light.on_turn_on / light.on_turn_off <light-on_turn_on_off_trigger>`
2019-10-24 14:19:43 +02:00
- :ref: `logger.on_message <logger-on_message>`
2021-01-18 13:35:20 +01:00
- :ref: `time.on_time <time-on_time>` / - :ref: `time.on_time_sync <time-on_time_sync>`
2022-06-08 12:33:29 +02:00
- :ref: `mqtt.on_message <mqtt-on_message>` / :ref: `mqtt.on_json_message <mqtt-on_json_message>` /
2022-06-03 03:51:54 +02:00
:ref: `mqtt.on_connect / mqtt.on_disconnect <mqtt-on_connect_disconnect>`
2023-04-27 00:47:52 +02:00
- :ref: `pn532.on_tag <pn532-on_tag>` / :ref: `pn532.on_tag_removed <pn532-on_tag_removed>` / :ref: `rc522.on_tag <rc522-on_tag>`
/ :ref: `rc522.on_tag_removed <rc522-on_tag_removed>` / :ref: `rdm6300.on_tag <rdm6300-on_tag>`
2019-01-09 15:05:26 +01:00
- :ref: `interval.interval <interval>`
2019-02-17 19:09:52 +01:00
- :ref: `switch.on_turn_on / switch.on_turn_off <switch-on_turn_on_off_trigger>`
2019-05-15 11:49:05 +02:00
- :doc: `remote_receiver.on_* </components/remote_receiver>`
- :doc: `sun.on_sunrise </components/sun>` / :doc: `sun.on_sunset </components/sun>`
2019-06-17 16:58:16 +02:00
- :ref: `sim800l.on_sms_received <sim800l-on_sms_received>`
2019-12-10 11:09:47 +01:00
- :ref: `rf_bridge.on_code_received <rf_bridge-on_code_received>`
2021-07-10 21:52:41 +02:00
- :ref: `ota.on_begin <ota-on_begin>` / :ref: `ota.on_progress <ota-on_progress>` /
:ref: `ota.on_end <ota-on_end>` / :ref: `ota.on_error <ota-on_error>` /
:ref: `ota.on_state_change <ota-on_state_change>`
2021-07-19 20:08:34 +02:00
- :ref: `display.on_page_change <display-on_page_change-trigger>`
2021-10-12 04:39:24 +02:00
- :ref: `cover.on_open <cover-on_open_trigger>` / :ref: `cover.on_closed <cover-on_closed_trigger>`
2023-11-07 06:17:18 +01:00
- :ref: `wifi.on_connect / wifi.on_disconnect <wifi-on_connect_disconnect>`
2018-06-01 18:10:00 +02:00
All Actions
2018-10-12 16:33:22 +02:00
-----------
2018-06-01 18:10:00 +02:00
- :ref: `delay <delay_action>`
- :ref: `lambda <lambda_action>`
2019-10-19 22:05:40 +02:00
- :ref: `if <if_action>` / :ref: `while <while_action>` / :ref: `wait_until <wait_until_action>`
2018-10-20 12:52:47 +02:00
- :ref: `component.update <component-update_action>`
2023-10-02 22:25:16 +02:00
- :ref: `component.suspend <component-suspend_action>` / :ref: `component.resume <component-resume_action>`
2019-10-19 16:32:55 +02:00
- :ref: `script.execute <script-execute_action>` / :ref: `script.stop <script-stop_action>` / :ref: `script.wait <script-wait_action>`
2018-10-20 15:19:31 +02:00
- :ref: `logger.log <logger-log_action>`
2019-01-06 18:56:14 +01:00
- :ref: `homeassistant.service <api-homeassistant_service_action>`
2022-04-14 05:58:59 +02:00
- :ref: `homeassistant.event <api-homeassistant_event_action>`
- :ref: `homeassistant.tag_scanned <api-homeassistant_tag_scanned_action>`
2019-01-06 18:56:14 +01:00
- :ref: `mqtt.publish <mqtt-publish_action>` / :ref: `mqtt.publish_json <mqtt-publish_json_action>`
- :ref: `switch.toggle <switch-toggle_action>` / :ref: `switch.turn_off <switch-turn_off_action>` / :ref: `switch.turn_on <switch-turn_on_action>`
- :ref: `light.toggle <light-toggle_action>` / :ref: `light.turn_off <light-turn_off_action>` / :ref: `light.turn_on <light-turn_on_action>`
2019-05-15 11:49:05 +02:00
/ :ref: `light.control <light-control_action>` / :ref: `light.dim_relative <light-dim_relative_action>`
/ :ref: `light.addressable_set <light-addressable_set_action>`
- :ref: `cover.open <cover-open_action>` / :ref: `cover.close <cover-close_action>` / :ref: `cover.stop <cover-stop_action>` /
:ref: `cover.control <cover-control_action>`
2019-01-06 18:56:14 +01:00
- :ref: `fan.toggle <fan-toggle_action>` / :ref: `fan.turn_off <fan-turn_off_action>` / :ref: `fan.turn_on <fan-turn_on_action>`
- :ref: `output.turn_off <output-turn_off_action>` / :ref: `output.turn_on <output-turn_on_action>` / :ref: `output.set_level <output-set_level_action>`
2022-05-11 02:47:56 +02:00
- :ref: `deep_sleep.enter <deep_sleep-enter_action>` / :ref: `deep_sleep.prevent <deep_sleep-prevent_action>` / :ref: `deep_sleep.allow <deep_sleep-allow_action>`
2019-05-15 11:49:05 +02:00
- :ref: `sensor.template.publish <sensor-template-publish_action>` / :ref: `binary_sensor.template.publish <binary_sensor-template-publish_action>`
/ :ref: `cover.template.publish <cover-template-publish_action>` / :ref: `switch.template.publish <switch-template-publish_action>`
/ :ref: `text_sensor.template.publish <text_sensor-template-publish_action>`
2019-03-13 16:17:05 +01:00
- :ref: `stepper.set_target <stepper-set_target_action>` / :ref: `stepper.report_position <stepper-report_position_action>`
2019-05-15 11:49:05 +02:00
/ :ref: `stepper.set_speed <stepper-set_speed_action>`
- :ref: `servo.write <servo-write_action>` / :ref: `servo.detach <servo-detach_action>`
2022-08-09 02:19:45 +02:00
- :ref: `sprinkler.start_full_cycle <sprinkler-controller-action_start_full_cycle>` / :ref: `sprinkler.start_from_queue <sprinkler-controller-action_start_from_queue>` /
:ref: `sprinkler.start_single_valve <sprinkler-controller-action_start_single_valve>` / :ref: `sprinkler.shutdown <sprinkler-controller-action_shutdown>` /
:ref: `sprinkler.next_valve <sprinkler-controller-action_next_valve>` / :ref: `sprinkler.previous_valve <sprinkler-controller-action_previous_valve>` /
:ref: `sprinkler.pause <sprinkler-controller-action_pause>` / :ref: `sprinkler.resume <sprinkler-controller-action_resume>` /
:ref: `sprinkler.resume_or_start_full_cycle <sprinkler-controller-action_resume_or_start_full_cycle>` / :ref: `sprinkler.queue_valve <sprinkler-controller-action_queue_valve>` /
:ref: `sprinkler.clear_queued_valves <sprinkler-controller-action_clear_queued_valves>` / :ref: `sprinkler.set_multiplier <sprinkler-controller-action_set_multiplier>` /
2023-02-22 02:48:02 +01:00
:ref: `sprinkler.set_repeat <sprinkler-controller-action_set_repeat>` / :ref: `sprinkler.set_divider <sprinkler-controller-action_set_divider>` /
:ref: `sprinkler.set_valve_run_duration <sprinkler-controller-action_set_valve_run_duration>`
2019-05-15 11:49:05 +02:00
- :ref: `globals.set <globals-set_action>`
- :ref: `remote_transmitter.transmit_* <remote_transmitter-transmit_action>`
- :ref: `climate.control <climate-control_action>`
2019-11-01 18:28:26 +01:00
- :ref: `output.esp8266_pwm.set_frequency <output-esp8266_pwm-set_frequency_action>` / :ref: `output.ledc.set_frequency <output-ledc-set_frequency_action>`
2019-05-15 11:49:05 +02:00
- :ref: `sensor.integration.reset <sensor-integration-reset_action>`
- :ref: `display.page.show_* <display-pages>`
2019-05-29 19:28:30 +02:00
- :ref: `uart.write <uart-write_action>`
2019-06-17 16:58:16 +02:00
- :ref: `sim800l.send_sms <sim800l-send_sms_action>`
2019-08-27 19:40:52 +02:00
- :ref: `mhz19.calibrate_zero <mhz19-calibrate_zero_action>` / :ref: `mhz19.abc_enable <mhz19-abc_enable_action>` / :ref: `mhz19.abc_disable <mhz19-abc_disable_action>`
2019-11-01 18:28:26 +01:00
- :ref: `sensor.rotary_encoder.set_value <sensor-rotary_encoder-set_value_action>`
2019-11-09 18:39:13 +01:00
- :ref: `http_request.get <http_request-get_action>` / :ref: `http_request.post <http_request-post_action>` / :ref: `http_request.send <http_request-send_action>`
2019-12-10 11:09:47 +01:00
- :ref: `rf_bridge.send_code <rf_bridge-send_code_action>`
- :ref: `rf_bridge.learn <rf_bridge-learn_action>`
2021-01-18 13:35:20 +01:00
- :ref: `ds1307.read_time <ds1307-read_time_action>` / :ref: `ds1307.write_time <ds1307-write_time_action>`
2022-12-23 00:33:58 +01:00
- :ref: `pcf85063.read_time <pcf85063-read_time_action>` / :ref: `pcf85063.write_time <pcf85063-write_time_action>`
2021-05-31 06:07:39 +02:00
- :ref: `cs5460a.restart <cs5460a-restart_action>`
2022-01-10 11:13:43 +01:00
- :ref: `pzemac.reset_energy <pzemac-reset_energy_action>`
2022-05-10 06:59:00 +02:00
- :ref: `number.set <number-set_action>` / :ref: `number.to_min <number-to-min_action>` / :ref: `number.to_max <number-to-max_action>` / :ref: `number.decrement <number-decrement_action>` / :ref: `number.increment <number-increment_action>` / :ref: `number.operation <number-operation_action>`
2022-05-11 02:47:56 +02:00
- :ref: `select.set <select-set_action>` / :ref: `select.set_index <select-set_index_action>` / :ref: `select.first <select-first_action>` / :ref: `select.last <select-last_action>` / :ref: `select.previous <select-previous_action>` / :ref: `select.next <select-next_action>` / :ref: `select.operation <select-operation_action>`
2022-06-08 12:33:29 +02:00
- :ref: `media_player.play <media_player-play>` / :ref: `media_player.pause <media_player-pause>` / :ref: `media_player.stop <media_player-stop>` / :ref: `media_player.toggle <media_player-toggle>`
2022-06-13 03:29:35 +02:00
/ :ref: `media_player.volume_up <media_player-volume_up>` / :ref: `media_player.volume_down <media_player-volume_down>` / :ref: `media_player.volume_set <media_player-volume_set>`
2022-08-08 03:00:00 +02:00
- :ref: `ble_client.ble_write <ble_client-ble_write_action>`
2018-06-01 18:10:00 +02:00
2018-12-05 10:19:48 +01:00
.. _config-condition:
All Conditions
--------------
- :ref: `lambda <lambda_condition>`
2023-10-03 11:58:14 +02:00
- :ref: `and <and_condition>` / :ref: `or <or_condition>` / :ref: `xor <xor_condition>` / :ref: `not <not_condition>`
2019-05-15 11:49:05 +02:00
- :ref: `for <for_condition>`
2019-01-06 18:56:14 +01:00
- :ref: `binary_sensor.is_on <binary_sensor-is_on_condition>` / :ref: `binary_sensor.is_off <binary_sensor-is_off_condition>`
- :ref: `switch.is_on <switch-is_on_condition>` / :ref: `switch.is_off <switch-is_off_condition>`
2018-12-05 10:19:48 +01:00
- :ref: `sensor.in_range <sensor-in_range_condition>`
2019-05-15 11:49:05 +02:00
- :ref: `wifi.connected <wifi-connected_condition>` / :ref: `api.connected <api-connected_condition>`
/ :ref: `mqtt.connected <mqtt-connected_condition>`
2020-10-06 02:55:53 +02:00
- :ref: `time.has_time <time-has_time_condition>`
2019-05-15 11:49:05 +02:00
- :ref: `script.is_running <script-is_running_condition>`
- :ref: `sun.is_above_horizon / sun.is_below_horizon <sun-is_above_below_horizon-condition>`
- :ref: `text_sensor.state <text_sensor-state_condition>`
2019-09-27 23:07:17 +02:00
- :ref: `light.is_on <light-is_on_condition>` / :ref: `light.is_off <light-is_off_condition>`
2021-04-03 04:00:51 +02:00
- :ref: `display.is_displaying_page <display-is_displaying_page-condition>`
2021-07-12 21:20:18 +02:00
- :ref: `number.in_range <number-in_range_condition>`
2021-09-02 02:16:15 +02:00
- :ref: `fan.is_on <fan-is_on_condition>` / :ref: `fan.is_off <fan-is_off_condition>`
2018-12-05 10:19:48 +01:00
2019-04-09 14:31:01 +02:00
All Lambda Calls
----------------
2019-04-15 21:50:51 +02:00
2019-04-09 14:31:01 +02:00
- :ref: `Sensor <sensor-lambda_calls>`
- :ref: `Binary Sensor <binary_sensor-lambda_calls>`
- :ref: `Switch <switch-lambda_calls>`
2019-04-15 22:06:35 +02:00
- :ref: `Display <display-engine>`
2019-04-09 14:31:01 +02:00
- :ref: `Cover <cover-lambda_calls>`
- :ref: `Text Sensor <text_sensor-lambda_calls>`
- :ref: `Stepper <stepper-lambda_calls>`
2021-07-12 21:20:18 +02:00
- :ref: `Number <number-lambda_calls>`
2019-04-09 14:31:01 +02:00
2018-06-01 18:10:00 +02:00
.. _delay_action:
2018-10-12 16:33:22 +02:00
`` delay `` Action
----------------
2018-06-01 18:10:00 +02:00
This action delays the execution of the next action in the action list by a specified
time period.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
2019-02-27 18:32:47 +01:00
on_...:
then:
2018-11-10 14:33:24 +01:00
- switch.turn_on: relay_1
2018-06-01 18:10:00 +02:00
- delay: 2s
2018-11-10 14:33:24 +01:00
- switch.turn_off: relay_1
2018-06-01 18:10:00 +02:00
# Templated, waits for 1s (1000ms) only if a reed switch is active
2018-10-20 14:53:27 +02:00
- delay: !lambda "if (id(reed_switch).state) return 1000; else return 0;"
2018-06-01 18:10:00 +02:00
.. note ::
This is a "smart" asynchronous delay - other code will still run in the background while
2021-08-10 13:37:43 +02:00
the delay is happening. When using a lambda call, you should return the delay value in milliseconds.
2018-06-01 18:10:00 +02:00
.. _lambda_action:
2018-10-12 16:33:22 +02:00
`` lambda `` Action
-----------------
2018-06-01 18:10:00 +02:00
This action executes an arbitrary piece of C++ code (see :ref: `Lambda <config-lambda>` ).
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-01 18:10:00 +02:00
on_...:
then:
2018-11-19 18:32:16 +01:00
- lambda: |-
2018-06-01 18:10:00 +02:00
id(some_binary_sensor).publish_state(false);
2019-01-06 18:56:14 +01:00
.. _lambda_condition:
`` lambda `` Condition
--------------------
This condition performs an arbitrary piece of C++ code (see :ref: `Lambda <config-lambda>` )
and can be used to create conditional flow in actions.
.. code-block :: yaml
on_...:
then:
- if:
condition:
# Should return either true or false
lambda: |-
return id(some_sensor).state < 30;
# ...
.. _and_condition:
.. _or_condition:
2023-10-03 11:58:14 +02:00
.. _xor_condition:
2019-05-15 11:49:05 +02:00
.. _not_condition:
2019-01-06 18:56:14 +01:00
2023-10-03 11:58:14 +02:00
`` and `` / `` or `` / `` xor `` / `` not `` Condition
----------------------------------------------
2019-01-06 18:56:14 +01:00
Check a combination of conditions
.. code-block :: yaml
on_...:
then:
- if:
condition:
2023-10-03 11:58:14 +02:00
# Same syntax for `and` as well as `xor` conditions
2019-01-06 18:56:14 +01:00
or:
- binary_sensor.is_on: some_binary_sensor
- binary_sensor.is_on: other_binary_sensor
# ...
2019-05-15 11:49:05 +02:00
- if:
condition:
not:
binary_sensor.is_off: some_binary_sensor
2018-08-22 22:05:28 +02:00
.. _if_action:
2018-10-12 16:33:22 +02:00
`` if `` Action
-------------
2018-06-01 18:10:00 +02:00
2018-08-22 22:05:28 +02:00
This action first evaluated a certain condition (`` if: `` ) and then either
executes the `` then: `` branch or the `` else: `` branch depending on the output of the condition.
2018-06-13 22:38:49 +02:00
2018-08-22 22:05:28 +02:00
After the chosen branch (`` then `` or `` else `` ) is done with execution, the next action is performed.
For example below you can see an automation that checks if a sensor value is below 30 and if so
turns on a light for 5 seconds. Otherwise, the light is turned off immediately.
2018-06-13 22:38:49 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-06-13 22:38:49 +02:00
on_...:
then:
2018-08-22 22:05:28 +02:00
- if:
2018-10-26 23:13:23 +02:00
condition:
lambda: 'return id(some_sensor).state < 30;'
then:
2019-01-06 18:56:14 +01:00
- logger.log: "The sensor value is below 30!"
2018-10-26 23:13:23 +02:00
- light.turn_on: my_light
- delay: 5s
else:
2019-01-06 18:56:14 +01:00
- logger.log: "The sensor value is above 30!"
2018-08-22 22:05:28 +02:00
- light.turn_off: my_light
2021-02-20 22:02:46 +01:00
Configuration variables:
2018-08-22 22:05:28 +02:00
2019-01-06 18:56:14 +01:00
- **condition** (**Required** , :ref: `config-condition` ): The condition to check which branch to take. See :ref: `Conditions <config-condition>` .
2021-11-28 19:57:01 +01:00
- **then** (*Optional* , :ref: `Action <config-action>` ): The action to perform if the condition evaluates to true.
2018-08-22 22:05:28 +02:00
Defaults to doing nothing.
2021-11-28 19:57:01 +01:00
- **else** (*Optional* , :ref: `Action <config-action>` ): The action to perform if the condition evaluates to false.
2018-08-22 22:05:28 +02:00
Defaults to doing nothing.
2018-12-05 10:19:48 +01:00
.. _while_action:
`` while `` Action
----------------
2023-10-25 16:51:24 +02:00
This action is similar to the :ref: `if <if_action>` Action. The `` while `` action loops
through a block as long as the given condition is true.
2018-12-05 10:19:48 +01:00
.. code-block :: yaml
2019-01-06 18:56:14 +01:00
# In a trigger:
on_...:
2018-12-05 10:19:48 +01:00
- while:
condition:
2019-01-06 18:56:14 +01:00
binary_sensor.is_on: some_binary_sensor
2018-12-05 10:19:48 +01:00
then:
2019-01-06 18:56:14 +01:00
- logger.log: "Still executing"
- light.toggle: some_light
- delay: 5s
2018-12-05 10:19:48 +01:00
2021-02-20 22:02:46 +01:00
Configuration variables:
2018-12-05 10:19:48 +01:00
2019-01-06 18:56:14 +01:00
- **condition** (**Required** ): The condition to check whether to execute. See :ref: `Conditions <config-condition>` .
2021-11-28 19:57:01 +01:00
- **then** (**Required** , :ref: `Action <config-action>` ): The action to perform until the condition evaluates to false.
2018-06-13 22:38:49 +02:00
2021-11-10 22:08:37 +01:00
.. _repeat_action:
`` repeat `` Action
-----------------
This action allows you to repeat a block a given number of times.
For example, the automation below will flash the light five times.
.. code-block :: yaml
on_...:
- repeat:
count: 5
then:
- light.turn_on: some_light
- delay: 1s
- light.turn_off: some_light
- delay: 10s
Configuration variables:
2021-11-28 19:57:01 +01:00
- **count** (**Required** , int): The number of times the action should be repeated.
- **then** (**Required** , :ref: `Action <config-action>` ): The action to repeat.
2021-11-10 22:08:37 +01:00
2019-02-17 19:16:54 +01:00
.. _wait_until_action:
`` wait_until `` Action
---------------------
This action allows your automations to wait until a condition evaluates to true. (So this is just
2019-10-18 09:22:48 +02:00
a shorthand way of writing a `` while `` action with an empty `` then `` block.)
2019-02-17 19:16:54 +01:00
.. code-block :: yaml
# In a trigger:
on_...:
- logger.log: "Waiting for binary sensor"
- wait_until:
binary_sensor.is_on: some_binary_sensor
- logger.log: "Binary sensor is ready"
2022-05-10 23:38:07 +02:00
If you want to use a timeout, the term "condition" is required:
.. code-block :: yaml
# In a trigger:
on_...:
- logger.log: "Waiting for binary sensor"
- wait_until:
condition:
binary_sensor.is_on: some_binary_sensor
timeout: 8s
- logger.log: "Binary sensor might be ready"
2021-10-12 21:23:38 +02:00
Configuration variables:
- **condition** (**Required** ): The condition to wait to become true. See :ref: `Conditions <config-condition>` .
- **timeout** (*Optional* , :ref: `config-time` ): Time to wait before timing out. Defaults to never timing out.
2019-02-17 19:16:54 +01:00
2018-10-20 12:52:47 +02:00
.. _component-update_action:
`` component.update `` Action
---------------------------
Using this action you can manually call the `` update() `` method of a component.
Please note that this only works with some component types and others will result in a
compile error.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-10-20 12:52:47 +02:00
on_...:
then:
- component.update: my_component
# The same as:
- lambda: 'id(my_component).update();'
2023-10-02 22:25:16 +02:00
.. _component-suspend_action:
`` component.suspend `` Action
----------------------------
Using this action you can manually call the `` stop_poller() `` method of a component.
After this action the component will stop being refreshed.
While the poller is suspendend, it's still possible to trigger on-demand updates by
using :ref: `component.update <component-update_action>`
Please note that this only works with PollingComponent types and others will result in a
compile error.
.. code-block :: yaml
on_...:
then:
- component.suspend: my_component
# The same as:
- lambda: 'id(my_component).stop_poller();'
.. _component-resume_action:
`` component.resume `` Action
---------------------------
Using this action you can manually call the `` start_poller() `` method of a component.
After this action the component will refresh at the original update_interval rate
This will allow the component to resume automatic update at the defined interval.
This action also allows to change the update interval, calling it without suspend,
replace the poller directly.
Please note that this only works with PollingComponent types and others will result in a
compile error.
.. code-block :: yaml
on_...:
then:
- component.resume: my_component
# The same as:
- lambda: 'id(my_component).start_poller();'
# Change the poller interval
on_...:
then:
- component.resume:
id: my_component
update_interval: 15s
2019-05-15 11:49:05 +02:00
.. _globals-set_action:
2019-05-12 22:44:59 +02:00
`` globals.set `` Action
----------------------
This :ref: `Action <config-action>` allows you to change the value of a :ref: `global <config-globals>`
variable without having to go through the lambda syntax.
.. code-block :: yaml
on_...:
- globals.set:
id: my_global_var
value: '10'
Configuration variables:
- **id** (**Required** , :ref: `config-id` ): The :ref: `config-id` of the global variable to set.
- **value** (**Required** , :ref: `templatable <config-templatable>` ): The value to set the global
variable to.
2018-10-20 12:52:47 +02:00
2021-02-20 22:02:46 +01:00
`` script `` Component
--------------------
2018-10-20 12:52:47 +02:00
2020-07-25 14:20:58 +02:00
With the `` script: `` component you can define a list of steps in a central place, and then
2018-10-20 12:52:47 +02:00
execute the script with a single call.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-10-20 12:52:47 +02:00
# Example configuration entry
script:
- id: my_script
then:
- switch.turn_on: my_switch
- delay: 1s
- switch.turn_off: my_switch
2020-07-25 14:20:58 +02:00
2021-02-20 22:02:46 +01:00
Configuration variables:
2020-07-25 14:20:58 +02:00
- **id** (**Required** , :ref: `config-id` ): The :ref: `config-id` of the script. Use this
to interact with the script using the script actions.
- **mode** (*Optional* , string): Controls what happens when a script is
invoked while it is still running from one or more previous invocations. Default to `` single `` .
- `` single `` : Do not start a new run. Issue a warning.
- `` restart `` : Start a new run after first stopping previous run.
- `` queued `` : Start a new run after previous runs complete.
- `` parallel `` : Start a new, independent run in parallel with previous runs.
2021-11-28 19:57:01 +01:00
- **max_runs** (*Optional* , int): Allows limiting the maxiumun number of runs when using script
2020-07-25 14:20:58 +02:00
modes `` queued `` and `` parallel `` , use value `` 0 `` for unlimited runs. Defaults to `` 0 `` .
2022-12-23 00:33:58 +01:00
- **parameters** (*Optional* , :ref: `Script Parameters <script-parameters>` ): A script can define one
or more parameters that must be provided in order to execute. All parameters defined here are
2022-11-09 04:52:04 +01:00
mandatory and must be given when calling the script.
2021-11-28 19:57:01 +01:00
- **then** (**Required** , :ref: `Action <config-action>` ): The action to perform.
2020-07-25 14:20:58 +02:00
2022-11-09 04:52:04 +01:00
.. _script-parameters:
`` Script Parameters ``
---------------------
2022-12-23 00:33:58 +01:00
Scripts can be defined with parameters. The arguments given when calling the script can be used within
2022-11-09 04:52:04 +01:00
the script's lambda actions. To define the parameters, add the parameter names under `parameters:` key
and specify the data type for that parameter.
Supported data types:
* `bool` : A boolean true/false. C++ type: `bool`
* `int` : An integer. C++ type: `int32_t`
* `float` : A floating point number. C++ type: `float`
* `string` : A string. C++ type: `std::string`
Each of these also exist in array form:
* `bool[]` : An array of boolean values. C++ type: `std::vector<bool>`
* Same for other types.
.. code-block :: yaml
script:
- id: blink_light
parameters:
delay_ms: int
then:
- light.turn_on: status_light
# The param delay_ms is accessible using a lambda
- delay: !lambda return delay_ms;
- light.turn_off: status_light
2020-07-25 14:20:58 +02:00
.. _script-execute_action:
`` script.execute `` Action
-------------------------
This action executes the script. The script **mode** dictates what will happen if the
script was already running.
.. code-block :: yaml
2018-10-20 12:52:47 +02:00
# in a trigger:
on_...:
then:
- script.execute: my_script
2022-12-23 00:33:58 +01:00
2022-11-09 04:52:04 +01:00
# Calling a non-parameterised script in a lambda
- lambda: id(my_script).execute();
2018-10-20 12:52:47 +02:00
2022-11-09 04:52:04 +01:00
# Calling a script with parameters
- script.execute:
id: blink_light
delay_ms: 500
2022-07-12 23:53:40 +02:00
2022-11-09 04:52:04 +01:00
# Calling a parameterised script inside a lambda
- lambda: id(blink_light)->execute(1000);
2022-07-12 23:53:40 +02:00
2018-12-05 10:19:48 +01:00
.. _script-stop_action:
`` script.stop `` Action
----------------------
2019-01-06 18:56:14 +01:00
This action allows you to stop a given script during execution. If the
2019-10-18 09:22:48 +02:00
script is not running, it does nothing.
2023-10-08 06:18:45 +02:00
This is useful if you want to stop a script that contains a
2020-07-25 14:20:58 +02:00
`` delay `` action, `` wait_until `` action, or is inside a `` while `` loop, etc.
You can also call this action from the script itself, and any subsequent action
will not be executed.
2018-12-05 10:19:48 +01:00
.. code-block :: yaml
# Example configuration entry
script:
- id: my_script
then:
- switch.turn_on: my_switch
- delay: 1s
- switch.turn_off: my_switch
# in a trigger:
on_...:
then:
- script.stop: my_script
2022-07-12 23:53:40 +02:00
or as lambda
2022-12-23 00:33:58 +01:00
.. code-block :: yaml
2022-07-12 23:53:40 +02:00
2022-07-20 12:10:05 +02:00
lambda: 'id(my_script).stop();'
2022-07-12 23:53:40 +02:00
2019-10-19 16:32:55 +02:00
.. _script-wait_action:
`` script.wait `` Action
----------------------
This action suspends execution of the automation until a script has finished executing.
2020-07-25 14:20:58 +02:00
Note: If no script is executing, this will continue immediately. If multiple instances
of the script are running in parallel, this will block until all of them have terminated.
2019-10-19 16:32:55 +02:00
.. code-block :: yaml
# Example configuration entry
script:
- id: my_script
then:
- switch.turn_on: my_switch
- delay: 1s
- switch.turn_off: my_switch
# in a trigger:
on_...:
then:
- script.execute: my_script
- script.wait: my_script
2022-10-26 23:10:27 +02:00
This can't be used in a lambda as it would block all functioning of the device. The script wouldn't even get to run.
2022-07-12 23:53:40 +02:00
2019-05-12 22:44:59 +02:00
.. _script-is_running_condition:
`` script.is_running `` Condition
-------------------------------
This :ref: `condition <config-condition>` allows you to check if a given script is running.
2020-07-25 14:20:58 +02:00
In case scripts are run in `` parallel `` , this condition only tells you if at least one script
2022-09-05 20:23:00 +02:00
of the given id is running, not how many. Not designed for use with :ref: `while <while_action>` , instead try :ref: `script.wait <script-wait_action>` .
2019-05-12 22:44:59 +02:00
.. code-block :: yaml
on_...:
if:
condition:
- script.is_running: my_script
then:
- logger.log: Script is running!
2022-07-12 23:53:40 +02:00
or as lambda
.. code-block :: yaml
2023-01-12 02:24:47 +01:00
lambda: |-
2022-12-22 14:55:37 +01:00
if (id(my_script).is_running()) {
2022-07-12 23:53:40 +02:00
ESP_LOGI("main", "Script is running!");
}
2019-05-12 22:44:59 +02:00
.. _for_condition:
`` for `` Condition
-----------------
This :ref: `Condition <config-condition>` allows you to check if a given condition has been
true for at least a given amount of time.
.. code-block :: yaml
on_...:
if:
condition:
for:
time: 5min
condition:
api.connected:
then:
- logger.log: API has stayed connected for at least 5 minutes!
Configuration variables:
- **time** (**Required** , :ref: `templatable <config-templatable>` , :ref: `config-time` ):
The time for which the condition has to have been true.
- **condition** (**Required** , :ref: `Condition <config-condition>` ):
The condition to check.
2019-01-09 15:05:26 +01:00
.. _interval:
2021-02-20 22:02:46 +01:00
`` interval `` Component
----------------------
2019-01-09 15:05:26 +01:00
2019-10-18 09:22:48 +02:00
This component allows you to run actions at fixed time intervals.
2019-01-09 15:05:26 +01:00
For example if you want to toggle a switch every minute, you can use this component.
2019-10-18 09:22:48 +02:00
Please note that it's possible to achieve the same thing with the :ref: `time.on_time <time-on_time>`
trigger, but this technique is more light-weight and user-friendly.
2019-01-09 15:05:26 +01:00
.. code-block :: yaml
# Example configuration entry
interval:
- interval: 1min
then:
- switch.toggle: relay_1
2021-02-20 22:02:46 +01:00
Configuration variables:
2019-01-09 15:05:26 +01:00
- **interval** (**Required** , :ref: `config-time` ): The interval to execute the action with.
2021-11-28 19:57:01 +01:00
- **then** (**Required** , :ref: `Action <config-action>` ): The action to perform.
2019-01-09 15:05:26 +01:00
2020-07-25 14:20:58 +02:00
Timers and timeouts
-------------------
While ESPHome does not provide a construction for timers, you can easily implement them by
combining `` script `` and `` delay `` . You can have an absolute timeout or sliding timeout by
using script modes `` single `` and `` restart `` respectively.
.. code-block :: yaml
script:
2021-06-14 14:05:12 +02:00
- id: hallway_light_script
mode: restart # Light will be kept on during 1 minute since
# the latest time the script is executed
then:
- light.turn_on: hallway_light
- delay: 1 min
- light.turn_off: hallway_light
2020-07-25 14:20:58 +02:00
...
2021-01-11 17:46:37 +01:00
on_...: # can be called from different wall switches
2020-07-25 14:20:58 +02:00
- script.execute: hallway_light_script
Sometimes you'll also need a timer which does not perform any action, that is ok too, just
use a single `` delay `` action, then in your automation check `` script.is_running `` condition
to know if your *timer* is going or due.
2018-06-01 18:10:00 +02:00
See Also
2018-10-12 16:33:22 +02:00
--------
2018-06-01 18:10:00 +02:00
- :doc: `configuration-types`
- :doc: `faq`
2019-02-07 13:54:45 +01:00
- :ghedit: `Edit`