Result two decimal places

How can I change this script to display only two decimal places? the Round option does not work, it displays the result 0.9999999191919191

    sensors:
       encja_wynikowa_cop:
         friendly_name_template: "COP"
         unique_id: COP
         availability_template: >-
          {%- if not is_state("sensor.encja_wynikowa_4", "unavailable") %}
            true
          {%- endif %}
         value_template: "{{ (states('sensor.encja_wynikowa_moc_wymmienika') | float) / states('sensor.encja_wynikowa_4') | round(2) }}"
         unit_of_measurement: "kW"```

It is rounding your second number before the division. Need some paranthesis in there.

{{ ((states('sensor.encja_wynikowa_moc_wymmienika') | float) / states('sensor.encja_wynikowa_4')) | round(2) }}
1 Like

Thank you for your help. I’m still learning and I don’t understand everything

Not a problem.

Wanna know how I saw it so fast? I have done it wrong a LOT :slight_smile:

1 Like

thank you for any help.
I am grateful

Would you like to make sure that this script does not display a value lower than 1?

And you are doing some things still wrong/not the best way. :wink:

float, round, … should have a default value.

Meaning? because I don’t want it to display 10 decimal places

It is not related to that. It is related to the cases, where your source don’t have a number (error cases).

See

I’ve read it and there’s not much in it for my script. Please tell me what I’m doing wrong

The point arganto is making is that the syntax should really be correct so that if something does happen, you won’t get an error.

In this case:

states('sensor.encja_wynikowa_moc_wymmienika') | float(0)

The syntax for float is float(default). So, if there is an invalid numeric state (such as unavailable) from the sensor, you won’t get an error. But, you will get invalid data.

What is your preference is the question I guess.

As far as a value lower than one, that will take an if statement.

{{ 1 if ((states('sensor.encja_wynikowa_moc_wymmienika') | float) / states('sensor.encja_wynikowa_4')) | round(2) < 1 else ((states('sensor.encja_wynikowa_moc_wymmienika') | float) / states('sensor.encja_wynikowa_4')) | round(2) }}

Or, something like that. Understand that I am “working” so I can’t validate that as I don’t have access to HA on my work computer. So, I could not test it.

But, is that really what you want? If less than 1, output 1, else output calculation? I haven’t tried to decipher what you are doing. Just looking at it syntactically.

1 Like

this is a script measuring COP for a Heat Pump converted from an air conditioner. And it works properly. But the annoying thing is that when the Heat Pump is not working, it keeps counting and comes up with values like 0.988467823485477 kW and 160 kW, which may confirm the correctness of the calculations, but they are unnecessary. The question was only created for cosmetic purposes.
Thank you for your help