Query arbitrary account balance in automated posting

86 views
Skip to first unread message

Richard Grayson

unread,
Sep 1, 2019, 2:08:43 AM9/1/19
to Ledger
Hi all,

I'm looking to set up a variant of envelope budgeting with Ledger, where a certain percentage of every Income gets put into an envelope unless the envelope has reached a certain threshold. For example, I may want to save up no more than a year's worth of monthly payments to a $5 subscription, so I would do something like

= /^Income/ and balance(Assets:Budgets:Foo) <= $60
    [Assets:Budgets:Foo]    0.1
    [Assets:Savings]            -0.1

I've scoured the ledger docs and random blog posts and can't seem to find a way to do this. Is it something that's possible and, if so, how?

John Wiegley

unread,
Sep 2, 2019, 2:22:10 PM9/2/19
to Richard Grayson, Ledger
>>>>> "RG" == Richard Grayson <mbrya...@gmail.com> writes:

RG> = /^Income/ and balance(Assets:Budgets:Foo) <= $60
RG>     [Assets:Budgets:Foo]    0.1
RG>     [Assets:Savings]            -0.1

If such support existed -- and I'm not sure it does -- the syntax would likely
be:

= expr account =~ /^Income/ and account("Assets:Budgets:Foo").amount <= $60

John

Michael Bryant

unread,
Sep 14, 2019, 4:55:05 PM9/14/19
to Ledger
I thought I responded to this, but I guess I didn't.

I tried that syntax and it didn't work. I'm assuming the support just doesn't exist right now.

As maintainer of Ledger, would you be willing to merge a pull request to support querying account information if I were to try to implement it?
I imagine the best implementation would be exposing all information via a struct-like syntax (as in your example). I can also run example syntax
by you before implementing anything, if that makes things easier.

John Wiegley

unread,
Sep 17, 2019, 7:55:59 PM9/17/19
to Michael Bryant, Ledger
>>>>> "MB" == Michael Bryant <mbrya...@gmail.com> writes:

MB> As maintainer of Ledger, would you be willing to merge a pull request to
MB> support querying account information if I were to try to implement it? I
MB> imagine the best implementation would be exposing all information via a
MB> struct-like syntax (as in your example). I can also run example syntax by
MB> you before implementing anything, if that makes things easier.

I've pushed a commit to master that makes this work now:

= expr account =~ /^Income/ && account("Assets:Budgets:Foo").total <= $60
[Assets:Budgets:Foo] 0.1
[Assets:Savings] -0.1

2019-09-16 Payee
Assets:Budgets:Foo $40
Equity:Balances

2019-09-16 Payee2
Assets:Checking $100
Income

Turns out the support to do this _was_ there, it was just the query parser
that was broken (which is what gets used after '=' up there).

John

Michael Bryant

unread,
Oct 6, 2019, 3:30:34 PM10/6/19
to Ledger
Thanks! This works. Any idea when the fix will appear in a new release?

John Wiegley

unread,
Oct 6, 2019, 6:58:54 PM10/6/19
to Michael Bryant, Ledger
>>>>> "MB" == Michael Bryant <mbrya...@gmail.com> writes:

MB> Thanks! This works. Any idea when the fix will appear in a new release?

Given that we released recently, it may be a while...

John
Reply all
Reply to author
Forward
0 new messages