I found a few frontend components in HACS that could do the formatting without having to create separate template sensors. They have varying tradeoffs between power/generality and ease of use. (search for repositories containing “template” in HACS). The template-entity-row customization pointed to in this thread is the closest fit but it would be nice to see something a bit simpler yet almost as flexible.
If you look at the formatting problem as one of “filtering” the value returned when reading an entity value before it’s displayed then there are a pile of “template filter” functions that can handle almost any kind of formatting or calculation. (E.g. the “round()” function, see Templating .) So what if a string comprised of template filter functions could be provided in the card description? A default that applies to any entity could be provided once for the entire card, with entity specific ones when the default needs to be overridden. The filters would behave as if any reference to the value of an ‘<entity>
’ would be replaced with '{{ <entity> | <filter> }}'
.
This would seem to keep simple things pretty simple but provide almost as much power as the solutions I found in HACS.
An example might be (taken from the yaml code editor of an entity card I use now):
type: entities
entities:
- entity: sensor.ro_inlet_pressure
- entity: sensor.ro_system_pressure
- entity: sensor.tank_pressure
- entity: sensor.tank_pressure_stddev
- entity: sensor.ro_inlet_pressure_stddev
title: Water Pressure
becomes (the “filter_template” attribute is new):
type: entities
filter_template: 'round(2)' # Default for all entities
entities:
- entity: sensor.ro_inlet_pressure
- entity: sensor.ro_system_pressure
- entity: sensor.tank_pressure
- entity: sensor.tank_pressure_stddev
filter_template: 'round(4)' # specific to sensor.tank_pressure_stddev
- entity: sensor.ro_inlet_pressure_stddev
filter_template: 'round(4)' # specific to sensor.ro_inlet_pressure_stddev
title: Water Pressure
This seems pretty simple and the meaning is (I hope) obvious.
Using template-entity-row the yaml is more complicated and might look like this (I think, untested):
type: entities
entities:
type: 'custom:template-entity-row'
icon: 'mdi:gauge'
name: sensor.ro_inlet_pressure
state: {{ (states('sensor.ro_inlet_pressure') | round(2) }}
type: 'custom:template-entity-row'
icon: 'mdi:gauge'
name: sensor.ro_system_pressure
state: {{ (states('sensor.ro_system_pressure') | round(2) }}
type: 'custom:template-entity-row'
icon: 'mdi:gauge'
name: sensor.tank_pressure
state: {{ (states('sensor.tank_pressure') | round(2) }}
type: 'custom:template-entity-row'
icon: 'mdi:calculator'
name: sensor.tank_pressure_stddev
state: {{ (states('sensor.tank_pressure_stddev') | round(4) }}
type: 'custom:template-entity-row'
icon: 'mdi:calculator'
name: sensor.ro_inlet_pressure_stddev
state: {{ (states('sensor.ro_inlet_pressure_stddev') | round(4) }}
title: Water Pressure
Although the meaning is still pretty obvious I had to do several things manually that the card normally does automatically (e.g. icon name, row name text and create the entire template to get and format the entity value. The learning curve is much steeper.
At some point I’d like to ramp up enough to be able to implement this rather than just drop an idea I think might work. But, one step at a time, I guess.