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
Is there any way to dynamically adjust the unit_of_measurement?
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
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.