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
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.
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.
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.