andrey-git / home-assistant-custom-ui

Custom UI elements for https://home-assistant.io
https://home-assistant.io
MIT License
526 stars 97 forks source link

how to format the customize for sensor.moon _stateDisplay #159

Closed Mariusthvdb closed 5 years ago

Mariusthvdb commented 5 years ago

Checklist

Browser + Version: Safari on Mac

CustomUI version: 20180831 Home Assistant release (hass --version): 0.81.6 Problem-relevant configuration.yaml entries:

    sensor.moon:
     templates:
       _stateDisplay: >
         return state|title|replace('_',' ');

please see https://community.home-assistant.io/t/strange-different-template-behavior-between-hassio-instances/77444

I am trying to change the regular display of these 2 sensors in a capitalized way (only cap the first character of the state, and remove the _ and replace that with a space.

Somehow my 2 systems with the exact same sensor, show differently. I know now that my above customization isn't correct, as explained i the community thread by @petro and shown in the inspector, but I can't get the correct format for it.

regular template would be:

{{states('sensor.moon').capitalize().replace('_',' ')}} and {{ states('sensor.season').capitalize() }}

please let me know how to adapt that for custom-ui.

Problem-relevant Home Assistant log entries:

adc112eeec674a9607b11fbf4b5b80cc0b35ac89

Any errors from browser Javascript console:

Mariusthvdb commented 5 years ago

answering myself and for reference:

    sensor.moon:
      templates:
        _stateDisplay: >
          function capitalizeFirstLetter(string) {return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();}
          string = state.replace('_',' ');
          return capitalizeFirstLetter(string);