Pre-Paid Electricity Counter

Hi all, I need some help with Counters and Input Numbers.

At home we have pre-paid electricity. I recently installed a Shelly EM to pull our electrical consumption through to Home Assistant, but now I want to take it a step further by syncing the pre-paid balance (standalone device) with Home Assistant, to ping us when our balance goes below a certain threshold, and to track if the energy meter of the pre-paid device is in line with the consumption tracked by the Shelly.

So I require a couple of functions.

Screenshot 2022-07-23 111617

  1. I need to be able to set the current value of the pre-paid meter. This is simple through a Counter.

  2. I need to decrease the value of the counter based on the energy consumption measured by the Shelly. In my mind, I need to measure the daily energy consumed with a utility meter, and deduct that value from the counter value just before midnight each day. If there’s a better/more frequent way to manage this I’d love to hear it.

  3. When purchasing the credits, we get a certain amount of kWh for the value purchased - this fluctuates based on total monthly consumption. This seems possible by using an Input Number and an Input Button. This number needs to be added to the Counter value.

  4. I need to ping an alert to our phones if the Counter value goes below a certain threshold. This I can do through NodeRed.

My difficulty is that in principle I know how to do this, but I just cannot get the code right.

I only really need help on Items 2 & 3, as the other ones are straightforward.


Hi @bertusras ,

I am not familiar with the use of prepaid electricity. I am not sure though wether a counter is meant for this application, maybe a input_number would be fine too. However for a counter below is a possibilty.

For #2 something that pops in my mind is to use multiple automations.

Do you have a sensor with “power use yesterday”? If not you can use the statistics integration for that.

  - platform: statistics
    name: "Power used last 24 hours"
    entity_id: sensor.household_electricity
    state_characteristic: value_max
      hours: 24

This should work I guess, but if not here are some alternatives to get a value of a sensor of the day before: Get temperature from yesterday

Then you can do an automation daily at midnight to subtract the value of yesterday of the current pre-paid Remaining.

With the service counter.configure which is described here: Counter - Home Assistant you can execute an automation that sets the value of the counter each day at 0.00.

If you make an automation which is executed at 0:00 which executes the service counter.configure in which you set the value of the counter of something like this: value: {{ states('counter.pre_paid_remaining')|float - states('sensor.power_used_last_24_hours)|float }}

I think an automation executed on the button click with the service counter.configure can be used to achieve #3 too. You might want to think about an extra button to deduct a value if you accidentally hit the button twice or add the wrong value.

You might want to use a input_number instead of counter. Cause then it would be easier to edit the value of Pre-Paid remaining if something gets messed up. Then instead of counter.configure you can use input_number.set_value

Thanks for the help so far Johan.

When inputting the code below into the Services (call service) tab under Developer tools, it works perfectly:

service: counter.configure
  value: "{{ states('input_number.pre_paid') | int + states('counter.pre_paid_remaining') | int}}"
  entity_id: counter.pre_paid_remaining

But I cannot get it to work in NodeRed at all. I’m probably doing something incredibly stupid, but I keep getting the same “Call-service error. expected int for dictionary value @ data[‘value’]” error. I have tried all the configurations of moustache brackets and single quotes and double quotes to no avail.

I’m not sure if the Data field in NodeRed should be an Expression or JSON, but I’ve not been able to get either option to work.

This is what my NodeRed call service node looks like:

And this is the code in the expression:

   "value": "{{ states('input_number.pre_paid') | int + states('counter.pre_paid_remaining') | int}}"

It seems to be sending the sum as a string, but if I remove the double quotes it says that my expression is invalid, so I am stumped.

I’m probably doing something really stupid, but this is really not my forte.

Edit: If I can just solve the addition/subtraction problem I’ll be able to figure the rest of the stuff out. But I’ve now wasted 2 hours tonight on this silly issue which someone else is probably laughing at me about.

Okay seems like I’ve been able to get it working by using the built-in Home Assistant Automations, however I would like to understand how to implement it in NodeRED as well.

Allright Bertus, good to hear you got it working, at least in native automations.

I don’t have any experience with NodeRed, however maybe this thread will help you out: Call service node Data inject msg.payload

1 Like

Hi Bertus
Would you be so kind as to share this project.
I have a IAMETER, and separate prepaid meter.
Would like to do exactly the same.

But unlike you I have no idea where to start even

Hi Louis, apologies I only saw your reply now. Unfortunately one of the recent Home Assistant updates broke my pre-paid tracker, so I’ll need to figure out what went wrong before I can give you a proper answer.

Okay so I’ve managed to get my counter working again, but I get this warning in HomeAssistant which is part of the issue that needs to be resolved.


I haven’t had time to look into it yet, but let me give you the bones of the current setup and you might be able to figure out how to resolve the issue above when it comes to it.

  1. Open your configuration.yaml file through your File Editor and add the following lines:
    source: sensor.shelly_em_channel_1_energy
    cycle: daily

Note: You will need to change your source to match your IAMETER configuration.


This household_electricity meter will start at 0kWh and count up during the day, and reset back to 0kWh at midnight each day. We will use this total consumption at midnight to deduct from our pre-paid balance.

You can ignore the first three above, as I use those to keep track of grow light energy consumption.

  1. Create 4 New Helpers

Settings – Devices and Services – Helpers Tab

  • Button – Label: Load Pre-Paid – Entity ID: input_button.load_pre_paid
  • Number – Label: Pre-Paid Balance – Entity ID: input_number.pre_paid_balance
  • Number – Label: Pre-Paid Credits – Entity ID: input_number.pre_paid
  • Counter – Label: Pre-Paid Remaining – Entity ID: counter.pre_paid_remaining


  1. Create your automations

Settings – Automations & Scenes – Automations Tab

  1. Create First Automation

This automation will deduct the value of your household_electricity meter from your pre-paid balance just before midnight each day

  • Name: Deduct Pre-Paid
  • Trigger: When the time is equal to 11:59:59 PM
  • Action:
service: counter.configure
  value: >-
    {{ states('counter.pre_paid_remaining') | int -
    states('sensor.household_electricty') | int}}
  entity_id: counter.pre_paid_remaining

Save Automation

  1. Create Second Automation

This automation will do 4 things:

  1. It will match your actual electrical meter prepaid balance to your HomeAssistant balance
  2. It will take your actual prepaid balance and top it up with the kWh that you have purchased
  3. It will reset your daily household_electricity counter to zero
  4. It will reset your input values for your actual pre-paid balance and credits loaded to zero
  • Name: Recharge Pre-Paid
  • Trigger: State – Entity: input_button.load_pre_paid
  • Create 4 Actions
  1. Counter: Pre-Paid Remaining
service: counter.configure
  value: "{{ states('input_number.pre_paid_balance') | int}}"
  entity_id: counter.pre_paid_remaining
  1. Counter: Pre-Paid Remaining
service: counter.configure
  value: >-
    {{ states('input_number.pre_paid') | int +
    states('counter.pre_paid_remaining') | int}}
  entity_id: counter.pre_paid_remaining
  1. Utility Meter: Calibrate household_electricity

You can program this one visually as per the below


Or with yaml as per the below:

service: utility_meter.calibrate
  value: "0"
  entity_id: sensor.household_electricty
  1. Counter: Pre-Paid Remaining

You can program this one visually as per the below


Or with yaml as per the below:

service: input_number.set_value
value: 0
- input_number.pre_paid_balance
- input_number.pre_paid

  1. Create your front-end to load new credits and check your HomeAssistant balances

On your overview page (or wherever you’d like to), create a new tab for Energy by editing your Dashboard.

Add a new Entities Card

You can title it however you’d like: i.e. Pre-Paid Electricity

Under Entities you need to load 5 items:

  1. household_electricity (to calculate your consumption for the day)
  2. counter.pre_paid_remaining (your HomeAssistant balance calculated at midnight)
  3. input_number.pre_paid_balance (where you will insert your actual balance from your meter)
  4. input_number.pre_paid (the amount of kWh credits you have purchased)
  5. input_button.load_pre_paid (to trigger your 2nd Automation above)


Some caveats:

  1. The Shelly meter and the pre-paid meter doesn’t always match, why, I don’t know, can only imagine that loadshedding plays a huge part.
  2. The calculations aren’t perfect. We’re also working with real numbers and integers, and unfortunately the counter only works with integers, so you lose some decimals in the process.

What you can also do with this is to set up triggers in NodeRED to push notifications to your phone if your calculated pre-paid balance is below a certain threshold. We’ve currently got it set so that once it dips below 70kWh balance it sends a first reminder, and once it dips below 45kWh it sends a critical reminder. Note, this will only trigger once a day at midnight as it doesn’t continuously reduce your HomeAssistant calculated balance, only at midnight.

Anyways, I hope this helps (and that you actually see it since you posted in December and I totally missed it until today when I came looking for a potential fix for the issue right at the top.

1 Like

Hey, I’m currently searching for a solution to this too, I have fitted an impulse monitor , (frient energy consumption monitor) to start tracking the usage, but it makes it difficult to see the £ balance on my meter, not WAF friendly and would like to get a balance counter set up on a tablet for the home.
Do you still have your automations you created?

I’m also in the “I have no idea where to start” category

Hi everyone. Anyone managed to make it work? It would be very much appreciated if anyone can share a working prepaid electricity. Thanks in advance.