# How to handle arrays and manage null indices (Brewfather)

**URL:** <https://discourse.nodered.org/t/how-to-handle-arrays-and-manage-null-indices-brewfather/39181>\
**Category:** General\
**Created:** [15 January 2021 14:46 UTC](https://discourse.nodered.org/t/how-to-handle-arrays-and-manage-null-indices-brewfather/39181 "2021-01-15T14:46:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cleggton](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@cleggton](https://discourse.nodered.org/u/cleggton)\
**Post date:** [15 January 2021 14:46 UTC](https://discourse.nodered.org/t/how-to-handle-arrays-and-manage-null-indices-brewfather/39181/1 "2021-01-15T14:46:53Z")

</div>

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)

```auto
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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c56f0ae72ba9a78fbb9eab11f8efaa3bba754874.png)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2cacad2b7c5ab4cf52445d7740484931074bc6f.png)

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 March 2021 14:46 UTC](https://discourse.nodered.org/t/how-to-handle-arrays-and-manage-null-indices-brewfather/39181/2 "2021-03-16T14:46:58Z")

</div>

Closed on 16 March 2021 14:46 UTC

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