Dynamically change sensor units

I have a sensor that ranges from 0 to 5000 grams. The senso values come from ESPHome.

In HA front end it shows as 0 to 5000 grams.

If the value is greater than 1000 grams I need it to show, for example, 2.3 kgs.

For values less than 1000 grams I need it to show, for example, 780 grams.

How can I dynamically change the units?

The sensor is of type INT.

Is this even possible?

You can template the value easy enough (if >1000 divide by 1000), but…

You can’t change a sensors unit of measurement.

Thank you.

I was afraid that might be the answer.

Is it possible to make a card display a different sensor based on a condition?

I could then create two sensors with different units and switch between them.

Yes it is. There’s this:

Create two cards, identical except for the g or kg sensor. Display the correct card depending on one of the sensor states.

type: custom:state-switch
entity: template
template: "{{ 'kg_card' if is_state('sensor.your_mass_sensor_in_g_here')|float >= 1000 else 'g_card' }}"
states:
  kg_card:
    type: markdown
    content: Replace this markdown card with your Kg card
  g_card:
    type: markdown
    content: Replace this markdown card with your g card

Brilliant!

Thank you. I’ll give that a try.