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/sicherheitshinweise/


Requirements

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 
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 now have to execute the following commands in the console of your Tasmota relay.

backlog switchmode1 1; rule1 1

and

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

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.


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. 🙂

Fab

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

Leave a Reply

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