# Function Node coding?

**URL:** <https://discourse.nodered.org/t/function-node-coding/75126>\
**Category:** General\
**Tags:** function-node, javascript\
**Created:** [13 February 2023 18:21 UTC](https://discourse.nodered.org/t/function-node-coding/75126 "2023-02-13T18:21:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [13 February 2023 18:21 UTC](https://discourse.nodered.org/t/function-node-coding/75126/1 "2023-02-13T18:21:32Z")

</div>

I'm sorry but I can't figure out how to code my function node.

This is what I need to run.

slope = 184.0 - 0.0 / 3730.0 - 746.0  
offset = 0.0 - (slope \* 746.0)

(msg.payload \* slope) + offset

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 February 2023 18:47 UTC](https://discourse.nodered.org/t/function-node-coding/75126/2 "2023-02-13T18:47:14Z")

</div>

Try...

```auto
const slope = 184.0 - 0.0 / 3730.0 - 746.0
const offset = 0.0 - (slope * 746.0)

msg.payload = (msg.payload * slope) + offset

return msg

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2023 19:02 UTC](https://discourse.nodered.org/t/function-node-coding/75126/3 "2023-02-13T19:02:17Z")

</div>

> [@Steve-Mcl](#):
>
> `const slope = 184.0 - 0.0 / 3730.0 - 746.0`

I think the OP probably meant  
`const slope = (184.0 - 0.0) /( 3730.0 - 746.0)`  
Otherwise it does not make sense.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2023 19:02 UTC](https://discourse.nodered.org/t/function-node-coding/75126/4 "2023-02-13T19:02:38Z")

</div>

You can do that with a Range node.

---

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [13 February 2023 19:03 UTC](https://discourse.nodered.org/t/function-node-coding/75126/5 "2023-02-13T19:03:41Z")

</div>

I'm still getting errors.

This is exactly what I have in my function node:

```auto
msg.payload =
{
const slope = 184.0 - 0.0 / 3730.0 - 746.0
const offset = 0.0 - (slope * 746.0)
     "Metrics": [
          { "name": "Sensor1",
               "Value": (msg.payload * slope) + offset
          },
          ]
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [13 February 2023 19:09 UTC](https://discourse.nodered.org/t/function-node-coding/75126/6 "2023-02-13T19:09:58Z")

</div>

Have you tried this in your function node??

```auto
const slope = (184.0 - 0.0) / (3730.0 - 746.0);
const offset = 0.0 - (slope * 746.0);

msg.payload =
{
     "Metrics": [
          { "name": "Sensor1",
               "Value": (msg.payload * slope) + offset
          },
          ]
}
return msg;

```

When I inject 1 as msg.payload I get this result...  
 ![result_function_node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40dcb77b66c9cda6617d2003ddec05959a16da7b.png)

---

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [13 February 2023 19:13 UTC](https://discourse.nodered.org/t/function-node-coding/75126/7 "2023-02-13T19:13:58Z")

</div>

Yes, thank you, I really don't know how I would get along without this forum, and everyones help.

---

<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:** [27 February 2023 19:14 UTC](https://discourse.nodered.org/t/function-node-coding/75126/8 "2023-02-27T19:14:00Z")

</div>

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