Aigars Mahinovs: Figuring out finances part 4
At the end of the last part of this,
we got a Home Assistant OS installation that contains in itself a Firefly III instance and that
contains all the current financial information. Now I will try to connect the two.
While it could be nice to create a fully-featured integration for Firefly III to Home Assistant
to communicate all interesting values and events, I have an interest on programming a more
advanced data point calculation for my budget needs, so a less generic, but more flexible approch
is a better one for me. So I was quite interested when among the addons in the Home Assistant
Addon Store I saw AppDaemon - a way
to simply integrate arbitrary Python processing with Home Assistant. Let's see if that can do
what I want.
For start, after reading the tutorial
, I wanted to create a simple script that would use
Firefly III REST API to read the current
balance of my main account and then send that to Home Assistant as a sensor value, which then
can be displayed on a dashboard.
As a quick try I modified the provided hello_world.py that is included in the default
AppDaemon installation:
import requests from datetime import datetime import appdaemon.plugins.hass.hassapi as hass app_token = "<FIREFLY_PERSONAL_ACCESS_TOKEN>" firefly_url = "<FIREFLY_URL>" class HelloWorld(hass.Hass): def initialize(self): self.run_every(self.set_asset, "now", 60 * 60) def set_asset(self, kwargs): ent = self.get_entity("sensor.firefly3_asset_sparkasse_main") if not ent.exists(): ent.add( state=0.0, attributes= "native_value": 0.0, "native_unit_of_measurement": "EUR", "state_class": "measurement", "device_class": "monetary", "current_balance_date": datetime.now(), ) r = requests.get( firefly_url + "/api/v1/accounts?type=asset", headers= "Authorization": "Bearer " + app_token, "Accept": "application/vnd.api+json", "Content-Type": "application/json", ) data = r.json() for account in data["data"]: if not "attributes" in account or "name" not in account["attributes"]: continue if account["attributes"]["name"] != "Sparkasse giro": continue self.log("Account :" + str(account["attributes"])) ent.set_state( state=account["attributes"]["current_balance"], attributes= "native_value": account["attributes"]["current_balance"], "current_balance_date": datetime.fromisoformat(account["attributes"]["current_balance_date"]), ) self.log("Entity updated")It uses a URL and personal access token to access Firefly III API, gets the asset accounts information, then extracts info about current balance and balance date of my main account and then creates and/or updates a "sensor" value into Home Assistant. This sensor is with metadata marked as a monetary value and as a measurement. This makes Home Assistant track this value in the database as a graphable changing value. I modified the file using the File Editor addon to edit the
/config/appdaemon/apps/hello.py file. Each
time the file is saved it is reloaded and logs can be seen in the AppDaemon Logs section - main_log for
logging messages or error_log if there is a crash. Useful to know that
requests library is included, but it hard to see in the
docks what else is included or if there is an easy way to install extra Python packages.
This is already a very nice basis for custom value insertion into Home Assistant - whatever you can with
a Python script extract or calculate, you can also inject into Home Assistant.
With even this simple approach you can monitor balances, budgets, piggy-banks, bill payment status and
even sum of transactions in particular catories in a particular time window. Especially interesting data
can be found in the insight section of the Firefly III API.
The script above uses a trigger
like self.run_every(self.set_asset, "now", 60 * 60) to simply run once per hour. The data in Firefly will not
be updated too often anyway, at least not until we figure out how to make bank connection run automatically
without user interaction and not screw up already existing transactions along the way. In theory a
webhook API of the Firefly III
could be used to trigger the data update instantly when any transaction is created or updated. Possibly
even using Home Assistant
webhook integration. Hmmm. Maybe.
Who am I kiddind? I am going to make that work, for sure! :D But first - how about figuring out the future?
So what I want to do? In short, I want to predict what will be the balance on my main account just
before the next months salary comes in. To do this I would:
- take the current balance of the main account
- if this months salary is not paid out yet, then add that into the balance
- deduct all still unpaid bills that are due between now and the target date
- if the credit card account has not yet been reset to the main account, deduct current amount on the cards
- if credit card account has been reset, but not from main account deducted yet, deduct the reset amount
I ve made it again. And again. And a few more times, and now it has an official household name, Pan Sbagliato , or Wrong Bread .
And this is the procedure I ve mostly settled on; starting on the day before (here called Saturday) and baking it so that it s ready for lunch time (on what here is called Sunday).
Saturday: around 13:00
I ve had up to a couple of hours variations in the times listed, with no ill effects.
Yang the cat bird
KDE snap Kamoso
This is a Public Service Announcement that my
I just noticed that I wrote this a decade ago, and then never got round to posting
it, so thought I might kick it off now to mark my tentative return to blogging.
At the


My stall. Photo credits: Tejaswini.
Snacks and tea at the front desk. CC-BY-SA 4.0 by Ravi Dwivedi.
Pictionary drawing nowhere close to the intended word Wireguard :), which was guessed. Photo by Ravi Dwivedi, CC-BY-SA 4.0.
Group photo. Photo credits: Tejaswini.
Tasty cake. CC-BY-SA 4.0 by Ravi Dwivedi.
Merchandise by sflc.in. CC-BY-SA 4.0 by Ravi Dwivedi.
Thunderbird in Debian stable (Bookworm) has received
This needs a user CSS file to override the button as non-visible.
To make it process the user CSS Thunderbird needs a config setting to be enabled:
The eighth release of the still fairly new