Tasmota need delay for first public MQTT

1,232 views
Skip to first unread message

Ivica Bojič

unread,
Nov 29, 2019, 11:25:01 AM11/29/19
to TasmotaUsers
Hello,

I use Tasmota sensor53 to measure electricity, deepsleep and battery. Upon waking up, an empty topic is sent before the meter reads in. Is it possible to delay the first topic? The second delay is using "#define TELE_PERIOD" in sonoff.ino.
This way, two states of 0 and value go to MQTT and this is not desirable.

Thank you for your help

Anivaldo Rezende

unread,
Nov 29, 2019, 7:47:32 PM11/29/19
to TasmotaUsers
Hi! Can you please copy the console and put it here to help us understand it?

Ivica Bojič

unread,
Nov 30, 2019, 2:19:22 AM11/30/19
to TasmotaUsers
Hello, as you see, first one is always empty, waiting next tele period.

12:59:48 MQT: Attempting connection...
12:59:49 MQT: Connected
12:59:49 MQT: tele/NodeMCU_Strom/LWT = Online (retained)
12:59:49 MQT: cmnd/NodeMCU_Strom/POWER =
12:59:49 RSL: tele/NodeMCU_Strom/INFO1 = {"Module":"Sonoff Basic","Version":"6.6.0.20(sonoff)","FallbackTopic":"cmnd/DVES_D53DD6_fb/","GroupTopic":"sonoffs"}
12:59:49 MQT: tele/NodeMCU_Strom/INFO2 = {"WebServerMode":"Admin","Hostname":"NodeMCU_Strom-7638","IPAddress":"192.168.0.20"}
12:59:49 MQT: tele/NodeMCU_Strom/INFO3 = {"RestartReason":"External System"}
12:59:49 MQT: stat/NodeMCU_Strom/RESULT = {"POWER":"ON"}
12:59:49 MQT: stat/NodeMCU_Strom/POWER = ON
12:59:57 RSL: tele/NodeMCU_Strom/STATE = {"Time":"2019-11-24T12:59:57","Uptime":"0T00:00:29","UptimeSec":29,"Heap":22,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"ON","Wifi":{"AP":2,"SSId":"Internet2","BSSId":"18:A6:F7:C0:B4:70","Channel":1,"RSSI":32,"LinkCount":1,"Downtime":"0T00:00:07"}}
12:59:57 MQT: tele/NodeMCU_Strom/SENSOR = {"Time":"2019-11-24T12:59:57","STROM":{"Total_in":0.0000,"Total_in":0.0000,"Total_in":0.0000}}
13:04:57 RSL: tele/NodeMCU_Strom/STATE = {"Time":"2019-11-24T13:04:57","Uptime":"0T00:05:29","UptimeSec":329,"Heap":22,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"ON","Wifi":{"AP":2,"SSId":"Internet2","BSSId":"18:A6:F7:C0:B4:70","Channel":1,"RSSI":28,"LinkCount":1,"Downtime":"0T00:00:07"}}
13:04:57 RSL: tele/NodeMCU_Strom/SENSOR = {"Time":"2019-11-24T13:04:57","STROM":{"Total_in":9896.0000,"Total_in":1611.0000,"Total_in":8284.0000}}

Anivaldo Rezende

unread,
Nov 30, 2019, 7:02:54 AM11/30/19
to TasmotaUsers
I don't any way to delay the first topic directly. What do you think about disable MQTT ON Boot with a rule, and then enable it ? you can use just one rule with the backlog comand and a delay between the disable and enable command, i think this will give some time to tasmota initialize the sensors readins

Ivica Bojič

unread,
Nov 30, 2019, 8:57:57 AM11/30/19
to TasmotaUsers
I found that deepsleep and mqtt use the same tele-period rule. So start at the same time and the reading will not take place. :-( Can rules help tell the sensor to turn on after 20 seconds and deepsleep only after a minute? If so how?

Anivaldo Rezende

unread,
Nov 30, 2019, 10:50:55 AM11/30/19
to TasmotaUsers
Hi !
I don't know how to differentiate between MQTT tele of Sensors and deepslepp. But try the following rule, it will disabled MQTT on boot, wait one minute then start MQTT, maybe this will give some time to tasmota start the sensors readings.

Rule1

ON System#Boot DO backlog SetOption3 0; Delay 600; SetOption3 1 ENDON

Ivica Bojič

unread,
Nov 30, 2019, 3:59:50 PM11/30/19
to TasmotaUsers
I Tried, but i using SCRIPT for electricity and maybe doesnt work with together. Compilation is ok, but Rule1 "Unknown command". :-(

Dne sobota 30. listopadu 2019 16:50:55 UTC+1 Anivaldo Rezende napsal(a):

Michael Ingraham

unread,
Dec 1, 2019, 9:25:42 AM12/1/19
to TasmotaUsers
Rules are disabled if you enabled Scripting. You have to implement that boot logic in your script.

Ivica Bojič

unread,
Dec 1, 2019, 10:50:29 AM12/1/19
to TasmotaUsers
I probably know how to do it, but I can't add a delay.
I want "on boot"
>D
>B
=>sensor53 r
=>SetOption3=0
=>DeepSleepTime=0
=>Delay=30
=>SetOption3=1
=>Delay=30
=>DeepSleepTime=3600

>M 1
+1,3,o,0,300,STROM,1,100,2F3F210D0A,063030300D0A


1,1.8.0(@1,Celková spotřeba,KWh,Celkova_spotreba,2
1,1.8.2(@1,Vysoký tarif,KWh,Vysoky_tarif,2
1,1.8.3(@1,Nízký tarif,KWh,Nizky_tarif,2

#


If I include it in the script, everything runs at the same time, plus SetOption only works once.

00:00:00 CFG: Loaded from flash at 3F7, Count 149
00:00:00 Script: nv=0, tv=0, vns=0, ram=16
00:00:00 Project NodeMCU Strom Tasmota Version 6.6.0.20(sonoff)-2_5_2
00:00:00 Script: performs "sensor53 r"
00:00:00 SNS: Hardware Serial
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"Time":"1970-01-01T00:00:00","SML":{"CMD":"restart"}}
00:00:00 Script: performs "SetOption3=0"
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"SetOption3":"ON"}
00:00:00 Script: performs "DeepSleepTime=0"
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"DeepSleepTime":0}
00:00:00 Script: performs "Delay=30"
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"Delay":0}
00:00:00 Script: performs "SetOption3=1"
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"SetOption3":"ON"}
00:00:00 Script: performs "Delay=30"
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"Delay":0}
00:00:00 Script: performs "DeepSleepTime=3600"
00:00:00 RSL: NODEMCU_proud/SENSOR/RESULT = {"DeepSleepTime":0}
00:00:00 SNS: Hardware Serial
00:00:00 WIF: Connecting to AP2 Internet home in mode 11N as NODEMCU_proud-7638...
00:00:04 WIF: Connected
00:00:04 HTP: Web server active on NODEMCU_proud-7638 with IP address 192.168.0.29
16:49:29 MQT: Attempting connection...
16:49:29 MQT: Connected
16:49:29 MQT: NODEMCU_proud/SENSOR/LWT = Online (retained)
16:49:29 MQT: NODEMCU_proud/SENSOR/cmnd/POWER = 
16:49:29 RSL: NODEMCU_proud/SENSOR/INFO1 = {"Module":"Sonoff Basic","Version":"6.6.0.20(sonoff)","FallbackTopic":"cmnd/DVES_D53DD6_fb/","GroupTopic":"tasmotas"}
16:49:29 MQT: NODEMCU_proud/SENSOR/INFO2 = {"WebServerMode":"Admin","Hostname":"NODEMCU_proud-7638","IPAddress":"192.168.0.29"}
16:49:29 MQT: NODEMCU_proud/SENSOR/INFO3 = {"RestartReason":"Software/System restart"}
16:49:29 MQT: NODEMCU_proud/SENSOR/RESULT = {"POWER":"ON"}
16:49:29 MQT: NODEMCU_proud/SENSOR/POWER = ON
16:49:37 RSL: NODEMCU_proud/SENSOR/STATE = {"Time":"2019-12-01T16:49:37","Uptime":"0T00:00:16","UptimeSec":16,"Heap":22,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"ON","Wifi":{"AP":2,"SSId":"Internet home","BSSId":"34:E8:94:32:72:9A","Channel":1,"RSSI":46,"LinkCount":1,"Downtime":"0T00:00:05"}}
16:49:37 RSL: NODEMCU_proud/SENSOR/SENSOR = {"Time":"2019-12-01T16:49:37","STROM":{"Celkova_spotreba":0.00,"Vysoky_tarif":0.00,"Nizky_tarif":0.00}}


Anivaldo Rezende

unread,
Dec 1, 2019, 1:54:17 PM12/1/19
to TasmotaUsers
I tried setoption with rules on boot, and it doesn't work, after the delay period when the SetOption3 1 command is executed the device restart. Sorry

heim...@gmail.com

unread,
Nov 17, 2021, 11:46:09 AM11/17/21
to TasmotaUsers
Sorry, a rather old post but I do have a similar problem.
The delay function in script is based on milliseconds, so you have to use delay(1000) for 1 second delay. the dalay command is actually only used between commands in a backlog.
And Yes with scripts rules are not possible.

My current problem:
I used deepsleep 3600 together with  a script, and since I am battery powered I want to be online as short as possible. I optimized overhead for dhcp and dns, and set
tper (telepriod) in the Boot section to 10 seconds, which is the shortest possible value.
My value is set in the T section it is set and output is done in the J section. After the Telemetrie is sent, the device sends the Deepsleep confirmation by mqtt and poweres down. (12-15s online)
Basically this works, but from time to time the deepsleep is shorter than expected (not triggered eg at 17:00:00 but at 16:59:40) than I have two measurements for the 16th hour and none for the 17th, because the next measurement would start at eg. 18:00:14. This will confuse most tools I've seen so far (curently used TIG telegraf->influx->grafana or homeassistants energy dashboard), since the calculated value expects one value each hour.

I think I have to check the time of the boot, and check  whether this happened to early (after time is set by ntp and delay dynamically)
Anybody already accomplished this?

Philip Knowles

unread,
Nov 18, 2021, 12:50:45 AM11/18/21
to heim...@gmail.com, TasmotaUsers
I'm not a script person but, if you're sure that the device will only wake up once an hour, could you create your own 'timestamp' as a variable which you increment every hour and publish your own topic which you read at the other end (and ignore the teleperiod one)?
If not, could you store the hour of the last sent message in a variable, check against the current time and if not incremented change the teleperiod to, say, 20 seconds to delay the message being sent?

Don't know if either would work in your use case.

Phil K


From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of heim...@gmail.com <heim...@gmail.com>
Sent: Wednesday, November 17, 2021 4:46:09 PM
To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: Tasmota need delay for first public MQTT
 
--
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/c3a5fc09-de59-494d-bdf6-3c905bb8e246n%40googlegroups.com.

heim...@gmail.com

unread,
Nov 18, 2021, 1:55:16 PM11/18/21
to TasmotaUsers
I have a fix on this, if anyone facing the same problem.
I first tried to increase the Teleperiod by constant  but I noticed, that sometimes the wakeup time was correct eg 18:00:02, but sometimes up to 3 minutes too early. I than tried to detect this in the script and adapt the tper accordingly. I noticed that now I have at least one measurement a short time after the full hour, but maybe another one short before, so every early wakeup I have two mqtt messages. I had to adapt the dashboards to use only the first measurement (first or minimum) of each hour interval. Still not perfect, since some tools are just calculationg the difference between measurements regardless of their time delta.

2021-11-18T13:00:06Z 2280.831
2021-11-18T14:01:22Z 2280.981
2021-11-18T14:58:59Z 2281.126     // an early wakeup ignored by using min in this interval (14-15:00Z)
2021-11-18T15:00:59Z 2281.13      // second measurement. Delta to the previous one is very small since only 2 minutes elapsed


What I did additionally:

>D
m=0
a=0

>B
if tper!=10 {
  tper=10
}

>S
m=time%60
if ((tinit>0) and (m>47) and (a==0)) {
a=(60-m)*60
tper=a
}


Since the S section is triggered each second, the fix for tper should only be done once (when a == 0)
and when minutes is > 47 (might also be adjusted to ~55 - 57... If I wake up even earlier, I do not want to stay online, because of battery life.
On boot tper is reset if if was adjusted before.
Reply all
Reply to author
Forward
0 new messages