Allow template unit on template:

just discovered we can not do

      - unique_id: badge_wind_compass
        state: >
          {{states('sensor.winddirection')}}
        name: >
          {{states('sensor.wind_compass_abbreviation')}}
        unit_of_measurement: >
          {{states('sensor.winddirection')}}°
        picture: >
          {%- set degrees = states('sensor.winddirection')|float(0) %}
          {%- set path = '/local/weather/wind_compass/' %}
          {%- set ext = '.png' %}
          {%- set num = ((degrees // 11.25) * 11.25 // 1)|int %}
          {%- set num  = 0 if num  == 360 else num %}
          {{"{}{}{}".format(path,num,ext)}}

which is very useful for displaying badges, and have some more meanings the Unit placeholder than only

Schermafbeelding 2021-12-04 om 11.16.42

because I use custom-ui I can do:

homeassistant:
  customize:
    sensor.badge_wind_compass:
      templates:
        unit_of_measurement: >
          if (entities['sensor.winddirection'])
          return entities['sensor.winddirection'].state + ' °';
          return 'Initializing';

to see:

Schermafbeelding 2021-12-04 om 11.18.00

allowing templates on the unit could lift the template: integration even further :wink:

Generally speaking, a given sensor represent “something” that is immutable. The value itself is mutable, of course.
As such, it’s unit_of_measurement should be immutable as well.

Specifically, I don’t get what you’re trying to achieve in setting the unit of your sensor to its value, no a “initializing” state, tbh…

I guess you’re trying to “abuse” the property for display purposes, but it’s far-fetched to do a FR for that :wink:

1 Like

yes I understand that, and thats what an exchange of thoughts with Frenck concluded too, see Can not template Unit of template: · Issue #60985 · home-assistant/core · GitHub.

What I am trying to do is have the badge placeholder do something else than only show the unit. As displayed above.

Since that placeholder is designed to display the unit as such, I was forced to fiddle with that unit. if another option were possible without touching the backend unit, that would be embraced ofc.

Separation of concerns: This is a frontend concern, not a Core one.

It’d probably be more efficient to create a custom badge that fits your need (assuming something like card-mod cannot already do that) or ask to Frontend (yeah, I know, pointless) rather than abusing unit_of_measurement…

sure. (though asking Frontend isnt pointless at all, they’re a very responsive troop of devs)

closing this FR. marked the comment with link to the Github as solution, but this wont close?
thanks for you feedback!

If you have a positive experience them implementing a FR, I’d be interested (somewhat) in learning how you achieved that, genuinely.

as said, I have good experience with the team, but what could help is posting in the frontend discussion section rather than here in the Feature request.

1 Like

I just think badges need love in general. There are so many fields that don’t work depending on the domain of the entity that is being displayed and it confuses the shit out of people.

Yes Petro that is true. Using person entities myself, I see the state being displayed in the unit box :wink:

That’s actually what I was looking for, I guess. Thanks.

has it been that long…

well, I do feel I found a reason to get back to this:

see:

      - unique_id: time_until_next_alarm
        name: Time until next alarm
        state: >
          {% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
          {% if timestamp %}
            {% set delta = timestamp-now().replace(second=0).replace(microsecond=0).timestamp() %}
            {% if delta/3600 < 1 %} {{delta|timestamp_custom('%M',False)}}
            {% elif delta/86400 <1 %} {{delta|timestamp_custom('%-H:%M',False)}}
            {% else %} {{(delta//86400)|int}}:{{delta|timestamp_custom('%-H:%M',False)}}
            {% endif %}
          {% else %}
            Not set, relax
          {% endif %}
        icon: >
          {{iif(this.state == 'Not set, relax','mdi:alarm-off','mdi:clock-end')}}
#         unit_of_measurement: >
#           {% if this.state == 'Not set, relax'%} 'Off'
#           {% elif this.state.split(':')|length - 1 == 0 %} M
#           {% elif this.state.split(':')|length - 1 > 1 %} 'D:H:M'
#           {% else %} 'H:M'
#           {% endif %}

        attributes:
          unit: >
            {% set length = this.state.split(':')|length %}
            {% set unit = {1:'M',2:'H:M',3:'D:H:M'} %}
            {% if this.state == 'Not set, relax'%} ''
            {% else %} {{unit.get(length,'')}}
            {% endif %}

the sensor returns various possible options, each requiring another unit. Dont think I need to explain as it will probably be clear, but this is what it does (now using custom-ui):

or

in the off state the unit would show:

I did try to find a current device_class for some generic unit, but only timestamp comes close, and that is not correct, as this is no true timestamp entity…

so, FR still holds…

You should be using duration sensors for those.

sorry Petro, but what do you mean by that?
My point is I would like the unit to change.

current custom-ui template for that:

homeassistant:

  customize:

    sensor.time_until_next_alarm:
      hide_attributes: templates
      templates:
        unit_of_measurement: >
          if (state === 'Not set, relax') return '';
          if (state.split(':').length == 1) return 'M';
          if (state.split(':').length == 3) return 'D:H:M';
          return 'H:M';

which makes it show everywhere, also in more-info

I could probably also use a template-entity-row and add the

            {% set length = this.state.split(':')|length %}
            {% set unit = {1:'M',2:'H:M',3:'D:H:M'} %}
            {% if this.state == 'Not set, relax' %} ''
            {% else %} {{unit.get(length,'')}}
            {% endif %}

after the state.

          - type: custom:template-entity-row
            entity: sensor.time_until_next_alarm
            state: >
              {{states(config.entity)}} {{state_attr(config.entity,'unit')}}

Aware we can have the front-end take care of these to a certain degree by using format: relative if the entity would be a timestamp:

  - type: conditional
    conditions:
      - entity: sensor.next_alarm_datetime
        state_not: unavailable
    row:
      entity: sensor.next_alarm_datetime
      name: Wekker gaat af
  - type: conditional
    conditions:
      - entity: sensor.next_alarm_datetime
        state_not: unavailable
    row:
      entity: sensor.next_alarm_datetime
      format: datetime
      name: Wekker gezet voor

      - unique_id: next_alarm_datetime
        name: Next alarm datetime
        state: >
          {% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
          {{as_datetime(timestamp)|as_local}}
        device_class: timestamp
        availability: >
          {% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
          {{timestamp != 0}}

For this use case, I can not change it to a timestamp, because then the Not Set would cause an error, and making it unavailable in that case would frustrate the Frontend too: I need it to show also in that Not Set case. Not hide it. As I do above for the other scenario.

1 Like