Template with condition

Hi there,

I’m using Riemann sum integral to calculate the amount of energy used while charging my car based on the current charge rate given in W.
Unfortunately, I get negative values while not charging, resulting in a false decrease of calculated energy.
So, I’m trying to use a condition template to get a sensor with the current charge rate or “0” if the rate appears negative.
So far, I’ve tried the following which doesn’t work:

template:
  - sensor:
      - name: auto_ladeleistung
        unit_of_measurement: "W"
        state: "{{ '{{ (states('sensor.charging_power')|int * 1000) | round(1) }}' if is_state('sensor.charging_power', '> 0') else '0' }}"
        state_class: measurement
        device_class: power

Any ideas? Thanks in advance!

I’m not a template expert but are that much quotes and brackets right?
Did you try the template in your ‘Developer Tools’ first?

Thanks, leaving out the outer brackets worked. At least now, the error is gone. No “0”, yet but I’ll give it a minute.

Edit: Didn’t work. Now, the state is

'0.0' if is_state('sensor.charging_power', '> 0') else '0'
state: "{{ 0 if (states('sensor.charging_power')|int * 1000) | round(1) < 0 else (states('sensor.charging_power')|int * 1000) | round(1) }}"
1 Like

Thanks a lot! Seems to work, now. Still need to try out if the correct state is given while charging but the “o” works fine.

Working great, now! Thanks again :slight_smile: