Reset counter after sending

1,861 views
Skip to first unread message

snx

unread,
Feb 6, 2022, 5:04:08 PM2/6/22
to TasmotaUsers
Hi guys,

I'm new to using tasmota!

My device is an ESP32 that counts a pulse on an input. The input is configured and is transmitted via MQTT every ~5mins (default).

I would like to reset the counter after every transmit. This should happen on the Tasmota side, after the MQTT sent the Counter, it should be reset.

Does someone know a way to do this?

Regards,
Johnny

AKS-Gmail-IMAP

unread,
Feb 6, 2022, 9:01:14 PM2/6/22
to TasmotaUsers
Do it at the Tasmota as part of a rule. You will eventually be using rules and as by product of that, you will eventually be doing simple compiles because conditionals and if then usage  in rules is available only in DIY compiles that set a flag to build with those available.

Here is a rule example using a counter that is reset. This Counter1 is the revolution of a bicycle rear wheel on a trainer. This is a NodeMCU ESP8266. Only Counter1 monitoring was the initial setup. Then a rule to calculate and publish a rate, MPH in this case, was implemented later as a rule. That explains as you see later why the rule here is more complicated than needed. So both Counter1 and MPH are monitored. Counter1 data goes out as part of a Status message. MPH is actually published. The reset here for Counter1 happens only on boot up. It could have been reset during the MPH calculation, but I wanted the counter to remain untouched during the MPH publishing so that I could double check the data. In this case the data is eventually handled by openHAB via a technical Rube Goldberg setup. 

In the Rule3 var6 is set to the current counter value every time the counter changes.
Var16 holds the ruletimer interval. It is 60 for 60 seconds. Perhaps it should be 61 or the rules#timer=1 changed to rules#timer=0. The intent is var7 to be the counts over the last ruletimer1 period. You can see here that just resetting the Counter1 to 0 after the MPH calculation would eliminate some code.

The counter reset command is the “counter1 0” that happens on System#Boot.

Rule3
On counter#c1 do var6 %value% endon
On rules#timer=1 do backlog var7=var6-var8; var8 %var6%; ruletimer1 var16; var15=(var7*4.9088/var16); publish stat/tasmota_E2EA27/WMPH %var15%; endon 
On System#Boot do backlog var16 60; ruletimer1 var16; counter1 0; endon

In Tasmota rules are entered in the console all as one line. But a handy thing to know, which I am not sure is mentioned anywhere, is that you can paste into the console field multiline text. The linefeeds are ignored. I think that is why you see Tasmota rules published like you the the one above.

I hope this gives you a kick start if you need one. 



--
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/4f498690-4f5f-4938-a03a-ab2204f2d6c4n%40googlegroups.com.

Philip Knowles

unread,
Feb 7, 2022, 2:19:11 AM2/7/22
to TasmotaUsers

Replied direct sorry

 

Sent from Mail for Windows

 

From: Philip Knowles
Sent: 06 February 2022 22:23
To: snx
Subject: RE: Reset counter after sending

 

rule1 on tele-counter#c1 do counter1 0 endon

Rule1 1

 

Regards

 

Phil K

Sent from Mail for Windows

--

snx

unread,
Feb 13, 2022, 5:47:59 AM2/13/22
to TasmotaUsers
Thanks for you your replies, resetting the counter on tele does now work.
Since my application is similar to the example above, i tried to send a custom value, but the value does not get send.

My goal is to take the counter value at moment of "tele" and build a custom publish out of it. I tried to adopt above example and added the following rule:

Rule1
On tele-counter#c1 do backlog var1=%value%; var2=(%var1%/5.0)/875; publish stat/tasmota_460CE3/CUSTOMVALUE %var2%; counter 1 0 endon

var1 should become the current counter value, which works
var2 should divide the counter value by 5 and then by 875

But it does not work, console outputs the following:

11:45:23.433 RUL: TELE-COUNTER#C1 performs "backlog var1=216; var2=(/5.0)/875; publish stat/tasmota_XXXXXX /CUSTOMVALUE ; counter 1 0"
11:45:23.446 MQT: stat/tasmota_XXXXXX/RESULT = {"Var1":"=216"}
11:45:23.645 MQT: stat/tasmota_XXXXXX /RESULT = {"Var2":"=(/5.0)/875"}
11:45:23.844 MQT: stat/tasmota_XXXXXX CUSTOMVALUE =
11:45:24.047 MQT: stat/tasmota_XXXXXX /RESULT = {"Counter1":1}

Philip Knowles

unread,
Feb 13, 2022, 9:03:08 AM2/13/22
to snx, TasmotaUsers

Not surprising as Tasmota doesn’t have maths functions built in unless you compile it.

 

mult1 0.0002286 will scale var1 by 1/4375. You’ll probably need to set calcres to 7

 

Rule1
On tele-counter#c1 do backlog var1=%value%; mult1 0.0002286; publish stat/tasmota_460CE3/CUSTOMVALUE %var1%; counter 1 0 endon

Viktor Füzesi

unread,
Jun 14, 2022, 1:05:28 AM6/14/22
to TasmotaUsers
Hi,

Hi,
Im struggleing to understand the way how tasmota counter could be reseted after sending a value..
I have googled and found this topic, but failed to implement on my tasmota device.

The situation is near de same: i have a tipping bucket precipitation sensor that sends a count on each tip, and utility meter summarize it as a hourly/daily/weekly amount. But if there is a power outage on the tasmota device, it sends the summarized total count value to the utility meter on recovery of power, in this case the value on the picture.
So i think the counter needs to be reseted to 0 after sending on tasmota.

Could someone please point me to the right direction?
Thx a lot!
Viktor

285654356_3244151822509479_6961921890413341569_n.jpg
image_2022-06-14_070359300.png

Philip Knowles

unread,
Jun 14, 2022, 2:12:09 AM6/14/22
to Viktor Füzesi, TasmotaUsers
There shouldn't be a space between counter and 1. Autocorrect probably

 

Rule1
On tele-counter#c1 do counter1 0 endon

Will reset counter to 0 after each message
 


From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of Viktor Füzesi <fuz...@gmail.com>
Sent: Tuesday, June 14, 2022 6:05:28 AM
To: TasmotaUsers <sonof...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages