Tasmota – Configure shut-off delay for relay-output

Letting a relay run with a shut-off delay is actually a function that is used quite often. Unfortunately (at least until today) this is not so easy to implement with the Tasmota relays.

A practical application for a shut-off delay is, for example, a bathroom fan.
A bathroom fan is often paired with the bathroom light switch. If the bathroom light is switched on, the bathroom fan also runs.
This is practical but has the disadvantage that, for example, after a shower, the moist air is only extracted when the light is switched on.

An overrun relay is helpful here. This waits a certain time after switching off (the light and thus also the fan relay) before the relay then also switches off the connected actuator (here the fan).

How you can activate this function on a Tasmota device with a trick is explained in the following article.

Safety instructions

I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/

Affiliate links / advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂


Helpful Articles:
Before you can use this function, you should have installed your Tasmota device and configured it so that you can access it via your WiFi. How to do this is described in the following articles.

Tasmota – Flash the firmware

Needed tools:
– none –

Needed material:

How it works

Unfortunately there is not “one” command in Tasmota with which you can implement a delay time.

However, there is the option of defining your own rules in the Tasmota firmware. With its help, various behaviors and a shut-off delay time can be defined. The following is an explanation of a possible solution.

First, we use the rules to define that the Tasmota relay listens for messages on a specific MQTT topic. As soon as a command to switch off the relay is received, the relay is not switched off directly. Instead, a timer is started. The relay is only switched off after this timer has expired.

If a command to switch on the relay is received while the timer is running, the timer is deactivated and the relay remains switched on.

The following section describes the behavior as a rule.

The rule you need

In the following you can see the rules that are needed.

Rule 1: Subscribe to the MQTT topic “cmnd/3dswitch” as soon as the relay is connected to your MQTT server. Of course, you can also choose a different topic here.

Rule 2: Activates the relay and resets the timer as soon as it receives a “1” on the subscribed topic.

Rule 3: Starts the timer to switch off the relay as soon as a “0” is received on the subscribed topic.

Rule 4: Deactivates the relay as soon as the previously set timer has expired. The set value “120” corresponds to a follow-up time of 120 seconds. If you want a different delay time, you can adjust this value accordingly.

on Mqtt#Connected do 
	Subscribe 3dswitch, cmnd/3dswitch 

on Event#3dswitch=1 do 
	backlog power1 on; ruletimer1 0 

on Event#3dswitch=0 do 
	ruletimer1 120 

on rules#timer=1 do 
	power1 0 

To apply this Tasmota configuration to your Tasmota relay, you now have to execute the following commands in the console of your Tasmota relay.

backlog switchmode1 1; rule1 1


on Mqtt#Connected do 
	Subscribe 3dswitch, cmnd/3dswitch 
on Event#3dswitch=1 do 
	backlog power1 on; ruletimer1 0 
on Event#3dswitch=0 do 
	ruletimer1 120 
on rules#timer=1 do 
	power1 0 

Activate the function for subscribing to MQTT topics

Unfortunately, a small problem is that subscribing to MQTT topics is not supported by the pre-compiled binaries of the Tasmota firmware. This must be activated separately and then programmed onto your Tasmota relay.

To do this, you have to download the firmware from the Tasmota GIT repository.

You can find the repository here: https://github.com/arendst/Tasmota

How you can download files from a Git repository is described in the article GitHub – How do I copy files from a Git repository to my computer

As soon as you have downloaded the files you should open the source code in the Arduino IDE. Then switch to the “my_user_config.h” file and/or search directly for the value “SUPPORT_MQTT_EVENT”.

Here you can see the relevant point. To activate the subscription functionality to MQTT topics, you must …
… activate this part (as shown in the picture) by removing the comment characters with the two “//”.

Then you have to program the firmware on your Tasmota relay.

You can also find information about this here: https://tasmota.github.io/docs/Subscribe-%26-Unsubscribe/

Switching the relay on and off

Actually, not much has changed to turn the now prepared relay on and off.

To switch on you just have to send a “1” to the topic “cmnd / 3dswitch” (or the one you configured).

To switch off you have to send a “0” to the configured topic. After the set delay time, the relay should switch off.

Have fun with the project

I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com   


  1. I am trying to create an switch using the MQTT platform. Either I get two lightning bolt symbols (one on/one off) or the toggle style switch, but it won’t stay on.

    The switch itself is called ‘tasmota21’ and the mqtt topic that controls the timer function is ‘3dswitch’.

    From my switches.yaml file
    – platform: mqtt
    name: “3dSwitch”
    unique_id: “delay_switch”
    #state_topic: “stat/3dswitch/RESULT”
    value_template: ‘{{ value_json[“POWER”] }}’
    command_topic: “cmnd/3dswitch”
    #availability_topic: “tele/tasmota21/LWT”
    qos: 1
    payload_on: “1”
    payload_off: “0”
    state_on: “ON”
    state_off: “OFF”
    payload_available: “Online”
    payload_not_available: “Offline”
    retain: true

    1. Hey ac3.14lot,
      actually I’m a bit confused. What system are you using? Is it possible that you are implementing this in Homeassistant or IObroker? 🙂
      Best regards

Leave a Reply

Your email address will not be published. Required fields are marked *