Switch Component

The switch domain includes all platforms that should show up like a switch and can only be turned ON or OFF.

Base Switch Configuration

switch:
  - platform: ...
    name: "Switch Name"
    icon: "mdi:restart"

Configuration variables:

  • name (Required, string): The name of the switch.
  • icon (Optional, icon): Manually set the icon to use for the sensor in the frontend.
  • inverted (Optional, boolean): Whether to invert the binary state, i.e. report ON states as OFF and vice versa. Defaults to False.
  • All other options from MQTT Component.

switch.toggle Action

This action toggles a switch with the given ID when executed.

on_...:
  then:
    - switch.toggle: relay_1

switch.turn_on Action

This action turns a switch with the given ID on when executed.

on_...:
  then:
    - switch.turn_on: relay_1

switch.turn_off Action

This action turns a switch with the given ID off when executed.

on_...:
  then:
    - switch.turn_off: relay_1

switch.is_on / switch.is_off Condition

This Condition checks if the given switch is ON (or OFF).

# In some trigger:
on_...:
  if:
    condition:
      # Same syntax for is_off
      switch.is_on: my_switch

lambda calls

From lambdas, you can call several methods on all covers to do some advanced stuff (see the full API Reference for more info).

  • publish_state(): Manually cause the switch to publish a new state and store it internally. If it’s different from the last internal state, it’s additionally published to the frontend.

    // Within lambda, make the switch report a specific state
    id(my_switch).publish_state(false);
    id(my_switch).publish_state(true);
    
  • state: Retrieve the current state of the switch.

    // Within lambda, get the switch state and conditionally do something
    if (id(my_switch).state) {
      // Switch is ON, do something here
    } else {
      // Switch is OFF, do something else here
    }
    
  • turn_off()/turn_on: Manually turn the switch ON/OFF from code. Similar to the switch.turn_on and switch.turn_off actions, but can be used in complex lambda expressions.

    id(my_switch).turn_off();
    id(my_switch).turn_on(true);
    // Toggle the switch
    id(my_switch).toggle();
    

switch.is_on / switch.is_off Condition

This condition passes if the given switch is on/off.

# in a trigger:
on_...:
  if:
    condition:
      switch.is_on: my_switch
      # same goes for is_off
    then:
    - script.execute: my_script