Help with template if + if

Hi!
I want to subtract the smartplug only if the power is greater than the smartplug to subtract.

Right now I have the following, but it never remains:
value_template: ‘{{ states(“sensor.potencia_general”)|float - (states(“sensor.smart_plug_20072959945005251h5448e1e9280472_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072959945005251h5448e1e9280472_power”)|float + states(“sensor.smart_plug_20072912418041251h5448e1e927b30f_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072912418041251h5448e1e927b30f_power”)|float + states(“sensor.smart_plug_20072932379811251h5448e1e927da02_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072932379811251h5448e1e927da02_power”)|float + states(“sensor.smart_plug_20072905738090251h5448e1e927eeb9_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072905738090251h5448e1e927eeb9_power”)|float + states(“sensor.smart_plug_20072909278609251h5448e1e927e0fb_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072909278609251h5448e1e927e0fb_power”)|float + states(“sensor.smart_plug_20072981737370251h5448e1e927b3b1_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072981737370251h5448e1e927b3b1_power”)|float + states(“sensor.smart_plug_20072908268816251h5448e1e927e06e_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072908268816251h5448e1e927e06e_power”)|float + states(“sensor.smart_plug_20072959473523251h5448e1e927f37b_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072959473523251h5448e1e927f37b_power”)|float + states(“sensor.smart_plug_20072946630631251h5448e1e927b6aa_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072946630631251h5448e1e927b6aa_power”)|float + states(“sensor.smart_plug_20072938118893251h5448e1e927e944_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072938118893251h5448e1e927e944_power”)|float + states(“sensor.smart_plug_20072978308250251h5448e1e927ed8f_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072978308250251h5448e1e927ed8f_power”)|float + states(“sensor.smart_plug_20072968613572251h5448e1e927f45a_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.smart_plug_20072968613572251h5448e1e927f45a_power”)|float + states(“sensor.vitro_horno_power”)|float if states(“sensor.potencia_general”)|float > states(“sensor.vitro_horno_power”)|float)|float|round(1) }}’

Can help me please? Many thanks!

That short description fails to explain what that very long chain of if statements is attempting to do.

In addition, the template is malformed because the inline if expression fails to terminate with an else.

Rather than attempt to untangle this knot of a template, perhaps it would be best if you explain what you are trying to do and then we can engineer a simpler way to achieve it.

Based on what I see, you have the following sensors:

sensor.potencia_general
sensor.vitro_horno_power
sensor.smart_plug_20072959945005251h5448e1e9280472_power
sensor.smart_plug_20072912418041251h5448e1e927b30f_power
sensor.smart_plug_20072932379811251h5448e1e927da02_power
sensor.smart_plug_20072905738090251h5448e1e927eeb9_power
sensor.smart_plug_20072909278609251h5448e1e927e0fb_power
sensor.smart_plug_20072981737370251h5448e1e927b3b1_power
sensor.smart_plug_20072908268816251h5448e1e927e06e_power
sensor.smart_plug_20072959473523251h5448e1e927f37b_power
sensor.smart_plug_20072946630631251h5448e1e927b6aa_power
sensor.smart_plug_20072938118893251h5448e1e927e944_power
sensor.smart_plug_20072978308250251h5448e1e927ed8f_power
sensor.smart_plug_20072968613572251h5448e1e927f45a_power

What are you attempting to calculate with those 14 sensors?

I want to calculate the remaining power for example :

General Power: 3500W - Smart1:500 - Smart2:1500 = 1500W Remaining Power

In this case, General Power = “potencia_general”
Note: Smart1 should be 0 if Power General < Smart1

Update:
@123 I have thinked to separate the operations.
Can you fix the Code, like should be?
states(“sensor.potencia_general”)|float - (states(“sensor.smart_plug_20072959945005251h5448e1e9280472_power”)|float if states(“sensor.smart_plug_20072959945005251h5448e1e9280472_power”)|float > states(“sensor.potencia_general”)|float else 0

So if I understood your explanation correctly, you want to subtract the value of all sensors from sensor.potencia_general but only if a sensor’s value is less than sensor.potencia_general? If the sensor’s value is greater than sensor.potencia_general, it is excluded from the calculation. Is that correct?

Let’s say sensor.potencia_general is 3500W and each sensor’s value is between 100W and 500W except one sensor is 3750W. You want to add all sensor values, except the one that is 3750W, and subtract the result from sensor.potencia_general. Is that how you want it to work?

Yes, has understand me :slight_smile:


  value_template: >
    {% set general = states('sensor.potencia_general') | float %}
    {% set sensors = 
      expand( 'sensor.smart_plug_20072959945005251h5448e1e9280472_power',
              'sensor.smart_plug_20072912418041251h5448e1e927b30f_power',
              'sensor.smart_plug_20072932379811251h5448e1e927da02_power',
              'sensor.smart_plug_20072905738090251h5448e1e927eeb9_power',
              'sensor.smart_plug_20072909278609251h5448e1e927e0fb_power',
              'sensor.smart_plug_20072981737370251h5448e1e927b3b1_power',
              'sensor.smart_plug_20072908268816251h5448e1e927e06e_power',
              'sensor.smart_plug_20072959473523251h5448e1e927f37b_power',
              'sensor.smart_plug_20072946630631251h5448e1e927b6aa_power',
              'sensor.smart_plug_20072938118893251h5448e1e927e944_power',
              'sensor.smart_plug_20072978308250251h5448e1e927ed8f_power',
              'sensor.smart_plug_20072968613572251h5448e1e927f45a_power',
              'sensor.vitro_horno_power' )
      | map(attribute='state') | map('float')
      | select('lt', general) | sum %}
    {{ general - sensors }}
1 Like

Thanks, but if sensor.smart_plug_20072968613572251h5448e1e927f45a_power > general, then the value is negative for example sensor.smart_plug_20072959945005251h5448e1e9280472_power: 3750w
general: 3500w {{ general - sensors }} → {{ 3500 - 3750 }} = -250 right?

If a sensor’s value is greater than general, it is excluded from the calculation. Only sensors that are less than general are included.

That’s what this part of the template does, it selects the sensors whose value is less than (lt) the value of the variable general.

select('lt', general)

For example, let’s say the value of general is 3500 and this represents the values of the other sensors:

[ 3600, 1000, 3700, 500, 250, 2000, 100 ]

The select('lt', general) filter will produce this:

[ 1000, 500, 250, 2000, 100 ]

The sum of the five values is 3850. The final result will be:

3500 - 3850 = -350
1 Like

Works!!! many thanks @123 where can learn that code language?

1 Like

This is a good place to start: