Email notification on Low Battery Voltage [notify in rules]

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Email notification on Low Battery Voltage [notify in rules]

#1 Post by fm-maniac » 14 Nov 2021, 12:22

In short, here's what I want to do:

The ESP8266 should send an Email when the battery voltage (measured externally on ADC) goes below a defined value.

First I set up notification #1. This is working properly. At least when clicking "test" the defined Email is sent!

Next I activated rules in the config and setup a simple rule:

Code: Select all

on System#Boot do
 notify 1
endon
I expected that the Email will be triggeres on each reboot. But nothing happens. What am I doing wrong?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Email notification on Low Battery Voltage [notify in rules]

#2 Post by TD-er » 14 Nov 2021, 13:07

System boot may be too early to send out an email as the network is not yet connected.
See the WiFi#Connected event:
https://espeasy.readthedocs.io/en/lates ... ore-events

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#3 Post by fm-maniac » 14 Nov 2021, 16:40

Thanks TD-er for your helpful reply!

I changed the rule to:

Code: Select all

on WiFi#Connected do
 notify 1
endon
and now the Email notification is triggered after reboot. BTW: My first approach was given as an example here: https://www.letscontrolit.com/wiki/inde ... tification.

Next I triggered the notification by the battery voltage getting below 3.5V:

Code: Select all

on BatteryMon#Ubat<3.5 do
 notify 1
endon
Thing is that the notification is triggered on each measurement below 3.5V (each 30s as setup in the device config).

What I would like to achive is to have this triggered only once (er even better once a day) as long as the value stays below 3.5V. This becomes even more complicated as the ESP is send to sleep mode after 30s of operation and wake up after 300s.

So I need to set a flag, log entry or something similiar that endures the sleep mode and can be used as condition for an if/else clause.

May I get some hints on how to achive this? some starting points for further investigations? That would be highly appreciated!

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#4 Post by fm-maniac » 14 Nov 2021, 17:32

I meanwhile found this thread

proposing this code:

Code: Select all

on Batterie#Spannung do
  if %eventvalue%<2.5 and [var#1]=0
    notify 1
    let,1,1
  endif
  if %eventvalue%>2.5
    let,1,0
  endif
endon
Unfortunatly this doesn't work while the ESP is put in sleepmode because of the variable not being properly initiated.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Email notification on Low Battery Voltage [notify in rules]

#5 Post by TD-er » 14 Nov 2021, 18:24

You can also use a dummy task and store the values in this dummy task value using taskvalueset. https://espeasy.readthedocs.io/en/lates ... skvalueset
As long as the ESP remains powered, the task values are stored in RTC memory and restored at boot.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#6 Post by fm-maniac » 21 Nov 2021, 15:51

I would like to follow up on this. I set up a dummy device called "Notification" with a variable "SendFlag" and created the following rule:

Code: Select all

on BatteryMon#Ubat do
  if %eventvalue%<3.5 and [Notification#SendFlag]=0
    notify 1, "The current battery voltage is: [BatteryMon#Ubat]V"
    TaskValueSet Notification,SendFlag,1 // Set Send Flag
  endif
  if %eventvalue%>=3.5
    TaskValueSet Notification,SendFlag,0 // Delete SendFlag
  endif
endon
The SendFlag maintains it's value during deep sleep so the notification is sent out only once. But if the battery voltage falls below 3.5V during the deep sleep phase, no notification is sent on the next awake cycle. Although I can observe the SendFlag changing to 1 in the device tab.

Code: Select all

3766: NTP : NTP replied: delay 30 mSec Accuracy increased by 0.066 seconds
3769: Time set to 1637505752.066 Time adjusted by 57446.13 msec. Wander: 0.000 msec/second Source: NTP
3773: Local time: 2021-11-21 15:42:32
3775: EVENT: Clock#Time=Sun,15:42
3795: EVENT: WiFi#Connected
3855: EVENT: System#NoSleep=30
3955: EVENT: Time#Set
10335: ADC : Analog value: 528 = 528.00
10352: EVENT: BatteryMon#Ubat=3.30
20335: ADC : Analog value: 528 = 528.00
20352: EVENT: BatteryMon#Ubat=3.30
30335: ADC : Analog value: 528 = 528.00
30353: EVENT: BatteryMon#Ubat=3.30
But it works the other way around when the battery voltage exceeds 3.5V during the sleep phase:

Code: Select all

3761: NTP : NTP replied: delay 20 mSec Accuracy increased by 0.120 seconds
3763: Time set to 1637505844.120 Time adjusted by 57505.47 msec. Wander: 0.000 msec/second Source: NTP
3767: Local time: 2021-11-21 15:44:04
3770: EVENT: Clock#Time=Sun,15:44
3789: EVENT: System#NoSleep=30
3856: EVENT: Time#Set
10334: ADC : Analog value: 746 = 746.00
10356: EVENT: BatteryMon#Ubat=4.66
10390: ACT : TaskValueSet Notification,SendFlag,0
20334: ADC : Analog value: 746 = 746.00
20356: EVENT: BatteryMon#Ubat=4.66
20389: ACT : TaskValueSet Notification,SendFlag,0
30334: ADC : Analog value: 746 = 746.00
30404: EVENT: BatteryMon#Ubat=4.66
Any ideas why the Flag chnages to "1" but notification is sent?

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Email notification on Low Battery Voltage [notify in rules]

#7 Post by Ath » 21 Nov 2021, 16:31

Can you add a line: eventlog,"NotificationSendFlag: [Notification#SendFlag]"
in your event handler 'on BatteryMon#Ubat do', before the first 'if', to see what that status really is after returning from deepsleep
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Email notification on Low Battery Voltage [notify in rules]

#8 Post by TD-er » 21 Nov 2021, 16:58

I think the WiFi connection itself may fail or take (much) longer if the battery voltage gets too low.
So you probably hit a timeout or DNS resolve fails.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#9 Post by fm-maniac » 21 Nov 2021, 17:15

Ath & TD-er, thanks for supporting me here. This is highly appreciated!

For clarification: The voltage I measure is an additional 2nd voltage from my lab power supply fed by voltage divider to the the ADC for test purpose. The supply voltage of the ESP itself is constant.

30s awake period: Ubat > 3.5V, SendFlag = 0
60s deep sleep period: Ubat change to <3.5V
30s awake period: event handler is entered with sendFlag = 1 -> should only be changed from 0 to 1 on passing the if clause?!

Code: Select all

10392: ACT : eventlog,'NotificationSendFlag: 1'
10395: Command unknown: eventlog,'NotificationSendFlag: 1'
BTW: why do I get the Command unknown entires?
Last edited by fm-maniac on 22 Nov 2021, 10:43, edited 2 times in total.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#10 Post by fm-maniac » 21 Nov 2021, 17:31

BTW: why do I get the Command unknown entires?
Using LogEntry instead of eventlog solves the issue.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#11 Post by fm-maniac » 21 Nov 2021, 17:38

Code: Select all

>> NetworkError when attempting to fetch resource. <<
5780: WD : Uptime 1 ConnectFailures 0 FreeMem 18176 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
5782: WiFi : Scan not needed, good candidate present
5820: NTP : NTP replied: delay 21 mSec Accuracy increased by 0.478 seconds
5822: Time set to 1637512379.478 Time adjusted by 57806.19 msec. Wander: 0.000 msec/second Source: NTP
5826: Local time: 2021-11-21 17:32:59
5835: EVENT: System#NoSleep=30
5857: EVENT: Time#Set
7249: EVENT: Clock#Time=Sun,17:33
10338: ADC : Analog value: 773 = 773.00
10358: EVENT: BatteryMon#Ubat=4.83
10371: ACT : LogEntry,'NotificationSendFlag: 0'
10374: NotificationSendFlag: 0
10407: ACT : TaskValueSet Notification,SendFlag,0
20335: ADC : Analog value: 773 = 773.00
20360: EVENT: BatteryMon#Ubat=4.83
20373: ACT : LogEntry,'NotificationSendFlag: 0'
20375: NotificationSendFlag: 0
20409: ACT : TaskValueSet Notification,SendFlag,0
30335: ADC : Analog value: 773 = 773.00
30358: EVENT: BatteryMon#Ubat=4.83
30371: ACT : LogEntry,'NotificationSendFlag: 0'
30373: NotificationSendFlag: 0
30409: ACT : TaskValueSet Notification,SendFlag,0
31806: WD : Uptime 1 ConnectFailures 0 FreeMem 17952 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
31807: WiFi : Scan not needed, good candidate present
>> NetworkError when attempting to fetch resource. <<
3461: EVENT: WiFi#ChangedAccesspoint
3578: EVENT: WiFi#ChangedWiFichannel
3635: WIFI : DHCP IP: 192.168.178.50 (ESPeasy-1) GW: 192.168.178.1 SN: 255.255.255.0 duration: 174 ms
3648: WIFI : Arduino wifi status: WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP
3650: firstLoopConnectionsEstablished
3654: EVENT: WiFi#Connected
3677: WD : Uptime 1 ConnectFailures 0 FreeMem 18648 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
3678: WiFi : Scan not needed, good candidate present
3719: NTP : NTP replied: delay 20 mSec Accuracy increased by 0.421 seconds
3722: Time set to 1637512471.421 Time adjusted by 57847.69 msec. Wander: 0.000 msec/second Source: NTP
3726: Local time: 2021-11-21 17:34:31
3728: EVENT: Clock#Time=Sun,17:34
3749: EVENT: System#NoSleep=30
3768: EVENT: Time#Set
10399: ADC : Analog value: 403 = 403.00
10411: EVENT: BatteryMon#Ubat=2.52
10425: ACT : LogEntry,'NotificationSendFlag: 1'
10427: NotificationSendFlag: 1

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#12 Post by fm-maniac » 22 Nov 2021, 17:01

Nobody having an idea how to solve this?

To monitor the SendFlag I created two additional rules:

Code: Select all

on system#boot do
  LogEntry,"SendFlag on boot: [Notification#SendFlag]"
endon

Code: Select all

on WiFi#connected do
  LogEntry,"SendFlag on wifi: [Notification#SendFlag]"
endon
None of this events showed up in the log windows. I assume that at that point of time logging may not be actice?

So is there's a chance that the first event for BatteryMon#Ubat occuring in the log isn't the first one operated? And that on the first operation the notification can't be sent out by any reason?

Code: Select all

on BatteryMon#Ubat do
  LogEntry,"SendFlag: [Notification#SendFlag]"

  if %eventvalue%>=3.5
    TaskValueSet Notification,SendFlag,0 // Delete SendFlag

  elseif %eventvalue%<3.5 and [Notification#SendFlag]=0
    TaskValueSet Notification,SendFlag,1 // Set Send Flag
    TaskValueSet Notification,Timestamp,%unixday%
    notify 1, "The current battery voltage is: [BatteryMon#Ubat]V"

  endif

endon
Last edited by fm-maniac on 22 Nov 2021, 17:14, edited 1 time in total.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Email notification on Low Battery Voltage [notify in rules]

#13 Post by TD-er » 22 Nov 2021, 17:12

Where do you read the rules?
Given the nature of the events you try to catch, you may only see these log items via serial log.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#14 Post by fm-maniac » 22 Nov 2021, 17:45

I'm geeting closer to a solution. I delayed triggering the notification by 5s and now it is sent out on awake when the monitored voltage dropes below the threshold during deep sleep.

Code: Select all

on BatteryMon#Ubat do
  LogEntry,"SendFlag: [Notification#SendFlag]"
  
  if %eventvalue%>=3.5
    TaskValueSet Notification,SendFlag,0 //Delete SendFlag

  elseif %eventvalue%<3.5 and [Notification#SendFlag]=0
    TaskValueSet Notification,SendFlag,1 //Set Send Flag
    TaskValueSet Notification,Timestamp,%unixday%
    timerSet,1,5 //set timer 1 to 5s
    
  endif
endon

on Rules#Timer=1 do
  LogEntry,"SendFlag: [Notification#SendFlag]"
  notify 1, "The current battery voltage is: [BatteryMon#Ubat]V"
endon
Obviously the rule is processed for the 1st time before WiFi is connected.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Email notification on Low Battery Voltage [notify in rules]

#15 Post by TD-er » 22 Nov 2021, 19:25

Code: Select all

[...]
  if %eventvalue%>=3.5
    [....]
  elseif %eventvalue%<3.5 and [Notification#SendFlag]=0
[...]
The elseif can be simplified, since the first part is the opposite of the first if part :)

Code: Select all

[...]
  if %eventvalue%>=3.5
    [....]
  elseif [Notification#SendFlag]=0
[...]

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#16 Post by fm-maniac » 22 Nov 2021, 20:22

Correct :D

Please be patient. I'm a old hand hardware guy and still in the learning phase. But this is a good opportunity for me to get more familiar with software and gain my knowledge.

I think I'll choose a different approach by checking the battery voltage once after wifi is connected.
Final use case is gathering data from a bme280 every 5min, sending them to a database and get back to deep sleep in between.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#17 Post by fm-maniac » 12 Dec 2021, 11:42

So this it how I currently solved it.

Task was to get the data from an BME280 (temperature, humidity, air pressure) send frequently to the database of my smartmerter (Volkszaehler).

After WiFi is connected data will be send via http. Subsequently I'm doing a two step check of the battery voltage. Below the 1st threshold an early warning is send by Email just once. If the 2nd threshold is cut an alert will be send every defined period (3minutes for testing puposes).

Finally the ESP will be set to deep sleep (60s for testing purposes).

Code: Select all

on WiFi#connected do
 
  //Send data to Volkszaehler
  SendToHTTP,192.168.178.36,80,middleware.php/data/b4a93980-5907-11ec-866f-a7fc10df3363.json?operation=add&value=[BME280#Temperature]
  SendToHTTP,192.168.178.36,80,middleware.php/data/a348a0e0-590a-11ec-9669-f7895ee1a54f.json?operation=add&value=[BME280#Pressure]
  SendToHTTP,192.168.178.36,80,middleware.php/data/fdf89540-590a-11ec-9fcd-cde54ac6ca54.json?operation=add&value=[BME280#Humidity]

  //Calculate timestamp from RTC
  Let,1,[Notification#TimestampDay]*86400+[Notification#TimestampSec]
  Let,2,3 //set reminder frequency in minutes

  if [BatteryMon#Ubat]>=4.0
    TaskValueSet Notification,SendFlag,0 //Delete SendFlag

  elseif [BatteryMon#Ubat]<4.0 and [Notification#SendFlag]=0
    TaskValueSet Notification,SendFlag,1 //Set Send Flag
    notify 1, "Early Warning: The battery voltage of your ESP is: [BatteryMon#Ubat]V"
  
  elseif [BatteryMon#Ubat]<3.5 and %unixtime%>[var#1]+[var#2]*60
    TaskValueSet Notification,TimestampDay,%unixday%
    TaskValueSet Notification,TimestampSec,%unixday_sec%
    notify 1, "Alert: The battery voltage of your ESP is: [BatteryMon#Ubat]V"
  endif 

  if [DisablePD#State]=0  
      timerSet,1,5 //set timer 1 to 5s, wait until notification is sent
  endif

endon

on Rules#Timer=1 do
  DeepSleep,60 //Send ESP to deep sleep for 60s
endon
Thing is that I have to delay the deep sleep. Otherwise the Email is not send! Although it is reported in the log.

Code: Select all

3949 : Info   : ACT  : timerSet,1,5
3974 : Info   : EVENT: BME280#Temperature=21.6
4637 : Info   : Email: To ***@***.de
4799 : Info   : EMAIL: Connection Closed Successfully
4810 : Info   : EVENT: BME280#Humidity=35
4881 : Info   : NTP  : NTP replied: delay 20 mSec Accuracy increased by 0.555 seconds
4884 : Info   : Time set to 1639305525.555 Time adjusted by 57817.31 msec. Wander: 0.000 msec/second Source: NTP
4887 : Info   : Local time: 2021-12-12 11:38:45
4891 : Info   : EVENT: Clock#Time=Sun,11:38
4935 : Info   : EVENT: BME280#Pressure=1021
5010 : Info   : EVENT: Time#Set
8952 : Info   : EVENT: Rules#Timer=1,1
8986 : Info   : ACT  : DeepSleep,60
8988 : Info   : EVENT: System#Sleep
9022 : Info   : SLEEP: Powering down to deepsleep...
9024 : Info   : WIFI : Set WiFi to OFF

Is there any event created that securely gives evidence that the notification task is closed and the Email is sent? Or any other ideas how to solve this smarter instead of simply delaying the deep sleep entry by 5s? My aim is to keep the uptime of the ESP as short as possible to elongate battery live time.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Email notification on Low Battery Voltage [notify in rules]

#18 Post by TD-er » 12 Dec 2021, 12:52

A few things to keep in mind when testing this:
I do perform an ADC read right before trying to connect to WiFi, since the ADC reading is affected by WiFi RF calibration.
But this is also right at boot, so that may be a current surge and thus the first reading upto the moment WiFi is connected may report a lower value than later samples.
This can be exactly what you need, as a discharged battery also shows increased internal resistance and thus the voltage may drop more at higher currents.
So just to keep in mind when testing this.

Another thing you should really look into is the quiescent current of the parts that are permanently connected to the battery, like the voltage regulator.
Most regulators, like the AMS1117 do have a terrible high quiescent current of 5 - 10 mA.
Meaning the voltage regulator alone does draw this current even when the ESP is in deep sleep.

Just one source of information on this subject: https://randomnerdtutorials.com/esp8266 ... batteries/

You can get away with LDO regulators which only can handle 200 mA, but then you may need to include a capacitor very close to the ESP of 22uF - 100uF.
But these may cause a high current when they are empty and need to be charged. For this you may need to add a R/C delay to the EN pin to delay starting the ESP until these caps are fully charged.
This may require some tweaking.

If you use sendtohttp, you may want to check "SendToHTTP wait for ack" on Tools->Advanced page to make sure the command is finished by the rules.

fm-maniac
Normal user
Posts: 22
Joined: 07 Nov 2021, 12:39
Location: Germany

Re: Email notification on Low Battery Voltage [notify in rules]

#19 Post by fm-maniac » 12 Dec 2021, 14:49

I already checked "SendToHTTP wait for ack".

For the LVR I opted for a Torex XC6220 with a quiesent current of only 8µA (typ) if in PS-Mode (load current < 1mA). An MCP1700 is specified with even lesser 1,6µA but the XC6220 has a lower dropout voltage.

Finally it's the on period that discharges the battery...

I improved the rule by applying the timer only in case of an email has to be sent. Otherwise the deep sleep is entered imediatly:

Code: Select all

on WiFi#connected do
  //Send data to Volkszaehler
  SendToHTTP,192.168.178.36,80,middleware.php/data/b4a93980-5907-11ec-866f-a7fc10df3363.json?operation=add&value=[BME280#Temperature]
  SendToHTTP,192.168.178.36,80,middleware.php/data/a348a0e0-590a-11ec-9669-f7895ee1a54f.json?operation=add&value=[BME280#Pressure]
  SendToHTTP,192.168.178.36,80,middleware.php/data/fdf89540-590a-11ec-9fcd-cde54ac6ca54.json?operation=add&value=[BME280#Humidity]

  //Calculate timestamp from RTC
  Let,1,[Notification#TimestampDay]*86400+[Notification#TimestampSec]
  Let,2,3 //set reminder frequency in minutes

  if [BatteryMon#Ubat]>=4.0
    TaskValueSet Notification,SendFlag,0 //Delete SendFlag
    event, CallDeepSleep

  elseif [BatteryMon#Ubat]<4.0 and [Notification#SendFlag]=0
    TaskValueSet Notification,SendFlag,1 //Set Send Flag
    notify 1, "Early Warning: The battery voltage of your ESP is: [BatteryMon#Ubat]V"
    timerSet,1,5 //set timer 1 to 5s, wait until notification is sent
  
  elseif [BatteryMon#Ubat]<3.5 and %unixtime%>[var#1]+[var#2]*60
    TaskValueSet Notification,TimestampDay,%unixday%
    TaskValueSet Notification,TimestampSec,%unixday_sec%
    notify 1, "Alert: The battery voltage of your ESP is: [BatteryMon#Ubat]V"
    timerSet,1,5 //set timer 1 to 5s, wait until notification is sent
  
  else
    event, CallDeepSleep
  endif
endon

on Rules#Timer=1 do
  event, CallDeepSleep
endon

on CallDeepSleep do
  if [DisablePD#State]=0  
    DeepSleep,60 //Send ESP to deep sleep for 60s
  endif
endon
BTW: There's an GPIO readback to disable the deepsleep if pulled to GND by external jumper. For maintenance purposes.

Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 35 guests