Template variable warning: list object has no element 1 when rendering

Hi!

I have a sensor named “sensor.day_time_openweather_custom” which result a combined string of two sensors (ex: day_clear, night_rain, …)

I use a markdown card, with the following details:

type: markdown
content: '{{ states.sensor.day_time_openweather_custom.state.split("_")[1] | title }}'

Which will split the string and show me only what is after “_” character (ex: (ex: Clear, Rain, …)

Now, all good, it is working, but the thing which is annoying me is that the log of home assistant is showing me too many lines of this:

2021-11-25 13:29:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: list object has no element 1 when rendering '{{ states.sensor.day_time_openweather_custom.state.split("_")[1] | title }}'

Any idea how to solve this?

Thank you in advance!

check to see if there’s 2 items in the list before extracting the second ([1]) item. On startup, you probably don’t have this second item.

Well… I thing it should be always with values in both items…

First item is written by me, manually, in the sensor, and the second item is extracted from weather state, which should be everytime.

That warning is shown only after restarting home assistant, which means that the value of the second item, is loading slower than home assistant, I don’t understand :smiley:

If is so, I will make a template to verify if there is any value first, then show it… I hope it will solve it. Thank you!

Not on startup. On startup everything is ‘unavailable’, which is 1 item.

Exactly… This is what I thought after I read your first message. Thank you. I will try an “if” template. :slight_smile: