mkdr36
July 12, 2022, 2:40pm
1
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.
Hellis81
(Hellis81)
July 12, 2022, 2:46pm
2
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
mkdr36
July 12, 2022, 2:58pm
3
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’
Hellis81
(Hellis81)
July 12, 2022, 3:01pm
4
We can’t help you if we don’t see what the issue is
petro
(Petro)
July 12, 2022, 3:03pm
5
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 }}
mkdr36
July 12, 2022, 3:11pm
7
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?
CO_4X4
(Colorado Four Wheeler)
July 12, 2022, 3:42pm
9
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.
mkdr36
July 12, 2022, 4:31pm
10
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:
petro
(Petro)
July 12, 2022, 4:32pm
11
that means your entity_id isn’t correct for 1 of the 3.
mkdr36
July 12, 2022, 5:21pm
12
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
petro
(Petro)
July 12, 2022, 5:26pm
13
make sure the order is correct.
mkdr36
July 12, 2022, 7:52pm
14
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 *
mkdr36
July 12, 2022, 10:46pm
15
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?
petro
(Petro)
July 12, 2022, 10:51pm
16
How about you post the actual template you’re attempting instead of fake sensor names.
mkdr36
July 12, 2022, 10:55pm
17
i would but the sensor names contain my smart meter MPAN .
mkdr36
July 12, 2022, 11:02pm
18
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.