Extracting certain parts of a string

Copy-paste the following template into the Template Editor and let me know if it reports what you want.

{% for x in state_attr('sensor.weather_alert', 'entries') %}
 {{ x[7:] }}
{% endfor %}

Where do you want to display the information?

1 Like

Thanks so much for the reply! I would like to display this in a card on the dashboard. When I put that into the template editor, it gives me TypeError: unhashable type: 'slice'

I can help you fix the template but none of the standard cards support the use of a template other than the Markdown card.

What’s the result of this:

{% for x in state_attr('sensor.weather_alert', 'entries') %}
 {{ x.title }}
{% endfor %}
1 Like

{{ state_attr(‘sensor.weather_alert’,‘entries’)[0].split(’: ')[1] }} might work

Change the 0 to 1 for the next line.

1 Like

Your suggestion to use split assumes each item in the list is a string. The first template I posted made the same assumption (and attempted to slice off the first 7 characters of the string). However it resulted in an error, so it’s not a string. I’m led to believe each list item is a dictionary (where the key is title and its value is a string).

1 Like

I get this…
New-1
Awesome! So theres no way I can get that on my dashboard??
Right now it looks like this:
New-2

I tried that and I got this:

UndefinedError: 'dict object' has no attribute 'split'

If it is a dictionary, then, I think, this should work:
{{ state_attr(‘sensor.weather_alert’,‘entries’)[0].title }}

1 Like

You can use it in a Markdown card.

1 Like

Yep, that worked great! But what if there are 2 or 3 or x number of alerts?

The template I posted uses a for-loop to display all of the available warnings. If there are no warnings, nothing will be displayed (and the template won’t produce an error message).

1 Like

Yes, yes! Amazing…

New-1

My existing card on top. New markdown card below. Awesome that it will display x number of alerts too!!
Thank you VERY much!

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title signaling to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like

Done!
Thanks again. This is exactly what I was looking for…

New-1

I have another feed, very similar, where I only want the first 3 title entries. How would the code above change?

Edit: Answered my own question:

{% for x in state_attr('sensor.cbc_news', 'entries') %}
{% if loop.index <= 3 %}{{x.title}}{% endif %}
{% endfor %}

I do have one more question for you. You can see from my post from 3 days ago how it looks with 3 alerts. Do you know why it looks like this with only 2 alerts…

New-1

In order to answer your question I would need to see the sensor’s screenshot (like the one in your first post) displaying the two alerts in its entries attribute.

1 Like

Here is the sensor setup in config…

  - platform: feedparser
    feed_url: https://weather.gc.ca/rss/warning/on-151_e.xml
    name: Weather Alert
    scan_interval:
      hours: 1
    inclusions:
      - title
    exclusions:
      - language

Is that what you needed? Thanks again!

I needed to see a screenshot of the sensor’s details, like the image in your first post. However, it’s probably too late now because there are no current advisories for your area.

Ah, ok thank you. I will post when it comes up again and include the right picture.