|
| 1 | +>D |
| 2 | +; A valid script must start with >D in the first line |
| 3 | + |
| 4 | +; Variables are defined and initialized (p means permanent) |
| 5 | +; Total number of counted pulses |
| 6 | +p:TotalPulses=0 |
| 7 | +;Variable to keep accumulated liters |
| 8 | +LiterAcc=0 |
| 9 | +; Total number of measured liters |
| 10 | +p:TotalLiter=0 |
| 11 | +; Initial offset for counter |
| 12 | +p:InitialOffset=0 |
| 13 | +; Target counter value |
| 14 | +p:TargetValue=0 |
| 15 | +; Variable for reset button |
| 16 | +resetb=0 |
| 17 | +; Variable for set value button |
| 18 | +setvaluebutton=0 |
| 19 | +delta=0 |
| 20 | +tmp=0 |
| 21 | + |
| 22 | + |
| 23 | +>B |
| 24 | +; >B is executed on BOOT time before sensors are initialized and on save script |
| 25 | +; After boot restore "total number of counted pulses" back to latest state of ESP pulse counter 1 (pc[1]) |
| 26 | +TotalPulses=pc[1] |
| 27 | + |
| 28 | +>S |
| 29 | +; >S is executed every second |
| 30 | +; If reset button resetb has been pushed (resetb has been set to 1) |
| 31 | +if resetb==1 |
| 32 | +then |
| 33 | +; The reset button variable resetb is set back to 0 |
| 34 | +resetb=0 |
| 35 | +; The total number of counted pulses is set back to 0 |
| 36 | +TotalPulses=0 |
| 37 | +; The total number of counted liters is set back to 0 |
| 38 | +TotalLiter=0 |
| 39 | +LiterAcc=0 |
| 40 | +; ESP variable counter1 is set back to 0 |
| 41 | +=>Counter1 0 |
| 42 | +endif |
| 43 | + |
| 44 | +if setvaluebutton==1 |
| 45 | +then |
| 46 | +setvaluebutton=0 |
| 47 | +InitialOffset=TargetValue-LiterAcc |
| 48 | +endif |
| 49 | + |
| 50 | +; Calulate instantaneous flow rate |
| 51 | +delta=pc[1]-TotalPulses |
| 52 | +TotalPulses=pc[1] |
| 53 | + |
| 54 | +tmp=delta/6.6/60 |
| 55 | +print Instantaneous flow rate=%tmp% |
| 56 | +LiterAcc=LiterAcc+tmp |
| 57 | +TotalLiter=InitialOffset+LiterAcc |
| 58 | + |
| 59 | +; Every 5 seconds: Print status and publish via MQTT |
| 60 | +if upsecs%5==0 |
| 61 | +then |
| 62 | +; Print a summary in the tasmota log |
| 63 | +print Status: Total (in liter): %TotalLiter%, Total (in pulses): %TotalPulses% |
| 64 | +=>publish stat/%topic%/RESULT {"TotalLiter":%TotalLiter%,"TotalPulses":%TotalPulses%} |
| 65 | +endif |
| 66 | +;Save permanent variables |
| 67 | +if upsecs%10==0 |
| 68 | +then |
| 69 | +svars |
| 70 | +endif |
| 71 | + |
| 72 | +>W |
| 73 | +; The lines in the >W section are displayed in the web UI main page. |
| 74 | +; Total number of measured liters (in liter) |
| 75 | +Total{m}%TotalLiter% Liter |
| 76 | +Initial offset{m}%InitialOffset% Liter |
| 77 | + |
| 78 | +; Button (variable name, text of ON state of button, text of OFF state of button) |
| 79 | +bu(setvaluebutton "Setting" "Set counter value") |
| 80 | +bu(resetb "Resetting" "Reset Total") |
0 commit comments