Noobie Node Red Automation Help

I'm a noob to Node Red with Home Assistant and have created an API flow that retrieves aviation weather. I parse it with the json node and am able to see all of the needed elements of the API call. What I'm struggling with is that HA retains entity values until it's either restarted or the entity updates. I'm trying to create a logic flow that will only display (visibility on the HA Dashboard) the cloud levels if there is valid data. The API that I use will provide me with up to three cloud layers (cloud type and altitude). There are not always three cloud layers when the API fires, so if those layers are not present, it generates an error (TypeError). I want to create a binary sensor that will show True when there is a valid numeric value and False when it's TypeError.

Any takers? If so, please be specific on the Node Red instructions. Thanks in advance!

I don't use HA, but this might give you some ideas ?

[{"id":"6428c4974a4ce320","type":"change","z":"90084585.8983b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$exists(clouds[1]) and (clouds[1] != null)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1280,"y":3270,"wires":[["403d953a8b084813"]]},{"id":"f2e47dd10b584294","type":"inject","z":"90084585.8983b8","name":"3 layers","props":[{"p":"clouds","v":"[\"one\",\"two\",\"three\"]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1070,"y":3250,"wires":[["6428c4974a4ce320"]]},{"id":"403d953a8b084813","type":"debug","z":"90084585.8983b8","name":"debug 59","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1450,"y":3270,"wires":[]},{"id":"63e8e89a13711084","type":"inject","z":"90084585.8983b8","name":"1 layer","props":[{"p":"clouds","v":"[\"one\"]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1070,"y":3300,"wires":[["6428c4974a4ce320"]]}]

I don't think that's the way I want to go. I've sat here and analyzed what I really want to do, which is to have a binary sensor off, say the cloud3 alt layer node and be = true for a numeric value (only) and discard anything (= false) that is non-numeric. When the flow looks at the json element in the query, an array is created for each of the cloud layers (if they exist). There may be a cloud layer at layers 1, but not layers 2 or 3. So, the array rows vary by cloud layer reports. I want to display an element in home assistant (really not an issue here) only when there is a current row in that json array. NodeRed is coming back and saying :TypeError..." instead of the entity wiping clean after that change so I don't display an entity unnecessarily?

The code snippet does that, true if the array has a specific element, false if not.
Though I don't really understand how you want to use this "binary" sensor ?

The issue is simply that the node expects a value in a given property and it doesn't exist.
Can you point me to the documentation for that particular node, as I don't know if it has any options which may help here.

Can you show me what you have set in the nodes configuration, for those shown typeError please.

If you send an empty value to it e.g. "" does that element get removed from the HA screen, or is there still a placeholder for the null value ?

I THINK what you actually need to do is return the value if it exists, if not then return an empty string for the "entity wiping clean" requirement

If you answer the above I can show you how to do that with a change node before the HA nodes.

insert a switch node in each flow before the ha nodes that throw errors and only pass on if not null.
isnull is a selectable test in the switch node, and the HA node should contain the JSON expression that points at the data element that is null resulting in the typedef issue. (I guess?)

Add an 'otherwise' option to the switch node and use its output to pass on a successfully found data element?

Sorry - my noob is showing. I solved the problem very simply. I needed the visibility section of the HA chip (mushroom chip for those of you using HA) to be visible when a valid number is present and be hidden when there is text (error or otherwise). Since cloud altitude layers are reported as numeric value, I set my visibility check for a numeric value between 0 - 60000. If there are no reported cloud layers (layers 1, 2 or 3) then I would see text in the payload (error message) and hence the condition would be false and chip would be hidden. This guarantees that I see valid data as of the API call, as opposed to using time. The latter won't work since weather updates are normally issued on the hour (or thereabouts) and augmented more frequently as the weather deteriorates and flight rules change. A static time interval makes this break, so the approach that I used seems to work and tested well with setting values in the Developers Tools area.

Appreciate the feedback and will still look into that approach as I'm always looking to learn.

Cheers!

1 Like