Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.

Log in or register to post/reply in the forum.

Begin and end of rain event

Makada Nov 30, 2021 06:04 PM


I would like to know the crbasic code to have the timestamp from begin and end from an rain event.

For example:

Started at 12:10 

Ended at 13:45

I have a precipitation type sensor, so when the type is >0 it should save a timestamp (begin precipitation episode), and as soon as the type is zero, another timestamp (end precipitation episode).

With this example i can think it is also possible to have the begin and end timestamp from a gale force wind, or temperatures below zero Celsius degrees.

Will it also be possible to have it with a report range (for a day) in RTMC?

A one minute table is used.

With the kindest regards. 

Makada Dec 2, 2021 08:47 AM

I think it can be done with dataevent and or timer, but i dont know to code this:(

To have a timestamp as soon as precipitation type is >0 and a timestamp when the precipitation type is 0 again.

I know how to get a timestamp from when precipitation type is >0 but then i will have a timestamp (one minute table) every minute as long as the type is >0.

But dont know how to get the end timestamp.

When i set the end timestamp to 0, i will get lost of timestamps when it is 0.

Ideally a data string with only a start timestamp and an end timestamp is needed.

With the kindest regards. 

Thein Dec 3, 2021 06:32 AM


I've q subroutine similar to what you need, I modified litte bit to add timestamp for rain start and stop, but havn't tested. Probably it might work or you may need to tinker from it.

Sub Rain_Chk
  If Rain_mm > 0 Then
    If TMR = 1 Then Rain_Start = rTime(3)&"/"&rTime(2)&"/"&rTime(1)&" "&rTime(4)&":"&rTime(5)-1
    Rain_Status = "Raining Now!!!"
    Rain = True
    TMR = Timer(1,Min,0)

  If TMR <> 0 AND Rain_mm = 0 Then
    Rain_Stop = rTime(3)&"/"&rTime(2)&"/"&rTime(1)&" "&rTime(4)&":"&rTime(5)
    Rain_Status = "Raining Stopped!!!"
    Rain = False
    TMR = Timer(1,Min,3)

  If TimeIsBetween(0,60,60,Min) Then
    RF_PH0 = RF_PH0 + Rain_mm
    RF_PH = RF_PH0
    If TimeIntoInterval (0,60,Min) Then RF_PH0 = 0
  End If

  If TimeIntoInterval (0,1440,Min) Then
    RF_PD = TRF_mm
    TRF_mm = 0
  End If
End Sub

 You may need the fiirst two "if" cases only. Pls let me know, whether it works or how it works.


Makada Dec 3, 2021 07:18 AM

Hi Thein,

Thank you for showing this piece of code.

I am running it now, waiting for some rain:)

With the kindest regards. 

Makada Dec 4, 2021 10:20 AM

Hi Thein, 

It is working:)

As you can see, i have it triggered by the precipitation type.

I have removed the sub and end sub because of an error, dont know if thats ok to do?

And made a string with beginning of event and end of event.

Next challenge will be to have the amount of precipitation during the event and which precipitation type, which are 0, 10, 20, 30, 40 and 50.

And a datatable with that complete string.

With the kindest regards.  

'Sub Rain_Chk
  If Precip_Type > 0 Then
    If TMR = 1 Then Rain_start = RTime(3)&"/"&RTime(2)&"/"&RTime(1)&" "&RTime(4)&":"&RTime(5)
    Rain_Status = "Raining Now!!!"
    Rain_mm = True
    TMR = Timer(60,Sec,0)

  If TMR <> 0 AND Precip_Type = 0 Then
    Rain_Stop = rTime(3)&"/"&rTime(2)&"/"&rTime(1)&" "&rTime(4)&":"&rTime(5)
    Rain_Status = "Raining Stopped!!!"
    Rain_mm = False
    TMR = Timer(60,Sec,3)
'End Sub
StringA = Rain_start + "," + Rain_Stop


Thein Dec 5, 2021 05:57 AM

Hi Makada,

Its great. Its ok to take out the Subroutine and put the codes inside the main scan or subscan. I normally do the calculation outside the scan as Subroutine and call the sub whenever neccessay, not to messy inside the scan loop. But still messy :-).

Sorry that I cannot share the whole program as there got other instruemst such as IPIs, Weather Sensors, PZs, SGs, WSPs, Camera, SMS & Email system etc. And so many subroutines also.

Good luck for your next challenge, I think put another a few timers such as  TMR1, 2, 3.. etc.. can do the job.

Best regards,

Makada Dec 6, 2021 09:08 AM

I see the Timer function is very inaccurate.

Sometimes starting at 2 instead of 1 and sometimes Timer goes from 8 to 10 instead of 8 9 10...

And because the Timer starts at 1, it is missing the first tip.

Because of this the total function doesnt reset because "if Timer =1 then...etc".

With the kindest regards. 

Thein Dec 7, 2021 07:55 AM

Hi Makada,

It's good to know that. Sure, you'll find a better way to do this.

"Edited"... I think something not rght with your timer. The example code I gave is "TMR = Timer (1,Min,0)", "1" means timer number one, "Min" means how the timer count. The interger TMR value you see will be in number of minutes, if you use "Min" and it will be in number of seconds if you use "Sec". Someone who knows better can vet this or you can brawse in help for Timer function. You said in your first post, your program runs with 1minute interval or 1minute table, thats why I gave an example as minute timer, even I minus 1 minute in Start Time as the timer will start only after the first minute scan of rain event, I supposed.

Your timesr is Timer(60,Sec,0), that's mean your timer number is 60 (it doesn't matter) and counting by second. So, your scan interval pulse in seconds. I do not know how this will work. In my second post to get 10min, 20min, 30min,... rain intensity, I shoud say Timer no. 2, 3, 4 .. etc if you already used Timer no. 1.

I hope you will find a better solution.

Best regards,

Makada Dec 8, 2021 02:10 PM

I had a go with the counter function.

I have used the windspeed as a precipitation sensor so i have data when its not raining.

With the code below i have the counter reset at a certain value working ok.

Also the total is working ok.

But i cant get the total to reset when the counter is zero, its adding data at the total as soon as the condition is True.


Scan (1,Sec,0,0)


If Wind_Spe >=4 Then

If Wind_Spe <4 Then
counter = 0



Makada Dec 14, 2021 07:27 AM

 I did found this piece of code working ok.

But i noticed it is including a value <3 when the table is called. 

So because of this the total isnt right.

Public EventStarted As Boolean
Public EnableOutput As Boolean, DisableStats As Boolean
Public EventTrigger As Boolean ' this is the Event trigger

Public counter As Long, PTemp, AirTemp, DeltaTemp 'testing variables
Const one = 1 'testing constant

DataTable(Stats,EnableOutput,3000)'NOTE: must specify a table size, do NOT auto-allocate
Maximum (1,DeltaTemp,FP2,DisableStats,False)
Minimum (1,DeltaTemp,FP2,DisableStats,False)
Sample (1,counter,FP2)
Totalize (1,one,FP2,DisableStats)'this tell you how many samples are included in the output



'==================== testing section of code

PanelTemp (PTemp,_60Hz) 'measure reference temperature
TCDiff (AirTemp,1,mV2_5C,1,TypeT,PTemp,True ,0,_60Hz,1.0,0) 'measure air temperature
DeltaTemp = AirTemp-PTemp 'calculate the difference in temperatures

If DeltaTemp>=3 Then 'test for event condition(s)
EventTrigger = true
EventTrigger = false
'==================== End testing section of code

If EventStarted=False AND EventTrigger=true Then'EventStarted tells if it has started


If EventStarted=True AND EventTrigger=False Then 'Event is over so output stats, and reset control variables for next event.




Log in or register to post/reply in the forum.