Tasmota script

500 views
Skip to first unread message

M Tobien

unread,
Feb 25, 2024, 11:49:38 AM2/25/24
to TasmotaUsers
hello
I need help to write a script that monitors gpio 14 and 15 on an ESP32. When 3.5 V is applied to the gpio, a message should be sent to mqtt with the status "on" for 8 seconds. Even if the actual pulse was much shorter (it can be between 0.5-10 seconds).
On the website of esp tasmota , the status for the respective GPIO should be displayed as "GPIOxx on" or "GPIOxx off" for the same amount of time as mqtt receives the status (8 seconds)
 I hope that someone can help me, or give me a web address where I can chat with people who work with scripts in tasmota.
THX
Marc

M Tobien

unread,
Feb 25, 2024, 11:54:14 AM2/25/24
to TasmotaUsers
I forgot to say that I mean the normal scripting and not Berry Scripting

Justin Adie

unread,
Feb 25, 2024, 1:48:13 PM2/25/24
to M Tobien, TasmotaUsers
In the configuration menu add switches on those gpios.  Ensure you use a switch_nX variant.  You should add pull down resistors on each pin.  10k should be fine.

In the console decouple the switches from the relay with so73 1

Now create a rule.  Assume switch 3 is on gpio14.  

Rule1 on switch3#state=1 do backlog0 publish sometopic on; ruletimer3 8 Endon on rules#timer=3 do publish sometopic off Endon


Not really sure what you mean by send an Mqtt message for 8 seconds. These things are asynchronous.  I have assumed you want to send an off message after 8 seconds.  

Replace sometopic with the full topic you wish to use.

If you really need the web UI to display on or off then you also should consider a synthetic relay for each switch. Add them in the configuration menu to unused gpios.  

Change the rule to be 

Rule1 on switch3#state=1 do backlog0 publish sometopic on; ruletimer3 8, power3 1 Endon on rules#timer=3 do backlog0 publish sometopic off; power3 0 Endon

Use webbuttonX to change the text for the relay.  E.g. in console: webbutton3 GPIO14






--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/3473f3f9-5e64-489b-a34e-d3a0de623e70n%40googlegroups.com.

M Tobien

unread,
Feb 25, 2024, 3:44:15 PM2/25/24
to TasmotaUsers
Thank you for your proposed solution. However, I encountered an issue where the rules didn't function as expected for me. Consequently, I'll need to resort to implementing the solution in a script instead. The challenge lies in translating the logic from the rules into a script format, which I'm currently unsure how to accomplish.

To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers+unsubscribe@googlegroups.com.

Justin Adie

unread,
Feb 25, 2024, 4:32:33 PM2/25/24
to M Tobien, TasmotaUsers
I'm not sure what you mean by a script.

Would you like us to help you get the rule working normally?  That would be the easiest solution in tasmota. Although your use case is unusual - it would also help to know the underlying purpose of what you're trying to achieve 

To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/a08b2799-a249-447e-a8d4-80a2f2a76395n%40googlegroups.com.

Philip Knowles

unread,
Feb 25, 2024, 11:54:40 PM2/25/24
to Justin Adie, M Tobien, TasmotaUsers
You can do it without a rule. Set the GPIO as a Button_i1 and another GPIO as Relay1. Set pulsetime1 80. The input will then toggle the output for 8 seconds which will repeat via MQTT. You might get a 'Hold' message too.


From: 'Justin Adie' via TasmotaUsers <sonof...@googlegroups.com>
Sent: Sunday, February 25, 2024 9:32:17 PM
To: M Tobien <ma...@tobien.net>
Cc: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: Tasmota script
 

Andrew L

unread,
Feb 26, 2024, 2:13:56 AM2/26/24
to TasmotaUsers
MQTT messages are sent once. So "on for 8 seconds" doesn't make sense, unless you mean send MQTT message "ON" and then after 8 seconds, send MQTT message "OFF". It is possible in scripting, however, it is less complicated to just use Rules and other already existing features of Tasmota. 

Do you want the 8 seconds to start after the gpio returns to 0v? Or do you want the 8 seconds to start as soon as the voltage is 3.5v? If the pulse is less than 0.5 seconds or greater than 10 seconds, do you want to ignore it? Can the pulse happen again within 8 seconds? If it does, do you want to restart the timer? Lots of details missing.

Be aware that the webpage only refreshes every 2.7 seconds, so you won't see real time information.

```
>D
button14=0
button15=0
timer14=0
timer15=0
gpio14=""
gpio15=""

>B
spinm(14 0)
spinm(15 0)

>F
button14=pin[14]
timer14+=1

if (button14==0 and timer14>5) {
=>publish /tasmota/stat/GPIO14 ON
gpio14="ON"
ts1(8000)
}

if button14>0 {
}
else {
timer14=0
}

>ti1
=>publish  /tasmota/stat/GPIO14 OFF
gpio14="OFF"

>W
<center>GPIO14=%gpio14%
```
This should get you started. But it takes some time to get into scripting and all the quirks. The webpage is helpful with examples.

M Tobien

unread,
Feb 26, 2024, 1:39:34 PM2/26/24
to TasmotaUsers
Thank you, that's exactly what I was looking for. It's already working almost as I need it to; however, I have one question that the Tasmota Scripting Language couldn't answer for me.
button14=pin[14]
timer14+=1

if (button14==0 and timer14>5) {
=>publish /tasmota/stat/GPIO14 ON
gpio14="ON"
ts1(8000)
}

if button14>0 {
}
else {
timer14=0
}

>ti1
=>publish /tasmota/stat/GPIO14 OFF
gpio14="OFF"
I do not understand ts1(8000) and later ti1. Can you explain those to me?
many thanks  

Andrew L

unread,
Feb 26, 2024, 9:27:35 PM2/26/24
to TasmotaUsers
ts1(8000) creates a 8 second timer, >ti1 is the script section that is executed when that timer expires.
Reply all
Reply to author
Forward
0 new messages