05.X update breaks custom sensors

Hoping someone can help.

On all 2023.4.X front ends, the sensors work perfectly, however, as soon as I update to 2023.5.X the custom sensors become unavailable.


> 
>   - platform: template
>     sensors:
> # Sensor to check from a goodwe sensor what the differential is between buy and sell
>       munic_meter_reading_differential:
>         friendly_name: 'Buy Sell Differential (Calculation)'
>         value_template: "{{ (states('sensor.energy_buy_sum')|float - states('sensor.energy_sell_sum')|float)|round(3) }}"
>         unit_of_measurement: "kWh"
>         
> # Sensor for Munic Power Current Reading at the meter
>       munic_meter_reading:
>         friendly_name: 'Munic meter current reading'
>         value_template: "{{ (states('counter.munic_calibration_meter_reading')|float + states('sensor.munic_meter_reading_differential')|float)|round(3) }}"
>         unit_of_measurement: "kWh"
>         
> # Sensor to check the meter status (How many are actualy used)
>       previous_munic_meter_reading:
>         friendly_name: 'Munic electricity usage'
>         value_template: "{{ (states('sensor.munic_meter_reading')|float - states('counter.munic_previous_meter_reading')|float)|round(3) }}"
>         unit_of_measurement: "kWh"
>

What’s different in version 2023.5.X is that when you include the unit_of_measurement option, the sensor’s value must be numeric. Your sensor values appear to be numeric (although when using the float filter you should supply it with a default value) so there must be something else. Are there any related error messages in the Log?

The alert only says the entity isn’t available. Unfortunately no other information is given.

Bases values are applied using helpers, maybe this is where I’m going wrong…
Where would I put the default value? In the sensors script?

Wherever you use float in your templates, replace it with float(0) if you want the default value to be zero. For example:

value_template: "{{ (states('sensor.energy_buy_sum')|float(0) - states('sensor.energy_sell_sum')|float(0))|round(3) }}"

If the value of either sensor.energy_buy_sum or sensor.energy_sell_sum is unavailable or unknown, it will be replaced by zero. So let’s say the value of both sensors is non-numeric, the calculation becomes (0 - 0) | round(3) which will report zero.

Thank you @123

I’ll give it a go and let you know.
Appreciate your help!

It looks like this is all related to the breaking change in the Riemann sum calculations, where if the lookup sensor hits “0” the state changes to unavailable, and breaks the integration.