Can I combine two or more if statements to calculate a value?

Hello
I use a sensor to calculate my power consumption. to do this, I add all the devices together to get one value. This also works. Now it can be that a sensor delivers no value. Which is sporadically the case with the Shelly sockets. For this I would like to build an if statement around each sensor that checks the value first and if it does not exist returns a status of 0.0 or the value. For this I have thought of the following statement:

            {% if (is_number(states('sensor.netzwerkfestplatte_power'))) and (states('sensor.netzwerkfestplatte_power') | float(0) > 0) %} 
              {{ (states('sensor.netzwerkfestplatte_power') | float(0) ) | round (3) }} 
            {% else %} 
              {{(states ('0.0') | float(0)) | round (3) }} 
            {% endif %} +

            {% if (is_number(states('sensor.kaffemaschine_power'))) and (states('sensor.kaffemaschine_power') | float(0) > 0) %} 
              {{ (states('sensor.kaffemaschine_power') | float(0) ) | round (3) }} 
            {% else %} 
              {{(states ('0.0') | float(0)) | round (3) }} 
            {% endif %} 

But I always get an error

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘%’) for dictionary value @ data[‘sensors’][‘power_total_consumption’][‘value_template’]. Got "{{\n {% if (is_number(states(‘sensor.netzwerkfestplatte_power’))) and (states(‘sensor.netzwerkfestplatte_power’) | float(0) > 0) %} \n {{ (states(‘sensor.netzwerkfestplatte_power’) | float(0) ) | round (3) }} \n {% else %} \n {{(states (‘0.0’) | float(0)) | round (3) }} \n {% endif %} | float(0) + \n {% if (is_number(states(‘sensor.kaffemaschine_power’))) and (states(‘sensor.kaffemaschine_power’) | float(0) > 0) %} \n {{ (states(‘sensor.kaffemaschine_power’) | float(0) ) | round… (See ?, line ?).

Is this combination of if statements not possible?

BR

Bastian

states('0.0') is non-sense… the states() function expects an entity ID as an argument.

And, no you cannot perform mathematical operations outside the template.

That is what the 0 default for the float filter is doing. states('sensor.netzwerkfestplatte_power') | float(0) will return the value of the sensor’s state as a float, returning 0.0 when the state is non-numeric. However, you don’t need to use float if you’re going to use round.

So to sum the two sensor values you can use:

{{ states('sensor.netzwerkfestplatte_power') | round(3, default=0) + 
states('sensor.kaffemaschine_power') | round(3, default=0) }} 

or

{{ expand('sensor.netzwerkfestplatte_power', 'sensor.kaffemaschine_power') 
| map(attribute='state') | map('round', 3, default=0) | sum  }}

Thanks that’s solves my issue.