Improve template to sum untracked electricity use

I’m on the hunt for when and where electricity is used in the house that isn’t tracked by my existing devices.

I have the following code to sum all of the trackers and deduct the value from the total household usage, but have a couple of problems with it:

  • Firstly, total only refreshes every 15 seconds of so, whilst tracked refreshes whenever any of the included entities updates, which can lead to significant -ve values. Is there anything smarter than just | abs to keep the value +ve?

  • Secondly, and more importantly, occasionally a sensor will become unavailable, not sure why, it’s on the list, but obviously you can’t | float a value of unavailable so I was wondering how this could be guarded against, using code that is easy to read and efficient

        {% set total = (states('sensor.household_power') | float) %}
        {% set tracked = (states('sensor.shelly_shem_c45bbe79043f_1_current_consumption')| float +
                  states('sensor.shelly_shem_c45bbe79043f_2_current_consumption')| float +
                  states('sensor.heat_recovery_system_power_usage')| float +
                  states('sensor.serverrack_energy_power')| float +
                  states('sensor.shelly_shem_c45bbe77e1b0_1_current_consumption')| float +
                  states('sensor.shelly_shem_c45bbe77e1b0_2_current_consumption')| float +
                  states('sensor.dishwasher_energy_power')| float +
                  states('sensor.heat_recovery_system_preheater_power_usage')| float +
                  states('sensor.washing_machine_energy_power')| float +
                  states('sensor.computer_desk_energy_power')| float +
                  states('sensor.shelly_shem_c45bbe79319d_1_current_consumption')| float +
                  states('sensor.shelly_shem_c45bbe79319d_2_current_consumption')| float)
        %}
        {{ (total - tracked) | round(2) }}  

Very appreciative of your thoughts

Why is that not “smart” enough?

It’s certainly the easiest method.

Use an availability template. You don’t mention if you are using the legacy template sensor platform or the the template integration and you have not shown the whole configuration, so I can’t give you an example. It’s in the docs.

Well,

wouldn’t ABS convert a negative number to it’s positive equivalent? It would be more appropriate to zero a negative value.

I’m using the template functionality in the dev tools just now.

Yes that is what the absolute value filter/function does. If you want negative values to be zero then you have to test for a negative value and replace it with zero. There is no function/filter for that.

Your availability template would look something like this:

        {{ states('sensor.household_power')|is_number and
          states('sensor.shelly_shem_c45bbe79043f_1_current_consumption')|is_number and
          states('sensor.shelly_shem_c45bbe79043f_2_current_consumption')|is_number and
          states('sensor.heat_recovery_system_power_usage')|is_number and
          states('sensor.serverrack_energy_power')|is_number and
          states('sensor.shelly_shem_c45bbe77e1b0_1_current_consumption')|is_number and
          states('sensor.shelly_shem_c45bbe77e1b0_2_current_consumption')|is_number and
          states('sensor.dishwasher_energy_power')|is_number and
          states('sensor.heat_recovery_system_preheater_power_usage')|is_number and
          states('sensor.washing_machine_energy_power')|is_number and
          states('sensor.computer_desk_energy_power')|is_number and
          states('sensor.shelly_shem_c45bbe79319d_1_current_consumption')|is_number and
          states('sensor.shelly_shem_c45bbe79319d_2_current_consumption')|is_number }}

Before you go any further using those entities you might want to change the entity_ids to something more descriptive.

Thanks, is it then a FOR LOOP to test the availability template and sum those which return true?

No, that is the entire availability template. You put it in this option:

https://www.home-assistant.io/integrations/template/#availability

It causes the sensor to return an unknown state if any of the sensors it relies on are non numeric (unknown or unavailable).

Ok, but I’m trying to sum the state of the available entities. I’m not clear how that fits in?
Thanks for your help with this

Well that’s even easier. Just supply a default value of zero for your float filters. If the states can’t be converted to numbers they are replaced with zero.

        {% set total = (states('sensor.household_power') | float(0)) %}
        {% set tracked = (states('sensor.shelly_shem_c45bbe79043f_1_current_consumption')| float(0) +
                  states('sensor.shelly_shem_c45bbe79043f_2_current_consumption')| float(0) +
                  states('sensor.heat_recovery_system_power_usage')| float(0) +
                  states('sensor.serverrack_energy_power')| float(0) +
                  states('sensor.shelly_shem_c45bbe77e1b0_1_current_consumption')| float(0) +
                  states('sensor.shelly_shem_c45bbe77e1b0_2_current_consumption')| float(0) +
                  states('sensor.dishwasher_energy_power')| float(0) +
                  states('sensor.heat_recovery_system_preheater_power_usage')| float(0) +
                  states('sensor.washing_machine_energy_power')| float(0) +
                  states('sensor.computer_desk_energy_power')| float(0) +
                  states('sensor.shelly_shem_c45bbe79319d_1_current_consumption')| float(0) +
                  states('sensor.shelly_shem_c45bbe79319d_2_current_consumption')| float(0))
        %}
        {{ (total - tracked) | round(2) }}  

You should always supply defaults for all your template filters and functions. Otherwise your templates may not load if they encounter an error.

1 Like

ah, didn’t know that! Thanks