Why is this simple calculation returning 0.0?

I am at a loss here.

I have the same thing for normal power and they are working fine.

For some reason, HA thinks that 0.994 x 0.08 is 0.0.

I wacked this into th template editor as per below, but the screen shot of the values are also shown.

{{ states(‘sensor.daily_hot_water_tariff_peak’) }}
{{ states(‘sensor.daily_hot_water_tariff_peak’) | int * 0.2079 | round(2) }}

{{ states(‘sensor.daily_hot_water_tariff_offpeak’) }}
{{ states(‘sensor.daily_hot_water_tariff_offpeak’) | int * 0.08 | round(2) }}

Order of operations. You are rounding the number 0.2079. Use some parentheses.

Also your sensor contains a floating point number, not an integer. Try this:

{{ (states('sensor.daily_hot_water_tariff_peak') | float * 0.2079) | round(2) }}

In future please use the </> button on the post toolbar to format your pasted config for the forum.

Because this
{{ states(‘sensor.daily_hot_water_tariff_offpeak’) }}
you are showing is = to 0.994.

That you are filtering to an integer, which gives you 0.

The int filter strips off the decimal, it doesn’t round. That is :

Why is this simple calculation returning 0.0

So as Tom said, if you filter it as a float, you will probably get what you expected.

Cool - thanks for tht.

I have int in many places, and a few floats and things… Just learning and copying other peoples stuff and trial and error…

I am backing up now, then will hit all these int’s…

Thanks again - all the extr brackets added, and all the int changes to float (not I understand what the int was doing - luckily most of my sensors that had it are whole numbers… but I will change all them too - I may be missing out on fractions of watts here and there…

Also note that this expression, and thus your automation or wherever you are using it, will fail and error out if the sensor goes unavailable. This because the state string “unavailable” cannot be cast to a float.

It is good practice to either add a default value, something like float(0) and precautions if this makes the result unexpected, or an additional has_value() check.

This would return none / null if the sensor is unavailable, rather than error out:

{{ (states('sensor.daily_hot_water_tariff_peak') | float * 0.2079) | round(2)
if has_value('sensor.daily_hot_water_tariff_peak') }}

Although if this is used in a sensor with a unit or device class, any non-numeric state will give an error.

The best precaution for sensors is to use an availability template, which does mean you (currently) have to configure it using YAML.