# OEE with Node-Red

**URL:** <https://discourse.nodered.org/t/oee-with-node-red/56067>\
**Category:** Industrial\
**Created:** [4 January 2022 02:14 UTC](https://discourse.nodered.org/t/oee-with-node-red/56067 "2022-01-04T02:14:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmccc](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jmccc](https://discourse.nodered.org/u/jmccc)\
**Post date:** [4 January 2022 02:14 UTC](https://discourse.nodered.org/t/oee-with-node-red/56067/1 "2022-01-04T02:14:59Z")

</div>

Hi, I am trying to make OEE calculations with the help of node red. In this function i'm trying to calculate the availability parameter (A).  
I am trying to write a function to give me the availability value, but no matter what I do, the output is NaN. It is simple math but Im new using node-red and Javascript.

the code:

 ![node-redforum1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8cb20425577ba2c889e7709c1dff5d0f8bb95b5.jpeg)

```auto
var hinicioproducao = flow.get("HoraInicio");
var hfimproducao = flow.get("HoraProducao");
var setup = flow.get("HoraSetup");
var downtimeloss = flow.get("Avarias");
msg.payload = [(hfimproducao - hinicioproducao)-(downtimeloss + setup)]/[(hfimproducao - hinicioproducao)]*100;
effectiveproductiontime = msg.payload;
return msg;

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [4 January 2022 03:29 UTC](https://discourse.nodered.org/t/oee-with-node-red/56067/2 "2022-01-04T03:29:48Z")

</div>

> [@jmccc](#):
>
> ```auto
> msg.payload = [(hfimproducao - hinicioproducao)-(downtimeloss + setup)]/[(hfimproducao - hinicioproducao)]*100;
> 
> ```

Square brackets in Javascript are used to build arrays, not for grouping expressions. Try changing the first-pair of brackets to parentheses, and just eliminate the extra second-pair of brackets:

`msg.payload = ((hfimproducao - hinicioproducao)-(downtimeloss + setup))/(hfimproducao - hinicioproducao)*100;`

But you must ensure that ALL of your variables are defined, or the answer will be NaN (not a number). A common way to do that in Javascript is to use the 'or' operator `||` to set a default if there is a missing flow value, like so:

` var setup = flow.get("HoraSetup") || 0;`

> [@jmccc](#):
>
> `effectiveproductiontime = msg.payload;`

This line effectively does nothing, and should be eliminated -- unless you plan on doing something with that value? At a minimum, it should be declared as a `var`, not just left as a bare name.

---

<div class="post-metadata">

**Author:** ![jmccc](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jmccc](https://discourse.nodered.org/u/jmccc)\
**Post date:** [5 January 2022 12:00 UTC](https://discourse.nodered.org/t/oee-with-node-red/56067/3 "2022-01-05T12:00:57Z")

</div>

Thanks for the answer. The calculation formula was incorrect which did not allow me to define all the variables well. 👍

---

<div class="post-metadata">

**Author:** ![jmccc](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jmccc](https://discourse.nodered.org/u/jmccc)\
**Post date:** [5 January 2022 12:01 UTC](https://discourse.nodered.org/t/oee-with-node-red/56067/4 "2022-01-05T12:01:50Z")

</div>

> [@shrickus](#):
>
> Square brackets in Javascript are used to build arrays

Nice tip. Didn't know about that.

---

<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:** [6 March 2022 12:01 UTC](https://discourse.nodered.org/t/oee-with-node-red/56067/5 "2022-03-06T12:01:56Z")

</div>

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