Math operations with a sensor value

I’d like to define a template sensor which is calculated.
a scaling factor is used to manipulate it, and therefore I need to use a “power” function.

here my sample, which doesn’t work:

template:
  - sensor:
      - name: "LeistungMPP1"
        unit_of_measurement: "W"
        state: >
          {% set mpp1 = states('sensor.power_MPP1') * pow(10, states('sensor.power_sunssf')) | float %}
          {{ mpp1 }}

sensor.power_MPP1 and sensor.power_sunssf are available. sensor.power_sunssf can be -10 to +10

 {% set mpp1 = states('sensor.power_MPP1') * 10 ** states('sensor.power_sunssf') | float %}
1 Like

sorry. it doesn’t work either…

even if I just try to use:
{% set mpp1 = states('sensor.power_MPP1') * 10 ** 1 | float %}

the state is unavailable.

same for:
{% set mpp1 = states('sensor.power_MPP1') * 10 | float %}

it works only that way:
{% set mpp1 = states('sensor.power_MPP1') | float %}

if pow is a correct syntax then this should work. (or use Tom’s syntax but add the | float)
You need to convert each state from string to float before doing the math.

template:
  - sensor:
      - name: "LeistungMPP1"
        unit_of_measurement: "W"
        state: >
          {% set mpp1 = states('sensor.power_MPP1') | float * 10 ** states('sensor.power_sunssf') | float  %}
          {{ mpp1 }}

It’s not. See:

https://jinja.palletsprojects.com/en/3.1.x/templates/#math

But yeah, I mist that first float filter.

1 Like

thanks! that worked!