Use template sensor to calculate remaining amperage

Hello,

I’m trying to create a template sensor which shows me my remaining amps on my power input in my house. I know i’ve got a 25 main fuse, and homeassistant can already see how much amps are being used right now using my smart meter (p1). I’ve got a sensor which shows me the currently used amps, but how can I create a template sensor which shows me the amount of amps I still have availible? I’m going to use this to support an EVSE charger with the amount available, so that i can charge my car using the full amount available.

Thanks!

This is what I do for conversions. I am only able to do one math operations at a time. I haven’t figured it out. This is in my sensor.yaml file

# Convert entity to amps remaining
      remaining_amps_0:
        friendly_name: "Remaining Amps 0"
        unique_id: "Home remaining Amps 0"
        value_template: "{{ states('sensor.main_amp')|float - 200 }}"  
#your sensor name replacing sensor.main_amp.  Also assume house has 200amp main panel.

      remaining_amps:
        friendly_name: "Remaining Amps"
        unique_id: "Home Remaining Amps"
        value_template: "{{ states('sensor.remaining_amps_0')|float * -1 }}"

That explains the example you had posted elsewhere, for temperature conversion, where you inexplicably split the calculation over two sensors. :thinking:

Consolidating the two calculations is as simple as this:

value_template: "{{ (states('sensor.main_amp')|float - 200) * -1 }}"

However, I am not sure why you chose to calculate it that way. Surplus amperage is simply maximum minus consumed. For rvoosterhaut’s application, the Template Sensor would be simply this:

- platform: template
  sensors:
    remaining_amps:
      value_template: "{{ 25 - states('sensor.whatever')|float }}"

Mostly because I am still learning and haven’t figured all of the syntax out. Engineer but not a software engineer. Thanks for the pointers.

I’ll assume you’re making a self-deprecating joke; no software engineering degree is needed to grok templating (but some exposure to a STEM curriculum certainly helps).

No malice intended at all. My last programming class was fortran 66. Much has changed since then and I just try my best and help where I can (There is no better learning experience than trying to teach). A degree (or training) is not required but it certainly helps. Engineers are not known for their linguistic skills and all programming is in a foreign language until you learn.

Thanks again for your feedback and best regards.

Join the club :slight_smile:

And punch cards.

Text output was a particular issue.

Mine was like Assembler on OS/390 :smiley:

That didn’t prevent me to self-learn C / C++ / Java / Python / Whatever language of the day, though :wink:
Syntax is just a detail. Once you have the grasp on how 3rd generation programming languages work (the “functional” ones are another beast), only the keywords / block delimiters change, basically…

1 Like

This value_template is working, thanks! Now i’m trying to create a negative value. So i’ve got a 25 amp fuse. When i’m using 10 amp, it should say -15 availble.
I thought it was a easy as switching the formula around, but that doesn’t seem to work

value_template: "{{ ('sensor.p1_l1_huidig_amperage_verbruik') - 25|float }}"

What did I miss?

|float is meant to convert your sensor to a float. Here, your are converting 25 to a float

value_template: "{{ states('sensor.p1_l1_huidig_amperage_verbruik')|float- 25 }}"

EDIT: and you forgot the “states” keyword

Thank you very much. What is a float sensor?

Floating-point value, aka “float”, is one of the elementary type in any programming languages (Fortran included :wink: ), alongside integers (“int”), and strings.

in HA, all states are inherently strings, upon which you cannot do math operations like minus.
The |float operation instructs HA to first convert the inherently string state to a float value, which then allow you to to the - 25 math operation.

Thats awesome, thanks for the explanation! I still have lot to learn.

You’re welcome. Please set @123 post as solution. I was merely commenting, here.

If the sensor’s name is remaining amperage then the value should be positive, not negative. A negative value implies a deficit of a resource, not a surplus. The remaining capacity for your 25 ampere circuit is a positive value.

Simple example: your car’s fuel gauge indicates a positive amount (not negative).

Your template isn’t using the float filter correctly.

The state value of any entity is a string (which means it is text and not a number such as an integer or a floating-point value). The purpose of using the float filter is to convert the string to a floating-point value. Only then can you use it in a calculation.

If really want to show a negative value for remaining capacity, either of these works but the second form is preferable:

value_template: "{{ sensor.p1_l1_huidig_amperage_verbruik.state | float - 25 }}"
value_template: "{{ states('sensor.p1_l1_huidig_amperage_verbruik') | float - 25 }}"

BTW, as per koying’s latest comment, it’s the custom of this community to mark the first post that identifies the problem and suggests how to correct it (not a subsequent post that duplicates it).

1 Like