Multiply two sensor values

Hi,

I am trying to multiply two sensor values.I got the following code so far (which doesn’t work)

  - platform: template
    sensors:
      helium_wallet_value:
        value_template:
          "{{ sensor.helium_hnt_oracle_price * sensor.helium_wallet_ABC }}"  

The individual sensors itself work fine and provide the following values:

image

The error logs says the following:

2021-12-12 20:47:38 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'sensor' is undefined when rendering '{{ sensor.helium_hnt_oracle_price * sensor.helium_wallet_ABC }}'
2021-12-12 20:47:38 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ sensor.helium_hnt_oracle_price * sensor.helium_wallet_ABC }}")
  - platform: template
    sensors:
      helium_wallet_value:
        value_template: "{{ states('sensor.helium_hnt_oracle_price') | float(0) * states('sensor.helium_wallet_ABC') | float(0) }}"  

Thanks. But that doesn’t make a difference. I more assume that the sensors I try to multiply are not zet available at the point in time I multiply them ( timing issue ). Could this be the case?

If it doesn’t produce the errors you previously had then it did make a difference.

If it still produces the same error messages then you did something wrong while implementing the suggested template.

Your original template is invalid because it incorrectly references the each entity’s value. The correct way to reference them is explained here (and the suggested template employs the states() function).

If that’s happening then the result of the suggested template will be zero.

You were right :slight_smile:. Now it’s working. I did a copy / paste mistake.

1 Like

not sure if this is a thing to comment older threads… But it really helped me a lot.

In 2022 it seems to work only with another formatting. Plus I added a rounding with 2 decimals.
I use different sensors but the idea is the same.

template:
  - sensor:
      - name: "Battery_current_power"
        unit_of_measurement: 'W'
        device_class: power
        state: "{{((states('sensor.van_modbus_batt_current')| float(0) * states('sensor.van_modbus_batt_volt')| float(0))) | round(2) }}"


1 Like

The example I posted above continues to work properly in version 2022.2 without requiring any changes.

The difference in your example is due to the inclusion of the float filter. There’s a need to wrap the summation in parentheses prior to using the round filter. That has always been necessary (not just in 2022.2). However, why you chose to use two pairs of parentheses is unclear because one pair is sufficient.

1 Like

Pls help to multiply by 5 to this sensor value:
value_template: “{{ (state_attr(‘climate.rozi_thermostat’,‘current_temperature’)) }}”
pls write the correct syntax!
Thx