Skip to content

Buttons and switches: why the difference and how to configure them

A typical device usually has at least one button (exception being bulbs and some lights) to control the power state(s). Additional buttons and switches can be wired to a free GPIO and configured in Module or Template settings.

Note

Other than relays/lights, Tasmota does not publish the state of components (switches, buttons, sensors, etc.) in real-time. State of components is transmitted automatically each TelePeriod via the SENSORS message.

Button vs. Switch~

A button (also called a push-button) is a momentary or non-latching switch which causes a temporary change in the state of an electrical circuit only while the switch is pressed. An automatic mechanism (i.e. a spring) returns the switch to its default position immediately afterwards, restoring the initial circuit condition.

A switch (more precisely a latching or toggle switch), when activated by the user, remains in that state until activated again.

Learn more about buttons and switches in this video.

Both have a similar function, but Tasmota distinguishes between a "Button" and a "Switch" in other ways.

Switch~

In Tasmota a Switch is any switch or push-button additionally connected to a free GPIO. Some possibilities include:

By default a switch toggles the corresponding power state (f.e. Switch1 controls Power1). Every time the switch gets flipped the power state changes (ON or OFF). Instead of the default toggling of the power state, switches can be configured to send messages to different MQTT topics or send commands to other Tasmota devices. To ignore the default behaviour define a rule which triggers on Switch<x>#State or use Switchtopic. Take note: If the rule matches only certain states, default switch behaviour is supressed only for those states.

Example

Make Switch1 publish its value to cmnd/custom-topic/SWITCH and not control Power1

Backlog SwitchMode 1; SwitchTopic 0
Rule1 on Switch1#state do Publish cmnd/custom-topic/SWITCH %value% endon
Rule1 1

Warning

If you define a switch with a number higher than available power outputs it will default to controlling Power1. Example: Switch4 on a device with Power1 and Power2 will control Power1.

And now, to make everything completely confusing:
A push-button can be configured as a Switch and a toggle switch can be configured as a Button. Configuring a toggle switch as a Button is not recommended!

SwitchMode~

To change the behavior of a physical input peripheral configured as a Tasmota Switch<x> component, whether a toggle switch or a momentary switch (i.e., a push-button), use the SwitchMode command. If there is more than one Switch<x> component, use SwitchMode<x> where <x> is the number of your switch from the Tasmota GPIO configuration.

SwitchMode, as the name implies, applies ONLY to GPIO configured in Tasmota as a Switch<x> component (9-16 & 82-89). SwitchMode has NO impact on the behavior of GPIO configured as Button<x> components (17-20, 90-93, & 112-129). SwitchMode sets the desired behavior of a Switch<x> component based on whether it's a switch or a push-button (i.e., a momentary switch) that is physically connected to the GPIO.

SwitchMode 0
Default mode

Set switch to toggle mode (0 = TOGGLE, 1 = TOGGLE).

Tasmota sends TOGGLE command each time the state of the circuit changes (closing or opening). In case of a push button attached Tasmota will send a TOGGLE command when pressed and another TOGGLE command when released.

Example

When the button is pressed, toggle the power to ring the doorbell; when the button is released, ring the doorbell again.

SwitchMode 1
Set switch to follow mode (0 = OFF, 1 = ON)

At the time when the circuit is closed, Tasmota will send ON and opening the circuit sends OFF.

Tip

You want to use SwitchMode 1 when connecting a toggle switch (e.g. a classic light switch) to your device. This way the "software switch" will mirror the state of the "hardware switch". If the real switch is in the "ON" position, the state in Tasmota is ON as well.

SwitchMode 2
Set switch to inverted follow mode (0 = ON, 1 = OFF)

At the time when the circuit is closed, Tasmota will send OFF and opening the circuit sends ON.

Tip

When connecting a momentary switch (i.e., a push-button) you will want to use SwitchMode 3..7.

SwitchMode 3
Set push-button mode (0 = TOGGLE, 1 = ON (default))

Tasmota will send a TOGGLE command when the button is released (opening the circuit). When pressing the button (closing the circuit) nothing will happen. Default state is ON and when pressed it's OFF. (This trigger is known as falling-edge)

SwitchMode 4
Set inverted push-button mode (0 = OFF (default), 1 = TOGGLE)

Tasmota will send a TOGGLE command when the button is pressed (closing the circuit). When the button is released (opening the circuit) nothing will happen. Default state is OFF and when pressed it's ON. (This trigger is known as rising-edge)

SwitchMode 5
Set push-button with long press mode (0 = TOGGLE, 1 = ON (default), long press = HOLD)

Tasmota will send a TOGGLE command when the button is released (opening the circuit). When pressing the button (closing the circuit) nothing will happen. Default state is ON and when pressed it's OFF. When held for the time set in SetOption32 (default = 4s), Tasmota sends HOLD (use Switch<x>#state=3 in rules).

SwitchMode 6
Set inverted push-button with long press mode (0 = OFF (default), 1 = TOGGLE, long press = HOLD)

Tasmota will send a TOGGLE command when the button pressed (closing the circuit). When the button is released (opening the circuit) nothing will happen. Default state is OFF and when pressed it's ON. When held for the time set in SetOption32 (default = 4s), Tasmota sends HOLD (use Switch<x>#state=3 in rules).

Long press or hold can be used in conjunction with rules to create additional features or to control another Tasmota device

SwitchMode 7
Set toggle push-button mode. Same as SwitchMode 0.

SwitchMode 8
Set switch to multi change toggle mode (0 = TOGGLE, 1 = TOGGLE, 2x change = HOLD).

Same as SwitchMode 0 but when the state of the circuit changes within 0.5s twice no TOGGLE commands are send but Tasmota sends HOLD (use Switch<x>#state=3 in rules).

Warning

When you change switch states fast (within 0.5s) some extra actions can be triggered using rules. ON/OFFpower state is only changed when there is no second switch change within 0.5s.

SwitchMode 9
Set switch to multi change follow mode (0 = OFF, 1 = ON, 2x change = HOLD)

Same as SwitchMode 1 but when the state of the circuit changes within 0.5s twice no OFF/ON commands are send but Tasmota sends HOLD (use Switch<x>#state=3 in rules).

Warning

When you change switch states fast (within 0.5s) some extra actions can be triggered using rules. ON/OFFpower state is only changed when there is no second switch change within 0.5s.

SwitchMode 10
Set switch to multi change inverted follow mode (0 = ON, 1 = OFF, 2x change = HOLD)

Same as SwitchMode 2 but when the state of the circuit changes within 0.5s twice no ON/OFF commands are send but Tasmota sends HOLD (use Switch<x>#state=3 in rules).

Warning

When you change switch states fast (within 0.5s) some extra actions can be triggered using rules. ON/OFFpower state is only changed when there is no second switch change within 0.5s.

SwitchMode 11
Set switch to pushbutton with dimmer mode

Tasmota will send a TOGGLE command (use Switch#state=2 in rules) when the button is pressed for a short time and is then released. When pressing the button (closing the circuit) for a long time (set in SetOption32) Tasmota sends repeated INC_DEC (increment or decrement the dimmer) commands (use Switch#state=4 in rules) as long as the button is pressed. Releasing the button starts a internal timer, the time is set in SetOption32. When released for the time set in SetOption32 Tasmota sends a CLEAR command (use Switch#state=6 in rules). If the button is pressed again before the timeout Tasmota sends a INV command (use Switch#state=5 in rules). The INV command is for the controlling software (home assistant) to switch between incrementing and decrementing the dimmer.

Tip

The dimmer mode can be used in conjunction with rules to create additional features or to control another Tasmota device. The dimmer mode can be used to turn a media player on and off and to control the volume of a media player with one switch.

SwitchMode 12
Set switch to inverted pushbutton with dimmer mode. The same as Switchmode 11 but with inverted behaviour.

SwitchMode 13
Set switch to "push to on" mode (1 = ON, 0 = nothing)

Tasmota will send an ON command when the button pressed (closing the circuit). When the button is released (opening the circuit) nothing will happen. Switch off using PulseTime.

SwitchMode 14
Set switch to inverted "push to on" mode (0 = ON, 1 = nothing)

This mode is useful with PIR sensors

SwitchTopic~

Warning

When using SwitchTopic 1 or 2 (or ButtonTopic 1 or 2) and your MQTT broker becomes unavailable, Tasmota falls back to default SwitchTopic 0 (or ButtonTopic 0), which is not optimal.
To avoid this, we recommend using rules. They simply always work!

If you still need to use SwitchTopic read on!

SwitchTopic 0

Default mode

By default a switch controls the corresponding power state and doesn't send any MQTT messages itself.

No MQTT message will be published on account of the new switch state. The message you see in console is the new power state that is controlled and not the switch state.

SwitchTopic 1

Sets MQTT switch topic to device %topic%

When changing the state of the switch an MQTT message is sent to the device topic with the payload according to SwitchMode set.

Example

Device topic tasmota with SwitchMode 3 yields the following message: MQT: cmnd/tasmota/POWER = TOGGLE

Notice the cmnd instead of the stat at the beginning.

This is the same as sending an MQTT commands to this device, the device power state will be set to the defined state.

SwitchTopic <value>

Set switch topic to a custom topic (32 characters max)

This will send an MQTT message to a custom defined topic similarly to option 1.

In the following example, we set the topic to tasmota02 with SwitchTopic tasmota02.

Example

Device topic tasmota with SwitchMode 1 and custom topic tasmota02 yields the following message: MQT: cmnd/tasmota02/POWER = ON

If you have another device with the topic tasmota02 this action will turn on its power while not affecting anything on the tasmota device.

SwitchTopic Summary

SwitchTopic 0 controls the power state directly.
SwitchTopic 1 sends an MQTT message to the device topic. This sets the state of the devices power accordingly.
SwitchTopic <value> sends an MQTT message command to the custom topic. This does not change the state of the devices power.

Button~

For Tasmota, a Button is typically a momentary push-button (or a capacitive touch button in some light switches). By default a button toggles the corresponding power state. Every time the button gets pressed a relay or light changes its Power state (ON or OFF). Besides toggling the Power state, a button is also used to activate multi press button functions, to do long press (HOLD) actions, or send messages to different MQTT topics.

A push-to-make button should use a Button<x> component while a push-to-break button should use Button<x>i (i.e., inverted).

To ignore default button behaviour of controlling power outputs you can:

  1. use SetOption73 1
  2. use ButtonTopic
  3. define a rule which triggers on Button<x>#State. Take note: If the rule trigger only certain states, default behaviour is supressed only for those states.

Make Button1 publish its value to cmnd/custom-topic/BUTTON and not control Power1

Backlog ButtonTopic 0
Rule1 on Button1#state do Publish cmnd/custom-topic/BUTTON %value% endon
Rule1 1

Multi-Press Functions~

Multipress functions for 2 and more presses cannot be changed using SetOptions or rules.

Danger

If you have changed ButtonTopic, SetOption1, SetOption11 or SetOption13 some of the listed functionality will be changed or removed.

Note

Button1 can directly control up to five relays. The number of the activated relay corresponds to the number of button presses and this feature is not present in the other buttons.
When ButtonTopic is set to default 0 a button will always send its state for rules.

1 short press

Toggles the power state. This will blink the LED once and send an MQTT status message like stat/tasmota/POWER = ON or another one like stat/tasmota/BUTTON<x> = {"ACTION":"SINGLE"} when SetOption73 is enabled. The button state for rules is 2 (10 if Setoption73 is enabled).

2 short presses

When using Button1 toggles the second power state (if available on the device). This will blink the LED twice and send an MQTT status message like stat/tasmota/POWER2 = ON or another one like stat/tasmota/BUTTON<x> = {"ACTION":"DOUBLE"} when SetOption73 is enabled. The button state for rules is 11.

3 short presses

When using Button1 toggles the third power state (if available on the device). This will blink the LED three times and send an MQTT status message like stat/tasmota/POWER3 = ON or another one like stat/tasmota/BUTTON<x> = {"ACTION":"TRIPLE"} when SetOption73 is enabled. The button state for rules is 12.

4 short presses

When using Button1 toggles the fourth power state (if available on the device). This will blink the LED for times and send an MQTT status message like stat/tasmota/POWER4 = ON or another one like stat/tasmota/BUTTON<x> = {"ACTION":"QUAD"} when SetOption73 is enabled. The button state for rules is 13.

5 short presses

When using Button1 toggles the fifth power state (if available on the device). This will blink the LED five times and send an MQTT status message like stat/tasmota/POWER5 = ON or another one like stat/tasmota/BUTTON<x> = {"ACTION":"PENTA"} when SetOption73 is enabled. The button state for rules is 14.

6 short presses

Start WifiConfig 2. Can be disabled using SetOption1 1

Long press

There are two separate functions associated with a button long press based on how long it is held:

  1. When held continuously for 40 seconds (Configurable with SetOption32, value is 10x the configured hold time) Tasmota will reset to firmware defaults and restart.
  2. If enabled, button pressed for 4 seconds (Configurable with SetOption32) creates a HOLD action and send an MQTT status message like stat/tasmota/BUTTON<x> = {"ACTION":"HOLD"} when SetOption73 is enabled. The button state for rules is 3.

If ButtonRetain has been enabled the MQTT message will also contain the MQTT retain flag.

Danger

When a button is configured as inverted or with a Switchmode that keeps it as ON while depressed it activates the reset to firmware defaults function. Change the Button configuration or SwitchMode to avoid repeated reset to defaults or use Setoption1 1 to disable that function.

Warning

If you define a button with a number higher than available power outputs it will default to controlling Power1. Example: Button4 on a device with Power1 and Power2 will control Power1.

ButtonTopic~

ButtonTopic 0

Default option

By default a button controls the corresponding power state and doesn't send any MQTT messages itself.

No MQTT message will be published on account of the new button state. The message you see in console is the new power state that is controlled and not the button state.

ButtonTopic 1

Sets MQTT button topic to device %topic%.

When changing the state of the button an MQTT message is sent to the device topic with the payload according to SwitchMode set.

Example

Device topic tasmota with SwitchMode 3 yields the following message: MQT: cmnd/tasmota/POWER = ON

Notice the cmnd instead of the stat at the beginning.

This is the same as sending an MQTT command to this device, the device power state will be set to the defined state.

ButtonTopic <value>

Set button topic to a custom topic (32 characters max).

This will send an MQTT message to a custom defined topic similarly to option 1.

Example

For example, we set the topic to tasmota02 with ButtonTopic tasmota02.
With SwitchMode 1 the device yields the following message: MQT: cmnd/tasmota02/POWER = TOGGLE

If you have another device with the topic tasmota02 this action will toggle its power state while not affecting anything on the tasmota device.

ButtonTopic Summary

ButtonTopic 0 controls the power state directly.
ButtonTopic 1 sends an MQTT message to the device topic. This sets the state of the devices power state accordingly.
ButtonTopic <value> sends an MQTT message command to the custom topic. This does not change the state of the devices power state.

When a Button is set to a different topic than 0 is not possible to use Button#State as a trigger for rules.

Changing Default Functionality~

If a ButtonTopic (and if SetOption1 1) or SwitchTopic 1 is defined (and SwitchMode is set to 5 or 6) and a button is pressed longer than defined Key Hold Time (SetOption32 default 4 seconds) an MQTT message like cmnd/%topic%/POWER HOLD will be sent. HOLD can be changed with StateText4.

Command SetOption11 allows for swapping the functionality between the SINGLE and DOUBLE press of the push button.

These changes result in the following:

Action matrix

Example

You can control a ceiling fan from a Sonoff Touch:
If your standard topic of Sonoff Touch is light and the ceiling fan topic is ceilingfan issue these commands on the Sonoff Touch to activate the double press feature.

ButtonTopic ceilingfan
SetOption11 1

All of the above is easier accomplished using Rules!


For a practical application of everything mentioned in this article read about this excellent LEGO nightstand switch project.