Show unit of measurement kW or W depending on the scale

Hi,
I’d like to see the unit of measurement W if the power is less than 1000W and kW if the power is greater or equal to 1000W.
I’ve implemented the following:

      power_lavastoviglie:
        device_class: power
        unit_of_measurement: W
        value_template: |
          {% set value0 = states("sensor.hs110_lavastoviglie_watts")|float %}
          {% if value0 > 1000 %}
            {{ (value0 / 1000)|round(2) }} k
          {% else %}
            {{ value0|round(2) }}
          {% endif %}

but when showing the “k” is not close to W
Capture
Is there any way to dynamically adjust the unit_of_measurement?

thanks

You are not setting a different unit of measurement, you are changing the sensor value. And a value of 3.22k does not make sense. It is not s number.

I think you may need to template the unit. If you can, I am not sure.

yes I changed the value and would like to change the unit as well

I do not know how to “template” the unit

@123 might be able to help with this.

Unit_of_measurement template although I don’t know if that solution is current. EDIT in fact I am not sure it was even a solution then :frowning:

There is also a useful discussion here Unit_of_measurement_template

It points out that you shouldn’t have a sensor randomly changing units, it will play havoc with history, graphing (and automations I suspect).

And that the sensor should keep units constant, and that this is a display issue.

As far as I know, no.

It won’t because there’s always a space between the value and the unit. In this case, you’ve made k part of the value.

you are right thea will have caos in database but I’m using the sensor only to display the value in a way not to exceed the border of the card
btw at the time beeing I’m happy to have the “k” as part of the value of the sensor untill I find better solution

So why not simply use kW for all values?

because of … my wife :upside_down_face:

Undoubtedly she is a reasonable person and will understand the situation if you explain it. By appending the letter k to the value it ceases to be numeric and you lose the ability to graph it. By having some values in watts and others in kilowatts you also lose the ability to perform simple arithmetic. For example, you won’t be able to use the History Stats sensor to display total energy.

If none of these drawbacks is important to her then at least she is aware of them from the start. If in the future there is a desire to graph the data, there will be no surprises to anyone that all history will be lost before the graph can be implemented.

This is why it is, as I said, a display issue. I am sure amongst all the lovelace cards available (official and unofficial) there must be some way of templating the display of a sensor variable.

I’m 99.99% positive that the ‘official’ cards don’t support templating.

That leaves the ‘unofficial’ cards as a possibility. I believe the auto-entities card supports templating.