Template sensor from imap

I want to extract my electricity bill amount from an email that i recieve. The body of this email is part of the entity state attribute but this is in html format like below. How can I use regex to extract the Net Payable Amount from this? Also the due date

<html>
	<div style=3D'font-size: 0; line-height: 0; text-align: center'>
		<img src=3D'../ui/img/kseblogo_app.png' border=3D0 alt=3D'KSEBL-OrumaNet'=
>
	</div>
	<div style=3D'width:700px;font-size: 10pt;text-align:left;'>
		<span>Dear Consumer,</span><br>
		<span>&nbsp;&nbsp;&nbsp;Your KSEB Bill is attached in PDF format.</span>
	</div>
	<br>
	<div>
	<table width=3D'700px' cellspacing=3D'0' align=3D'left' cellpadding=3D'5px=
' border=3D'1' style=3D'font-size:11pt'>
	<tr height=3D'40px'>
		<td align=3D'center' colspan=3D'2' style=3D'border:0px; background-color:=
#00689D; color:white;'>
			<b>BILL ABSTRACT</b>
		</td>
	</tr>
	<tr height=3D'30px'>
		<td width=3D'40%'>Consumer Number</td>
		<td width=3D'60%'><b>1168</b></td>
	</tr>
	<tr height=3D'30px'>
		<td width=3D'40%'>Customer Name</td>
		<td width=3D'60%'><b>AN</b></td>
	</tr>
	<tr>
		<td>Ele. Section</td>
		<td><b>Katam [4521]</b></td>
	</tr>
	<tr>
		<td >Bill Number</td>
		<td ><b>4531340</b></td>
	</tr>
	<tr>
		<td >Bill Amount</td>
		<td ><b>1193.00</b></td>
	</tr>
	<tr>
		<td >Bill Date</td>
		<td ><b>06-06-2022</b></td>
	</tr>
	<tr>
		<td >Due Date</td>
		<td ><b>16-06-2022</b></td>
	<tr>
		<td >Disconnection Date</td>
		<td ><b>01-07-2022</b></td>
	</tr>
	</tr>
	<tr>
		<td><b>Net Payable Amount </b></td>
		<td style=3D'font-size:18px;'><b>1533.00</b></td>
	</tr>
	<tr>
		<td colspan=3D'2' align=3D'center' ><span style=3D'font-size:14px;text-al=
ign:center;color:#4D4D4D'>For Quick Pay,Registration and  Online Payment, P=
lease visit <a style=3D'color:#00689D;' target=3D'_blank' href=3D'https://w=
ss.kseb.in/selfservices/qr?wss=3D1145210028368~202206~9847642825~074ab9437e=
f4de0f190e423ce2f0ddc1'>wss.kseb.in</a></span>
	</tr>
	<tr>
		<td colspan=3D'2' align=3D'center' ><span style=3D'font-size:14px;text-al=
ign:center;color:#4D4D4D'>For Download KSEB mobile app, Please visit <a sty=
le=3D'color:#00689D;' target=3D'_blank' href=3D'https://play.google.com/sto=
re/apps/details?id=3Dcom.mobile.kseb&hl=3Den_IN'>KSEB</a></span>
	</tr>
	<tr>
		<td colspan=3D'2' align=3D'center'>
			<i><span style=3D'font-size:12px;text-align:center;color:#a2a2a2'>This m=
essage is generated electronically. Please do not respond to this mail.</i>=
</span></td>
		</td>
	</tr>
	</table>
	</div>
</html>

Most likely not the most efficient way to do it (I suck at regex) but it works on your test sample:

Bill amount (assuming it can never be negative):

  {{ state_attr("sensor.your_sensor", "your_attribute") | regex_findall_index(find="Bill Amount<\/td>\D+<td ><b>(\d+\.\d\d)", index=0, ignorecase=False) }}

If it can be negative:

  {{ state_attr("sensor.your_sensor", "your_attribute")  | regex_findall_index(find="Bill Amount<\/td>\D+<td ><b>(-?\d+\.\d\d)", index=0, ignorecase=False) }}

Due Date:

  {{ state_attr("sensor.your_sensor", "your_attribute")  | regex_findall_index(find="Due Date<\/td>\D+<td ><b>(\d\d-\d\d-\d{4})", index=0, ignorecase=False) }}

Note that find= can be picky about using single or double quotes. It usually requires double. Use a multi-line template if you have to.

I used this to test the expressions: https://regex101.com/

1 Like

I tried both the template but it gives the error IndexError: list index out of range.
Also I am trying to get Net Payable Amount an not Bill amount.

Show the sensor config you tried.

It worked. There was an issue with my testing. I got the net payable amount with

 {{ state_attr("sensor.rivana_email", "body") | regex_findall_index(find="Net Payable Amount <\/b><\/td>\D+<td style=3D'font-size:18px;'><b>(\d+\.\d\d)", index=0, ignorecase=False) }}
1 Like