Capitalize() function missing?

I’m trying to capitalize some strings that I get via a scrap sensor.
Looking at the Jinja2 ref guide I see the capitalize() function but that’s not recognised in HA.
Anybody aware of a workaround?

Would titlecase do the trick?

So for a plant alert Kodi notification I use:

  kodi_notification_lemon:
    alias: Lemon Problems on Kodi
    sequence:
      - service: notify.digi_amp_notify
        data:
          title: "Lemon is Concerned about"
          message: "{{ states.plant.lemon.attributes.problem | title }}"

http://jinja.pocoo.org/docs/2.10/templates/#title

2 Likes

Capitalize is just working fine for me

{{ 'just a test' | title }}

Just A Test

{{ 'just a test' | capitalize }}

Just a test

2 Likes

FYI the capitalize() also works fine too

{{ "just a test".capitalize() }}

Just a test

1 Like

Thanks all, for some reason I got in my head that it would be in the format capitalize(“string”) Still getting used to jinja which is “backwards” compared to what I’m used to

This is an object oriented language. Most string manipulation functions are built into the string class. All strings are a string object, so the function is called from the object. Like lowercase:

mystring.lower() or "foo".lower()

If you take a few python tutorials, you’ll pick some of this odd undocumented behavior up.

2 Likes

I have two sentences like this for example:

"test some sentence. Second sentence."

I would like only first letter of first word to be uppercase without touching the second sentence.

Is it possible? Title or capitalize are not working for my case. Capitalize is making my second sentance lowercase and title is making every single word uppercase.

Well it depends on your string. If it’s always going to be made of 2 sentences, then you could do this:
{{somestring.split(".")[0] | capitalize}}.{{somestring.split(".")[1]}}.

If it’s always the first letter of your string, regardless of how many sentences, then you could try this:
{{ somestring[0] | capitalize }}{{ somestring[1:] }}

1 Like