# How to pass variables values to Function node?

**URL:** https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028
**Category:** General
**Created:** [15 February 2019 21:21 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028 "2019-02-15T21:21:01Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Traste](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/traste/32/5910_2.png) [@Traste](https://discourse.nodered.org/u/Traste)
#### Post date: [15 February 2019 21:21 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/1 "2019-02-15T21:21:01Z")

</div>

Hi,

I need to use this pass these values in a Function node:

```auto
msg.payload = '[{ "time”:”TIMESTAMP”,”entity": "entity1", "type": "living_room", "state": “STATE_VALUE”, "value": "2", "location":"bedroom" }]';
return msg;

```

All values in capital letters have to be replaced with the actual values from the previous node. I tried using this, but it won't work:

```auto
msg.payload = '[{ "time”:”TIMESTAMP”,”entity": "entity1", "type": "living_room", "state": “{{{click}}}”, "value": "2", "location":"bedroom" }]';
return msg;

```

Previous node is setup this way:  
 ![org%202019-02-15%2022-19-09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9d6af2ef906ff361e39df8cdb991fa9ebfd7aae6.jpeg)

Any ideas? Also, any way to get the timestamp on that?

Thanks!

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 February 2019 21:28 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/2 "2019-02-15T21:28:26Z")

</div>

Hi @Traste

You say all the properties in capital letters need to be replaced, I can see `TIMEZONE` `STATE_VALUE` and `VALUE` - what are the properties of the message you are passing the function that contain those values? `msg.payload.TIMEZONE`? or something else?

---

<div class="post-metadata">

### Author: ![Traste](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/traste/32/5910_2.png) [@Traste](https://discourse.nodered.org/u/Traste)
#### Post date: [15 February 2019 21:38 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/3 "2019-02-15T21:38:20Z")

</div>

Thanks for replying. So sorry, but I just noticed I did not paste it the data properly. I wanted to write "timestamp" instead of timezone, and only 1 variable more. I edited my message 😅

So, I need:

- TIMESTAMP: a way to add the current time with the timestamp format
- STATE\_VALUE: I get this value from the previous node, from msg.click, as per my image

Thanks!

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 February 2019 21:44 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/4 "2019-02-15T21:44:00Z")

</div>

As you want it to be a JSON String, the easiest way to create the string is to create the javascript object and then convert it to JSON.

```auto
var myObject = [
    {
        "time": Date.now(),
        "entity": "entity1",
        "type": "living_room",
        "state": msg.click
        "value": "2",
        "location": "bedroom"
    }
];
msg.payload JSON.stringify(myObject);
return msg;

```

You haven't said anything about the required format of the timestamp - I've used `Date.now()` to get the current time in milliseconds since epoch. If you need a different format, that are countless examples of generating timestamps in JavaScript using the `Date` object.

---

<div class="post-metadata">

### Author: ![Traste](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/traste/32/5910_2.png) [@Traste](https://discourse.nodered.org/u/Traste)
#### Post date: [15 February 2019 21:54 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/5 "2019-02-15T21:54:09Z")

</div>

That really does sound great! Although I'm getting some kind of syntax error. Sorry, I'm new to coding :-/

![org%202019-02-15%2022-53-06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0bb0d2b229e221e9e43ed9f584bd03c34d445f0b.jpeg)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 February 2019 21:54 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/6 "2019-02-15T21:54:33Z")

</div>

Oops - I missed a comma after `msg.click`.

---

<div class="post-metadata">

### Author: ![Traste](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/traste/32/5910_2.png) [@Traste](https://discourse.nodered.org/u/Traste)
#### Post date: [15 February 2019 22:00 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/7 "2019-02-15T22:00:21Z")

</div>

Thanks! I got it working with this syntax:

```auto
var myObject = [
    {
        "time": new Date().toISOString(),
        "entity": "entity1",
        "type": "living_room",
        "state": msg.click,
        "value": "2",
        "location": "bedroom"
    }
];
msg.payload= JSON.stringify(myObject);
return msg;

```

You made my day!

As I said, thanks a million!

---

<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: [16 February 2019 07:23 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/8 "2019-02-16T07:23:43Z")

</div>

I would be interested to know why you want it as a JSON string rather than a js object.

---

<div class="post-metadata">

### Author: ![Traste](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/traste/32/5910_2.png) [@Traste](https://discourse.nodered.org/u/Traste)
#### Post date: [16 February 2019 19:30 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/9 "2019-02-16T19:30:13Z")

</div>

I'm using the BigQuery insert node, and that's the output format it needs 🙂

---

<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: [16 February 2019 21:25 UTC](https://discourse.nodered.org/t/how-to-pass-variables-values-to-function-node/8028/10 "2019-02-16T21:25:42Z")

</div>

> [@Traste](#):
>
> that's the format it needs

I see you are right, how unusual, normally one would expect to pass in an object not a json string.
