Error with REST configuration

I’m trying to parse a value returned by https://www.meteoromania.ro/wp-json/meteoapi/v2/starea-vremii as follows: value_template: ‘{{ (value_json[“features”]|selectattr(“properties.nume”,“eq”,“POIANA STAMPEI”)|first)[“properties”][“presiunetext”] | regex_search(r’[\d.]+‘, index=0) }}’

If I leave the regex out of the value_template: ‘{{ (value_json[“features”]|selectattr(“properties.nume”,“eq”,“POIANA STAMPEI”)|first)[“properties”][“presiunetext”] }}’ it works fine so clearly I’m doing something wrong.

Check quoting? Looks like ’ is used inside the template. Like the regex needs double quotes

This works just fine with single quotes.

And your regex above contains single quotes, which breaks it. Change those to doubles.

Also, please format code correctly for the forum with the </> button. I would have posted a corrected version for you, but the unformatted code now contains invalid “smart” quotes and I’m too lazy to fix all of those.

Are you suggesting to replace this:

value_template: '{{ (value_json["features"]|selectattr("properties.nume","eq","POIANA STAMPEI")|first)["properties"]["presiunetext"]| regex_search(r'[\d.]+', index=0) }}'

with this?

value_template: '{{ (value_json["features"]|selectattr("properties.nume","eq","POIANA STAMPEI")|first)["properties"]["presiunetext"]| regex_search(r"[\d.]+", index=0) }}'

This seems to work:

value_template: '{{ (value_json["features"]|selectattr("properties.nume","eq","POIANA STAMPEI")|first)["properties"]["presiunetext"]| regex_search("[\d.]+",0) }}'

Although I haven’t checked your logic in detail — it returns a value, at least.

It returns “True” instead of extracting the number from the initial string.
Without the final regex I get something similar to this: “910.5 mb, in crestere”. I’m trying to further parse this to extract only the number “910.5”.

Ah right. Try this:

value_template: '{{ (value_json["features"]|selectattr("properties.nume","eq","POIANA STAMPEI")|first)["properties"]["presiunetext"]|regex_findall("[\d.]+")|first }}'
2 Likes

awesome, thanks!