Unit_of_measurement template

I want to change the unit of measurement to KW when more than 1000 W are being used on my sensor.

  value_template: "{% if states('sensor.energy_usage')|float > 1000 %}{{ (states('sensor.energy_usage')|float / 1000) | round(1) }}{% else %}{{ states('sensor.energy_usage' )}}{% endif %}"
  unit_of_measurement: "{% if states('sensor.energy_usage')|float > 1000 %}KW{% else %}W{% endif %}"
  friendly_name: Energy Usage

Is there any solution to this?

Bump.

I have the same challenge.
I measure ping response as suggested by AnderssonPeter in Ping sensor

When the host don’t respond, the sensor reports “Offline”, and then I want the unit of measurement to be empty.

I tried this, but it didn’t work as intended…

unit_of_measurement: '{{% if is_state("binary_sensor.bahnhof", "on") %}ms{% else %}{% endif %}}'

unit%20of%20mesurement

Any tips?

The issue is that not all fields of a sensor can take a template that is evaluated. If the field isn’t setup to be template capable then you can only use a static value.

I has a similar issue when I wanted to change the friendly_name of a camera to text that represented the time the image was taken since the image would change every five minutes.

The solution I came up with in the end was to install the customizer add-on:

I then had a automation that triggered whenever the state changed on the sensor and this called the customizer service to update the friendly_name field.
You could use the same approach to change the unit_of_measurement field when the state changed depending on it’s value.

Sweet work around. I’ll try that. Thanks!

try:

 unit_of_measurement: '{{ "ms" if is_state("binary_sensor.bahnhof", "on") else "" }}'

did this work?
i ask because im trying this:

homeassistant:
  customize:
    sensor.ha_update_available:
      state_card_mode: badges
      unit_of_measurement: '{{states.sensor.ha_latest_version.state}}'
      templates:
        theme: >
          if (state = 'Yes') return 'green_badge'; else return 'blue_badge';

template tool shows what i would like:

But the frontend won’t…

53

I don’t think unit_of_measurement accepts value templates. When I posted that I was unsure. After this past month, I’m convinced it’s not possible. If you want the version displayed as a sensor, you may want to suppress the ha_update_available sensor and make a new one that displays the version number as the status. Then just don’t use a unit_of_measure.

Hi there I am a newbie to Home Assistant, using HA for a final year school project! Following AnderssonPeter’s ping sensor post, may I know from the configuration he posted is it all in one yaml file or in their own separate yaml files ie. binary_sensory.yaml, sensor.yaml? Would really appreciate the help! Thanks in advance :smile:

I see now i missed this one for a long time, and owe you an answer yet:

homeassistant:
  customize:
    sensor.ha_update_available:
      state_card_mode: badges
      templates:
        unit_of_measurement: ${entities['sensor.ha_latest_version'].state}
        theme: >
          if (state === 'Yes') return 'green_badge';
          return 'grey_badge';

does it. :wink:

1 Like

currently the UoM setting seems to break HA (0.85.0). I see constant errors (at least when using lovelace) and the frontend is unresponsive. Had to comment out the UoM line for now.

in the frontend the message:

Service system_log/write called

keeps poping up and in the logs I see the error:

:0:0 Script error.
homeassistant frontend.js.latest.201901090 script error

repeating several times per second

ahh, too bad really.

Haven’t updated to 0.84.6 because custom-ui author @andrey indicated _stateDisplay will lose customization possibility, this adds another downer to upgrading.
Im quite amazed how updating the HA versions which clearly focusses development
of Lovelace, deteriorates the regular HA frontend incrementally…
And Lovelace missed a lot in customization options we had in regular HA. most prominent of these in my setup are customizations regarding per entity theming and coloring in Lovelace, extra badges, show_last_changed, etc etc.

If regular HA now starts to break down on all of these nice frontend improvement custom-ui made possible, that is a true disappointment.

That all being said, couldn’t it be something else is causing this issue in your setup?

I had quite some extra changes but none of them involved customizing theming etc. I just finally enabled the ‘update available’ badge in 0.84.6 just before moving to 0.85.0. I already reverted back to 0.84.6 for now and am testing if it works there properly.

other errors I noticed in 0.85.0 is weird page refreshes (at least in safari) and the complete theme setting to some white form where buttons went missing or even the complete page going to white.
Also my xiaomi gateways were not working properly, found some unable to connect errors in the logs but that might be related to a recent firmware update of the gateways, although it seems to work fine in 0.84.6.
I will enable things step by step to see what works and what not.

At least for now (for your sake) lovelace is not the default but that wil change in 0.86.0. I guess the groups.yaml will than still be possible to use with an extra configuration setting.

Extra customization features will come later on for lovelace, I have no doubt about it, but that will take some time.