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.
I need to be able to set the current value of the pre-paid meter. This is simple through a Counter.
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.
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.
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.
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.
sensor:
- platform: statistics
name: "Power used last 24 hours"
entity_id: sensor.household_electricity
state_characteristic: value_max
max_age:
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
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:
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.
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.
Open your configuration.yaml file through your File Editor and add the following lines:
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.
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:
household_electricity (to calculate your consumption for the day)
counter.pre_paid_remaining (your HomeAssistant balance calculated at midnight)
input_number.pre_paid_balance (where you will insert your actual balance from your meter)
input_number.pre_paid (the amount of kWh credits you have purchased)
input_button.load_pre_paid (to trigger your 2nd Automation above)
Some caveats:
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.
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.
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?