HowTo: Tasmota – Set off-delayed after-running of a relay

The overrun of a relay is actually a quite often used function. Unfortunately, this is (at least until today) not so easy to realize with the Tasmota relays.

A practical application for an overrun relay is, for example, a bathroom fan.
A bathroom fan is often coupled with the bathroom light switch. So when the bathroom light is on, the bathroom fan runs as well.
This is practical but has the disadvantage that, for example, after showering, the humid air is extracted only when the light is on.

An overrun relay is helpful here. This waits after switching off (the light and thus also the fan relay) for a certain time 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 notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.

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 make a purchase via this link, will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support in being able to offer other 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 firmware

Required tool:
- none

Required material:

The mode of operation

Unfortunately, there is not “one” command in Tasmota with which you can implement a follow-up time.

However, there is the possibility to define own rules in the Tasmota firmware. With its help different behaviors and also a run-on time can be defined. In the following 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. Only after this timer has expired, the relay is switched off.

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

The following section describes the behavior as a rule.

The required rule

Below 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 follow-up time, you can adjust this value accordingly.

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

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

backlog switchmode1 1; rule1 1


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

Enable function to subscribe to MQTT topics

A small problem is unfortunately that subscribing to MQTT topics is not supported by the pre-compiled binaries of the Tasmota firmware. This has to be enabled separately and then programmed on your Tasmota relay.

For this you have to download the firmware from the Tasmota GIT repository.

You can find the repository here:

How to download files from a Git repository is described in the article GitHub - How do I copy files from a Git repository to my computer? 

Once you have downloaded the files you should open the source code in the Arduino IDE. Then go to the file “my_user_config.h” and/or look directly for the value “SUPPORT_MQTT_EVENT”.

Here you can see the relevant post. To enable subscribing to MQTT topics, you need to...
...activate this part (as shown in the picture) by removing the comment with the two “//”.

After that you have to program the firmware to your Tasmota relay.

You can also find more information here:

Switching the relay on and off

Not much actually changes to turn the now prepared relay on and off.

To turn it on, all you have to do is send a “1” to the topic “cmnd/3dswitch” (or the one you configured).

To turn it off you have to send a “0” to the configured topic. After the set follow-up time, the relay should then switch off.

Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS 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 think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at       


  1. I'm trying to create a 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

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

This site uses Akismet to reduce spam. Learn how your comment data is processed.