ashscott
(Ash)
September 6, 2021, 12:32pm
1
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?
tom_l
September 6, 2021, 12:36pm
2
You can template the value easy enough (if >1000 divide by 1000), but…
You can’t change a sensors unit of measurement.
ashscott
(Ash)
September 6, 2021, 2:06pm
3
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.
tom_l
September 6, 2021, 2:48pm
4
Yes it is. There’s this:
Ever wanted a lovelace card that’s only shown for certain users?
Ever wanted to show a certain lovelace card in one spot if the door is open, and another one otherwise?
Ever wanted to be able to swap out a part of your layout when you press a button?
Ever wanted to do that only on one device, and not on all of them at once?
Ever wanted to show different cards depending on the size of your window, or on wheter you’re viewing it on a touch enabled device or not?
[image]
[image]
State-sw…
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
ashscott
(Ash)
September 6, 2021, 7:11pm
5
Brilliant!
Thank you. I’ll give that a try.