I Think I need some help from the python gurus on here.
I’ve been trying for a couple of days now to figure out why what I’m trying to do isn’t working and I’m obviously missing something.
I would like to take the output of a custom component that creates an attribute and parse the result to extract certain keys.
I’ve copied the text of the attribute exactly as it is into the template editor and I can dig into it and get out exactly what I want. So that’s all good.
But when I use the same syntax to manipulate the attribute directly the results are not the same and I can’t figure out why.
Here is a screen shot of what is successful. For brevity I’ve paired down the text in the json for easier reading but it is structurally identical to the actual text in the attribute.
However, when I use the same syntax on the real sensor I get:
the reason that I get that error is because when I try to use the index notation for index 0 the only thing it picks up is the ‘[’ symbol at the beginning of the string.
And for reference here is the actual text of the attribute. The author of the component hasn’t done any formatting on it so it’s just a wall of text that I had to format to even know how to try to extract what I wanted. But like I said above, it’s syntactically equal to the formatted one above.
[{"id": "https://api.weather.gov/alerts/NWS-IDP-PROD-3677132-3183041", "type": "Feature", "geometry": null, "properties": {"@id": "https://api.weather.gov/alerts/NWS-IDP-PROD-3677132-3183041", "@type": "wx:Alert", "id": "NWS-IDP-PROD-3677132-3183041", "areaDesc": "Western Kenai Peninsula", "geocode": {"UGC": ["AKZ121"], "SAME": ["002122"]}, "affectedZones": ["https://api.weather.gov/zones/forecast/AKZ121"], "references": [{"@id": "https://api.weather.gov/alerts/NWS-IDP-PROD-3676260-3182579", "identifier": "NWS-IDP-PROD-3676260-3182579", "sender": "[email protected]", "sent": "2019-07-01T03:22:00-08:00"}], "sent": "2019-07-01T15:39:00-08:00", "effective": "2019-07-01T15:39:00-08:00", "onset": "2019-07-01T15:39:00-08:00", "expires": "2019-07-02T10:00:00-08:00", "ends": "2019-07-03T13:00:00-08:00", "status": "Actual", "messageType": "Update", "category": "Met", "severity": "Moderate", "certainty": "Likely", "urgency": "Expected", "event": "Dense Smoke Advisory", "sender": "[email protected]", "senderName": "NWS Anchorage, AK", "headline": "Dense Smoke Advisory issued July 1 at 3:39PM AKDT until July 3 at 1:00PM AKDT by NWS Anchorage, AK", "description": "* LOCATION...Interior Kenai Peninsula, including the Sterling\nHighway corridor from Sterling to Cooper Landing.\n\n* SMOKE...Smoke from the Swan Lake fire will reduce visibilities\nto one quarter mile or less at times. The worst conditions\nwill be late at night through the morning hours.\n\n* TIMING...Through Wednesday, though there is potential for\ndense smoke to persist through the remainder of the week.\n\n* IMPACTS...Travel may be difficult due to low visibility.\nPersons with respiratory problems may have difficulty\nbreathing when outside.", "instruction": "A dense smoke advisory means widespread fires will create smoke,\nlimiting visibilities. If driving, slow down, use your\nheadlights, and leave plenty of distance ahead of you in case a\nsudden stop is needed. For the latest road conditions call\n5 1 1 or visit 511.alaska.gov.", "response": "Execute", "parameters": {"NWSheadline": ["DENSE SMOKE ADVISORY REMAINS IN EFFECT UNTIL 1 PM AKDT WEDNESDAY FOR THE INTERIOR KENAI PENINSULA"], "VTEC": ["/O.CON.PAFC.SM.Y.0003.000000T0000Z-190703T2100Z/"], "PIL": ["AERNPWAER"], "BLOCKCHANNEL": ["CMAS", "EAS", "NWEM"], "eventEndingTime": ["2019-07-03T13:00:00-08:00"]}}}, {"id": "https://api.weather.gov/alerts/NWS-IDP-PROD-3676668", "type": "Feature", "geometry": null, "properties": {"@id": "https://api.weather.gov/alerts/NWS-IDP-PROD-3676668", "@type": "wx:Alert", "id": "NWS-IDP-PROD-3676668", "areaDesc": "Western Prince William Sound; Anchorage; Copper River Basin; Susitna Valley; Matanuska Valley; Western Kenai Peninsula", "geocode": {"UGC": ["AKZ125", "AKZ101", "AKZ141", "AKZ145", "AKZ111", "AKZ121"], "SAME": ["002261", "002122", "002020", "002240", "002170", "002068", "002050"]}, "affectedZones": ["https://api.weather.gov/zones/forecast/AKZ125", "https://api.weather.gov/zones/forecast/AKZ101", "https://api.weather.gov/zones/forecast/AKZ141", "https://api.weather.gov/zones/forecast/AKZ145", "https://api.weather.gov/zones/forecast/AKZ111", "https://api.weather.gov/zones/forecast/AKZ121"], "references": [], "sent": "2019-07-01T11:58:00-08:00", "effective": "2019-07-01T11:58:00-08:00", "onset": "2019-07-01T11:58:00-08:00", "expires": "2019-07-03T12:00:00-08:00", "ends": null, "status": "Actual", "messageType": "Alert", "category": "Met", "severity": "Moderate", "certainty": "Observed", "urgency": "Expected", "event": "Special Weather Statement", "sender": "[email protected]", "senderName": "NWS Anchorage, AK", "headline": "Special Weather Statement issued July 1 at 11:58AM AKDT by NWS Anchorage, AK", "description": "Elevated water conditions will persist for Eagle River. For those\nnear or attempting to cross Eagle River, be prepared for very\nhigh and swift water. Please see weather.gov/aprfc for the latest\nconditions and forecasts.\n\nGenerally across Southcentral, high river conditions are expected\nalong the Matanuska, Skwentna, Klutina, Sixmile, Talkeetna, and Kenai\nrivers. Water levels are expected to continue and/or rise into\nAction Stage and Bankfull conditions through the weekend. No\nsignificant impacts are expected along these rivers.\n\nA Flood Warning has been issued for the Yentna River at Fish\nCreek. Please see the Flood Warning for that area for further\ndetails.", "instruction": "", "response": "Execute", "parameters": {"EAS-ORG": ["WXR"], "PIL": ["AERSPSAER"], "BLOCKCHANNEL": ["CMAS", "EAS", "NWEM"]}}}]
I’m beginning to think the problem is that it’s because the text isn’t properly formatted json. But if that’s the case then why does it work when I manipulate it after I copy it directly to the template editor and set it as a “value”?
I’ve tried to do split(), replace() regex_findall_index() to try to slice and dice it up to get to what I want but there are too many layers of “{”, “[” and “id” to get it worked out.
Any suggestions?