A few weeks ago, I decided to track my credit card expenses in Home Assistant, and also notify me or my wife if any one of us made a payment from the shared bank account. The goal was to track our expenses without having to manually add them in an app, and because our bank doesn't allow other apps to integrate with it, I found two ways of doing this:
- Reading notifications from the bank with Tasker (if you have an Android device)
- Reading email receipts from the bank with the "IMAP Email Content" integration from Home Assistant
I'm using an Android device but my wife has an iPhone, so the second option works better for us. We use a Curve credit card which allows us to switch between different cards, and it also sends an email with every receipt. This means that no matter what card we pick in the Curve app, we'll have the transaction in our email. From here on, it gets easy, I just have to:
- Read emails from Curve with the "IMAP Email Content" sensor
- Extract the amount and the seller
- Display the information in Home Assistant
Reading emails with the "IMAP Email Content" sensor
I have a folder in my Gmail account for Curve receipts, so I just have to detect when a new email arrives in that folder. Here's how this can be done in Home Assistant:
- platform: imap_email_content
name: curve_email
server: imap.gmail.com
port: 993
username: !secret gmail_email
password: !secret gmail_password
folder: Receipts
senders:
- support@imaginecurve.com
The folder is called "Receipts", but in my case I also have Uber receipts in there, so I choose to only read emails that come from Curve.
Extracting the amount and the seller
If it makes this easier to understand, here is how an email from Curve looks like:
I'm interested only in the amount and the seller(highlighted above) so I can display them in Home Assistant. In order to do this, I create a template sensor that retrieves the information I need.
last_expense:
friendly_name: Last expense
unit_of_measurement: RON
value_template: >
{{ state_attr('sensor.curve_email','subject') | regex_findall_index("\d+\.?\d+? RON") | replace(" RON","") | round(2) }}
attribute_templates:
seller: >
{{ state_attr('sensor.curve_email','subject')[27:1000] | regex_findall_index(".* on") | replace(" on","")}}
I'll be honest with you, I suck at regex, so if you have a better way of extracting this information, please go ahead :) For me this works pretty well, so I can set the value of the sensor to the amount spent, and I also have an attribute called "seller" that tells me where I spent that amount.
Display the information in Home Assistant
Once we have this information in Home Assistant, we can display it however we want. I have a dashboard with my expenses that not only includes the credit card expenses but also shows how much gas and energy I spent so far for the current month. I decided to use a logbook card that shows a list of the payments we made along with two gauges that show the monthly and daily amounts spent. This way we can track if we're on budget and see what the latest expenses are without opening our banking app.
I also implemented a mobile notification to be sent to both our phones whenever a purchase is made. However, this backfired when I bought a Christmas gift for my wife and she got notified with the amount and the store, so she immediately realized what I was doing 😂 So if you want to implement this as well, you might want to consider a "pause" functionality for tracking expenses for situations like this one.