Jinja Help Needed (how to extract value from email body)

I have a question. If I have this block of text (from email body that is sent to me everyday).

Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

Forecast: “Today’s forecast is cloudy with a chance of meatball.”

bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

How do I extract the value of the forecast from the block of text using Jinja2 so that I can send it as notification?

Please bear in mind the value is dynamic and will change from day to day.

You could use the following filters:

{{ x|regex_findall_index('Forecast: ".*"')|replace('Forecast: ','')|replace('"','') }}

E.g., I did the following on the Templates page:

{% set x = 'blah\nblah\nForecast: "Today\'s forecast is cloudy with a chance of meatball."\nblah\nblah' %}
ORIGINAL:
---------
{{ x }}

FORECAST:
---------
{{ x|regex_findall_index('Forecast: ".*"')|replace('Forecast: ','')|replace('"','') }}

and got this result:

ORIGINAL:
---------
blah
blah
Forecast: "Today's forecast is cloudy with a chance of meatball."
blah
blah

FORECAST:
---------
Today's forecast is cloudy with a chance of meatball.
2 Likes

thank you so much. it works!

1 Like

@pnbruckner

Sorry. Need your help again. What if the value breaks into few lines? Example…

Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

Forecast: "Today’s forecast is cloudy with a
chance of meatball."

bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

Then I’m stumped. Not sure how to do that in Jinja, if it’s even possible. I think I would try a completely different way to extract the information (like doing it in a python script somehow.)

1 Like