# Cannot set global variable to 0

**URL:** <https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737>\
**Category:** General\
**Created:** [23 August 2023 08:21 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737 "2023-08-23T08:21:42Z")\
**Posts on this page:** 18\
**Page:** 3

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [23 August 2023 11:52 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/41 "2023-08-23T11:52:12Z")

</div>

Only payload[0].che is currently stored, in global.evcharge (which I reset to 0 at midnight)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 11:55 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/42 "2023-08-23T11:55:11Z")

</div>

What are those 3 values?

gen = ?? Generated power used?  
grd = ?? Grid power used?  
che = ?? Charged? (Power going/gone into the car?)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 12:06 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/43 "2023-08-23T12:06:48Z")

</div>

Here is a SUGGESTION.

Not proven to work, but it shows you the idea of how to make it fairly easy.

Given:  
The `Data Messages` node is what you receive every 2 seconds.  
and the `Midnight signal` triggers at midnight. I haven't set that, but it is easy enough to do.

It doesn't disconnect the power when the car is _full_ as you haven't told me what message is received to signal that.

```auto
[{"id":"e2496d38a39c4158","type":"function","z":"d188b95f33e5f7e4","name":"function 31","func":"if (msg.payload == \"MIDNIGHT\")\n{\n // Move variables to yesterday's values.\n global.set(\"ya\",global.get(\"a\"));\n global.set(\"yb\", global.get(\"b\"));\n global.set(\"yc\", global.get(\"c\"));\n // Wipe values for the new day.\n global.set(\"a\",0);\n global.set(\"b\",0);\n global.set(\"c\",0);\n return; // Exit with nothing.\n}\n\nglobal.set(\"a\", msg.payload[0].gen);\nglobal.set(\"b\", msg.payload[0].grd);\nglobal.set(\"c\", msg.payload[0].che);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3850,"y":530,"wires":[["663e05233356e4ad"]]},{"id":"8d95cee44fc6fbc4","type":"inject","z":"d188b95f33e5f7e4","name":"Data messages.","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":3620,"y":530,"wires":[["e2496d38a39c4158"]]},{"id":"cb0df40e254149a1","type":"inject","z":"d188b95f33e5f7e4","name":"Midnight signal","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"MIDNIGHT","payloadType":"str","x":3790,"y":430,"wires":[["e2496d38a39c4158"]]},{"id":"663e05233356e4ad","type":"debug","z":"d188b95f33e5f7e4","name":"The rest of the flow.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4070,"y":530,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [23 August 2023 12:15 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/44 "2023-08-23T12:15:26Z")

</div>

> [@Mikee1234K](#):
>
> payload[0].gen = pv generation  
> payload[0].grd = power drawn from the grid  
> payload[0].mgl = max green level (when I use a eco setting on the charger)  
> payload[0].che = total charge of the ev

payload[0].gen = pv generation  
payload[0].grd = power drawn from the grid  
payload[0].che = total charge of the ev in kwh

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 12:15 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/45 "2023-08-23T12:15:35Z")

</div>

There is another _problem_ I can see.

Say you charge the car in the morning. Say from 06:00 to ..... 08:00.  
You've used power.

You drive to work and then home.

Then put it on charge when you get home at.... 18:00.  
Does this new charge cycle add to the morning's charge power used?

All good if that is how it works.

The only time my flow _resets_ is at `MIDNIGHT`.

You can use that signal to then (after a short delay) send _yesterday's_ values to somewhere.  
And I haven't sent a `0` to wipe stuff. Sorry. But that would be done in this part of the node:

```auto
    global.set("a",0);
    global.set("b",0);
    global.set("c",0);
    return; // Exit with nothing.

```

You would construct a message to send a `0` in which ever part you need.  
Sorry. I missed that when I wrote it. I am trying to keep it at/in the bigger picture and not micromanage how you do it.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [23 August 2023 12:16 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/46 "2023-08-23T12:16:28Z")

</div>

I'll have a look at that.  
There is no message when the car is full, it simply will stop charging

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 12:16 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/47 "2023-08-23T12:16:37Z")

</div>

Yeah, sorry. As I just posted.....

I hope that helps you though.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 12:17 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/48 "2023-08-23T12:17:21Z")

</div>

How do you detect that?

Oh, the `0` value.... Ok.  
(Right?)  
Could you clarify that?

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [23 August 2023 12:19 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/49 "2023-08-23T12:19:20Z")

</div>

If you compare the first debug I sent, and the second, there is a difference in thet in the second ebug message is a payload[0].che, in the first there isnt. So when there isnt, it means no charge so far, so I would like to set global.evcharge to 0 until there is a value

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 12:24 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/50 "2023-08-23T12:24:42Z")

</div>

Ok, slight tweak.

_(The hamster is slipping a bit in the wheel)_

```auto
[{"id":"e2496d38a39c4158","type":"function","z":"d188b95f33e5f7e4","name":"function 31","func":"if (msg.payload == \"MIDNIGHT\")\n{\n // Move variables to yesterday's values.\n global.set(\"ya\",global.get(\"a\"));\n global.set(\"yb\", global.get(\"b\"));\n global.set(\"yc\", global.get(\"c\"));\n // Wipe values for the new day.\n global.set(\"a\",0);\n global.set(\"b\",0);\n global.set(\"c\",0);\n\n msg.payload[0].che = 0;\n\n return msg; //\n}\n\nglobal.set(\"a\", msg.payload[0].gen);\nglobal.set(\"b\", msg.payload[0].grd);\nglobal.set(\"c\", msg.payload[0].che);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3850,"y":530,"wires":[["663e05233356e4ad"]]},{"id":"8d95cee44fc6fbc4","type":"inject","z":"d188b95f33e5f7e4","name":"Data messages.","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"dat\":\"23-08-2023\",\"tim\":\"10:27:52\",\"ectp1\":0,\"ectp2\":0,\"ectp3\":0,\"ectt1\":\"Internal Load\",\"ectt2\":\"None\",\"ectt3\":\"None\",\"bsm\":0,\"bst\":0,\"cmt\":254,\"dst\":1,\"div\":0,\"frq\":50.21,\"fwv\":\"3562S4.539\",\"gen\":1887,\"grd\":48,\"pha\":1,\"pri\":1,\"sta\":1,\"tz\":0,\"vol\":2455,\"bss\":0,\"lck\":16,\"pst\":\"B1\",\"zmo\":3,\"pwm\":5216,\"zs\":1026,\"rdc\":3,\"rac\":6,\"zsh\":4,\"zsl\":2,\"ectt4\":\"None\",\"ectt5\":\"None\",\"ectt6\":\"None\",\"newAppAvailable\":false,\"newBootloaderAvailable\":false,\"beingTamperedWith\":false,\"batteryDischargeEnabled\":false,\"g100LockoutState\":\"NONE\",\"mgl\":100,\"sbh\":17,\"sbk\":5}]","payloadType":"jsonata","x":3620,"y":530,"wires":[["e2496d38a39c4158","10453d1ffb80afe9"]]},{"id":"cb0df40e254149a1","type":"inject","z":"d188b95f33e5f7e4","name":"Midnight signal","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"MIDNIGHT","payloadType":"str","x":3790,"y":430,"wires":[["e2496d38a39c4158"]]},{"id":"663e05233356e4ad","type":"debug","z":"d188b95f33e5f7e4","name":"The rest of the flow.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4070,"y":530,"wires":[]},{"id":"10453d1ffb80afe9","type":"switch","z":"d188b95f33e5f7e4","name":"Charged?","property":"payload[0].che","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":3840,"y":570,"wires":[["eb9fb4f4a4a899f4"]]},{"id":"eb9fb4f4a4a899f4","type":"function","z":"d188b95f33e5f7e4","name":"Car full","func":"msg.payload[0].che = 0;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4040,"y":570,"wires":[["663e05233356e4ad"]]}]

```

Have a look at this one instead.

(ARGH! yes, the hamster is really not doing good now.)  
The `car full` node is kinda redundant.

But again: I am trying to keep this as an overview.  
That would be replaced with a message sent to YOU. What that `function` node does is done in the other one anyway.  
This is more a _spur_ to indicate to you the car is full.

`function 31` alternate/new code:

```auto
if (msg.payload == "MIDNIGHT")
{
    // Move variables to yesterday's values.
    global.set("ya",global.get("a"));
    global.set("yb", global.get("b"));
    global.set("yc", global.get("c"));
    // Wipe values for the new day.
    global.set("a",0);
    global.set("b",0);
    global.set("c",0);

    msg.payload[0].che = 0;

    return msg; //
}

if (msg.payload[0].che == 0)
{
    // Car is now charged.
    //
    // You need to construct a message that you send.
    // Maybe just returning the message would
    // suffice?
    return msg;
}

global.set("a", msg.payload[0].gen);
global.set("b", msg.payload[0].grd);
global.set("c", msg.payload[0].che);

return msg;

```

You don't need the `switch` or `car full` nodes now.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [23 August 2023 16:07 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/51 "2023-08-23T16:07:08Z")

</div>

That all makes sense. I went out with the car, and plugged it back in on my return. Its not charging again (yet). Its not displaying 0, its blank. So what I thought would happen does. When the .che payload doesnt exist, like when its not actually charging, its not setting evcharge to 0.  
I have not incorporated your flow to replace mine as I had to pop out

Just tested your function node (the amended one) and its the same, its not 'catching' the .che payload not existing

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 August 2023 20:52 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/52 "2023-08-23T20:52:06Z")

</div>

Ok, new day. New attempt at solving the problem.

Here is some new `function 31` code to try:

```auto
// This is to happen when the car is connected for charging
// but still hasn't actually started charging.
// Done first to catch/stop any problems if it isn't set.
// And if it isn't set, do we really need to store any of it's data?
if (msg.payload[0].che == null) {
    //
    msg.payload[0].che = 0;
    return msg; //
}

// This happens at midnight.
if (msg.payload == "MIDNIGHT") {
    // Move variables to yesterday's values.
    // Those are gloobal context named: ya yb yc
    global.set("ya", global.get("a"));
    global.set("yb", global.get("b"));
    global.set("yc", global.get("c"));
    // Wipe values for the new day.
    global.set("a", 0);
    global.set("b", 0);
    global.set("c", 0);

    msg.payload[0].che = 0;

    return msg; //
}

// This is to happen when the car is fully charged.
if (msg.payload[0].che == 0) {
    // Car is now charged.
    //
    // You need to construct a message that you send.
    // Maybe just returning the message would
    // suffice?
    return msg;
}

// This is what normally happens.
// global contexts set to payload values.
global.set("a", msg.payload[0].gen);
global.set("b", msg.payload[0].grd);
global.set("c", msg.payload[0].che);

return msg;

```

I gave it a quick test and it seemed to do what I wanted.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [24 August 2023 07:57 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/53 "2023-08-24T07:57:30Z")

</div>

We are getting closer. Adding the .che ==0 did not work initially. Looking at a debug node it shows .che = 0. But its not setting evcharge to 0. What I do not understand, later in the function, if .che = 0 it sets global.evcharge to 0. But when I look at the context data, its not there, so somehow that gets ignored ?  
But, just out of curiosity, I put a change node after the function node, which sets global.evcharge to .che  
Now it works as intended.

---

<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:** [24 August 2023 08:05 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/54 "2023-08-24T08:05:26Z")

</div>

> [@Trying\_to\_learn](#):
>
> `if (msg.payload[0].che == null) {`

That is not going to work well. If msg.payload is "MIDNIGHT" then msg.payload[0].che will be null, so it will never get to your MIDNIGHT test. Also, the line `msg.payload[0].che = 0` will do nothing as msg.payload is not an array. See this test flow to demonstrate

```auto
[{"id":"83f0158803738885","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"MIDNIGHT","payloadType":"str","x":300,"y":2300,"wires":[["4205f065d7caa3c7"]]},{"id":"4205f065d7caa3c7","type":"function","z":"bdd7be38.d3b55","name":"function 7","func":"if (msg.payload[0].che == null) {\n //\n node.warn(\"msg.payload[0].che is null\")\n // but the next line does nothing because msg.payload[0] is not an object\n msg.payload[0].che = 0;\n return msg; //\n}\n\n// This happens at midnight.\nif (msg.payload == \"MIDNIGHT\") {\n msg.payload = \"found MIDNIGHT\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":2300,"wires":[["b3faeb9cf780af13"]]},{"id":"b3faeb9cf780af13","type":"debug","z":"bdd7be38.d3b55","name":"debug 99","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":2300,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2023 08:48 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/55 "2023-08-24T08:48:41Z")

</div>

I haven't looked yet, but you are rigtht.

I am doing most of this _by the seat of my pants_.  
I know that isn't good, but at this point it is the best I can do.

I'll look at your post/code now.

_(just did that now)_

Not wanting to look/sound argumentative but if I put the `midnight` test before the testing if that part of the message is `null`.... Would that help?

I understand the incoming message is an array but is _missing_ that part. The other parts do.  
So... as I read it that should work.  
I admit I used an example of what was posted but ok: it doesn't have a `payload` part now I look.

I am stuck how to get a good example of a message.  
(No blame assigned.)  
Can you see one? Could you help me get/find it?

Thanks though for the help.

Would this be better?

```auto
// This happens at midnight.
if (msg.payload == "MIDNIGHT") {
    // Move variables to yesterday's values.
    // Those are gloobal context named: ya yb yc
    global.set("ya", global.get("a"));
    global.set("yb", global.get("b"));
    global.set("yc", global.get("c"));
    // Wipe values for the new day.
    global.set("a", 0);
    global.set("b", 0);
    global.set("c", 0);

    msg.payload[0].che = 0;

    return msg; //
}

// This is to happen when the car is connected for charging
// but still hasn't actually started charging.
if (msg.payload[0].che == null) {
    //
    msg.payload[0].che = 0;
    return msg; //
}

// This is to happen when the car is fully charged.
if (msg.payload[0].che == 0) {
    // Car is now charged.
    //
    // You need to construct a message that you send.
    // Maybe just returning the message would
    // suffice?
    return msg;
}

// This is what normally happens.
// global contexts set to payload values.
global.set("a", msg.payload[0].gen);
global.set("b", msg.payload[0].grd);
global.set("c", msg.payload[0].che);

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:** [24 August 2023 15:27 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/56 "2023-08-24T15:27:42Z")

</div>

I have not been following the thread. I just noticed that the code you posted was not going to work.  
If you want me to have a look I need to know what is in the various input message types and what output is required in each case.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2023 20:44 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/57 "2023-08-24T20:44:58Z")

</div>

This is my last/latest effort at getting it working.

```auto
[{"id":"8d95cee44fc6fbc4","type":"inject","z":"d188b95f33e5f7e4","name":"Data messages.","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"sno\":xxx,\"dat\":\"23-08-2023\",\"tim\":\"11:37:42\",\"ectp1\":1534,\"ectp2\":0,\"ectp3\":0,\"ectt1\":\"Internal Load\",\"ectt2\":\"None\",\"ectt3\":\"None\",\"bsm\":0,\"bst\":0,\"cmt\":254,\"dst\":1,\"div\":1534,\"frq\":49.93,\"fwv\":\"3562S4.539\",\"gen\":2399,\"grd\":-15,\"pha\":1,\"pri\":1,\"sta\":3,\"tz\":0,\"vol\":2451,\"che\":0.42,\"bss\":0,\"lck\":16,\"pst\":\"C2\",\"zmo\":3,\"pwm\":1260,\"zs\":3078,\"rdc\":3,\"rac\":61,\"rrac\":-55,\"zsh\":12,\"zsl\":6,\"ectt4\":\"None\",\"ectt5\":\"None\",\"ectt6\":\"None\",\"newAppAvailable\":false,\"newBootloaderAvailable\":false,\"beingTamperedWith\":false,\"batteryDischargeEnabled\":false,\"g100LockoutState\":\"NONE\",\"mgl\":100,\"sbh\":17,\"sbk\":5}","payloadType":"jsonata","x":3620,"y":530,"wires":[["e2496d38a39c4158"]]},{"id":"e2496d38a39c4158","type":"function","z":"d188b95f33e5f7e4","name":"This node does the work.","func":"// Extract date from message.\nif (msg.payload.dat !== undefined)\n{\n var date = msg.payload.dat;\n context.set(\"date\",date);\n //node.warn(date);\n}\n\n// This happens at midnight.\nif (msg.payload == \"MIDNIGHT\") {\n // Move variables to yesterday's values.\n // Those are gloobal context named: ya yb yc\n // I have also created a new message (msg1) that sends\n // out data to be used in the `template` node for logging\n var msg1 = {};\n var old_a = global.get(\"a\");\n var old_b = global.get(\"b\");\n var old_c = global.get(\"c\");\n global.set(\"ya\", global.get(\"a\"));\n global.set(\"yb\", global.get(\"b\"));\n global.set(\"yc\", global.get(\"c\"));\n // Wipe values for the new day.\n global.set(\"a\", 0);\n global.set(\"b\", 0);\n global.set(\"c\", 0);\n\n msg.payload.che = 0;\n\n // This is sent out the second output of he `function` node\n // and is sent into a `template` node to then make\n // a human readable line of text for logging.\n msg1 = {\"a\":old_a,\"b\":old_b,\"c\":old_c,\"date\":context.get(\"date\")};\n\n return [msg,msg1]; //\n}\n\n// This is to happen when the car is connected for charging\n// but still hasn't actually started charging.\nif (msg.payload.che == null) {\n //\n msg.payload.che = 0;\n return msg; //\n}\n\n// This is to happen when the car is fully charged.\nif (msg.payload.che == 0) {\n // Car is now charged.\n //\n // You need to construct a message that you send.\n // Maybe just returning the message would\n // suffice?\n return msg;\n}\n\n// This is what normally happens.\n// global contexts set to payload values.\nglobal.set(\"a\", msg.payload.gen);\nglobal.set(\"b\", msg.payload.grd);\nglobal.set(\"c\", msg.payload.che);\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3890,"y":530,"wires":[["663e05233356e4ad"],["e6f960eb820b18ca"]]},{"id":"cb0df40e254149a1","type":"inject","z":"d188b95f33e5f7e4","name":"Midnight signal","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"MIDNIGHT","payloadType":"str","x":3790,"y":430,"wires":[["e2496d38a39c4158"]]},{"id":"663e05233356e4ad","type":"debug","z":"d188b95f33e5f7e4","name":"The rest of the flow.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4160,"y":530,"wires":[]},{"id":"e6f960eb820b18ca","type":"template","z":"d188b95f33e5f7e4","name":"Make message human readable","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Readings from {{date}} a {{a}} b {{b}} c {{c}}","output":"str","x":3840,"y":590,"wires":[["b72fd9acc186918d"]]},{"id":"b72fd9acc186918d","type":"debug","z":"d188b95f33e5f7e4","name":"Send this to a log file.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4130,"y":590,"wires":[]}]

```

NOTE:

I am confused with the message.

Your code mentions `msg.payload[0].che` and others like it.

But after a bit of testing and checking and testing, etc....

That doesn't look right.

It should be - as far as I can see:  
`msg.payload.che` No `[0]` in it.

It this fails try putting them back in on the appropriate lines in the `function` node.

---

<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:** [23 October 2023 20:45 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/58 "2023-10-23T20:45:18Z")

</div>

Closed on 23 October 2023 20:45 UTC

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

[Previous page](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737.md?page=2)
