Using Rest sensor to grab HTML value from JSON

Hi all,

I’m trying to create a forecast page and am having trouble with my rest sensor.

I have been able to navigate through the json response and pick out values into seperate sensors to use within HA. All except for one.

The difference with this one (I presume it is the problem) is that it has HTML/potential unescaped characters in it that are causing the rest sensor to trip up.

Here’s the section of JSON I’m working with:

The following:

value_template: "{{ value_json.layout.primary.slots.main.modules[1].forecasts[2].text }}"

Gives me:

<p class=\"u-textGrey fineprint\">Issued: 11:53am Mon, 26 Feb</p>

Removing the “.text” from that value template also gives me a valid sensor state - just a large string with the key:value pair exactly as per the JSON response.

However, the problem I have is with the 1st “text” value in the image above.

I’ve used the exact same value template with the index of 1 to try extract this data but always end up with “unknown”.

Is there anything within that response that would trip up the sensor? Here is the full value from the JSON response (sorry it’s one big long string):

"<p><b>Nearby coastal warnings:</b> Nil for  , Nil for  </p><p><b>Today: </b>Variable 5 knots. Northerly 10 knots developing this afternoon, rising to 15 knots this evening. Sea slight. Cloudy with poor visibility in periods of rain, some heavy.</p><p><b>Tuesday: </b>Northerly 15 knots, turning westerly 10 knots early morning, then southwest 10 knots in the afternoon. Easing to variable 5 knots in the evening. Sea slight. Poor visibility in occasional rain developing in the morning, clearing in the evening.</p><p><b>Wednesday: </b>Southwest 10 knots. Fine.</p><p><b>Thursday: </b>Southerly 10 knots, easing to variable 5 knots early. Fine.</p><p><b>Friday: </b>Variable 5 knots, becoming southwest 15 knots early. Fine.</p>"

Thanks in advance.

Look in Settings → System → Logs.

You will see an error indicating that the state exceeds 255 characters.

Attributes have a much larger limit ~65k so that is where you need to store your value.

Try trop remove the tags maybe?

Wasn’t aware of the character limit on state! Updated to an attribute and voila!

For anyone that comes across this later, this is what worked in my sensors.yaml config file:

- platform: rest
  resource: "https://www.metservice.com/publicData/webdata/marine/recreational/locations/waitemata"
  name: metservice_forecast_data
  scan_interval: 7200 # scan every 2 hours
  value_template: "OK" # note, you need to provide a default value for the state
  json_attributes:
    - text
  json_attributes_path: "$.layout.primary.slots.main.modules[1].forecasts[1]"
1 Like