Help with template syntax - 3 sensors

Hi All,

probably a basic question here - I have searched for an answer and looked at the templating documentation but I cant get this to work.

I have 3 sensors relating to electricity consumption (kwh) and cost of that consumption (price per unit in GBP and daily standing charge in GBP.

I need to express the following as a template : (sensor 1 * sensor 2 + sensor 3) and round the result to 2 decimal places.

Honestly I dont know where to start.

this is what I have but I know its wrong! -

value_template: "{{ (states('sensor.one')) * (states('sensor.two')) + (states('sensor.three')) }}"

with this syntax i get no errors but the sensor value is unavailable.

all help appreciated.

Use the developer tools templates to test your templates.

I believe you need to add float filters.

value_template: "{{ states('sensor.one') | float * states('sensor.two') | float+ states('sensor.three') | float }}"
1 Like

yeh tried that - while it builds the sensor I get no values out of it. if I check it in the template editor I get expected token got ‘string’

We can’t help you if we don’t see what the issue is

try

value_template: "{{ expand('sensor.one', 'sensor.two', 'sensor.three') | map(attribute='state') | select('is_number') | map('float') | sum }}"

edit: just realized it’s not a summation, try

value_template: >
  {% set a, b, c = expand('sensor.one', 'sensor.two', 'sensor.three') | map(attribute='state') | map('float', 0) | list %}
  {{ a * b + c }}

sorry no, spoke too soon. I need to multiple sensor 1 and sensor 2 then add the value of sensor 3. I think this just sums them all?

yes, see my last edit

That’s a great solution, very well written. That’s one of the things I love about visiting here, seeing things like this and getting ideas of how to maximize script coding.

thanks - though I get the following error - ValueError: not enough values to unpack (expected 3, got 2)

I assume this is related to the output of one of sensors.

for reference the current output from the 3 sensors is:

Screenshot 2022-07-12 at 17.30.54

that means your entity_id isn’t correct for 1 of the 3.

ah yes - found the issue.

I now get a value but its not the result of the calculation, its produces a result of 12 or so. when it should produce 2.11 or so.

Bit confused !

so manual calc is 12 (usage in kwh) * 0.15gbp + 0.25gbp = 2.11 (rough figures)l

make sure the order is correct.

yeh I tried changing the order of sensor 1 and 2 but still same result. baffling. could it be to do with the differing number of decimal places per sensor?

EDIT - tried changing the arguments to sensor 1 + sensor 2 + sensor3 and got the same result - its just summing them I think, seems to ignoring the *

just an update on this - couldnt get the @petro solution to work (proabably something i did incorrectly!) so i reverted to messy syntax and ended up with this:

{{((states('sensor.one')| float(0) * states('sensor.two')| float(0))) + states('sensor.three') | round(2) }}

it works but I cant control the decimal places - it fluctuates between two and a much longer string. anyone know how to tame this?

How about you post the actual template you’re attempting instead of fake sensor names.

i would but the sensor names contain my smart meter MPAN .

for me this is solved as it can be for now - I can force additional rounding using template love lace cards so its all good. thanks for the support.