How to handle arrays and manage null indices (Brewfather)

Hi there,

First time on the forum, and a newbie on node-red, though having some success by trawling the forum.

I am trying to get some data from an API (brewfather) and pass that to sensors on home assistant. The API gives a response like this.

The array changes size depending on the number of batches in that state in Brewfather (API)

array[2]
0: object
_id: "bklHukGNhaFUOzWgJ24bDWx9gcz3S4"
name: "Batch"
batchNo: 2
status: "Completed"
brewer: null
brewDate: 1607172885991
recipe: object
name: "Vienna Lager - 17l"
fermentationStartDate: 1607172885991
1: object
_id: "15fuw3HnEcKoXOlcGHjn8wtCEaalLW"
name: "Batch"
batchNo: 1
status: "Completed"
brewer: null
brewDate: 1606003200000
recipe: object
name: "DH - American Pale 17l"
fermentationStartDate: 1606003200000

I am trying to pull the name from the batch and pass it to home assistant, using the HA addon.

Each HA node is setup to select msg.payload[0...2].recipe.name, depending on the sensor

Nice and easy right? The only problem is when one of the indexes of the array is empty (i.e. there is no batch in that slot) I get an error.

I am really struggling with the design to handle that error/scenario.

I tried this also

Which works without errors, but clearly if there is no element in the message, it doesn't get split and routed, so my HA sensor stays populated even though what I want it to do is say "empty" or something.

I'm guessing that the change node is what i need here, but am at a loss on how to implement it.

Any thoughts would be helpful

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.