# 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:** 20\
**Page:** 2

<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 10:01 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/21 "2023-08-23T10:01:41Z")

</div>

Ok, thanks.

So it is all coming from the charger. No car messages.

Can you post a message that is used while the car is charging and explain what part does/means what?  
(How much power has gone into the car, is the car charged?)

I appreciate you are doing a bit of extra stuff with your solar panels, etc.

But for this example _we_ will try to concentrate on the message ONLY for the car.  
2 seconds is fine for how often.

---

<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 10:02 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/22 "2023-08-23T10:02:56Z")

</div>

I think I found a solution. If my global.evcharge equals payload[0].che, I set payload[0].che to 0. That now displays 0 until the value changes

---

<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 10:03 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/23 "2023-08-23T10:03:58Z")

</div>

Good.

I am sorry if I wasted any of your time. But sometimes it helps to talk about it with others to help get a better understanding of what is happening.

---

<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 10:15 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/24 "2023-08-23T10:15:11Z")

</div>

Indeed it does. It certainly helped me simplifying my flows. Still not entirely sure it will work as intended, I have to see what happens once the car starts charging, and what happens after midnight.  
I also need to think about what you said, what do I want if I charge over night, which will happen in the winter when I do not have enough power from my PV panels to charge the car, and need to use the grid power over night. I need to rething everything. I also need to grab screenshots of the debug of the charger when charging, and when not charging to see the differences  
Your help and time is much appreciated

---

<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 10:22 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/25 "2023-08-23T10:22:38Z")

</div>

I'm glad I could help.

Suggestion:

The `debug` nodes and their output.  
Save them to a file for a few days.  
But don't just save the messages. That makes it (sometimes) difficult to read later on.

Put them through a `template` node to _format_ the message into something human readable.  
Then you get a readable log file that you can go back and look at later AND understand. 😉

I can help you with that if you want.

The `midnight` part shouldn't be that difficult.  
_We_ just need to discuss where/how the important values are stored.

---

<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 10:26 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/26 "2023-08-23T10:26:02Z")

</div>

Silly question. How do I put them through a template node ?

---

<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 10:26 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/27 "2023-08-23T10:26:40Z")

</div>

Well, it would help if you showed me an entire message and explain what all the parts mean.  
(Copy a real one from the `debug` node - use the copy button, and paste it with the `</>` stuff)

---

<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 10:39 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/28 "2023-08-23T10:39:23Z")

</div>

Here is a sample message

```auto
23/08/2023, 11:27:53node: debug 175msg : Object
object
payload: array[1]
0: object
sno: xxx
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
_msgid: "0c7dc120b3c1fc99"

```

The data I am using from this is

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 (currently not rported as no charge so far today)  
payload[0].div = pv power going into my car  
payload[0].zmo = charger mode  
payload[0].pst = ev mode

I think that is what I am using (so far)

---

<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 10:47 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/29 "2023-08-23T10:47:06Z")

</div>

I'm not sure how you got that message.

Look here (example but not quite a big message like yours)

 ![Screenshot from 2023-08-23 20-42-43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/3866b9b4a5dc73841171470a57b58acb58ad81f1.png)

If you click on that `copy` icon/button it copies the value/structure to the clipboard.  
Before you do that though, _expand_ any groups.  
They are indicated by little triangles like seen just left of the `object` to the left of where the arrow is pointing.  
Click on them and any groups.

Then copy it and paste it.  
That way I will see the entire structure.  
(sorry)

---

<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 10:49 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/30 "2023-08-23T10:49:14Z")

</div>

here you go

```auto
{"payload":[{"sno":xxx,"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}],"_msgid":"0c7dc120b3c1fc99"}

```

---

<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 10:49 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/31 "2023-08-23T10:49:57Z")

</div>

Edit it and put 3 of the ` at the start and 3 at the end.

That way it will be expanded.  
(I'm guessing you just used 1 of the reverse ' at the start and end.)  
Or use the `</>` button.

---

<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 10:53 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/32 "2023-08-23T10:53:00Z")

</div>

I did use the \</\> button

---

<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 10:54 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/33 "2023-08-23T10:54:07Z")

</div>

Don't worry. I copied it and pasted it into an `inject` node and am looking at it now.  
Hang on.

---

<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:02 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/34 "2023-08-23T11:02:51Z")

</div>

```auto
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 (currently not rported as no charge so far today)
payload[0].div = pv power going into my car
payload[0].zmo = charger mode
payload[0].pst = ev mode

```

Sorry, I'm distracted. Blazing saddles is on just now.

Ok.

Given the car is charging during the day from not quite full to full.

Can you show me the _formula_ you use to determine the numbers?

---

<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:07 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/35 "2023-08-23T11:07:57Z")

</div>

No formula needed, the charger is reporting the total charge in payload[0].che  
Its reported as total in kwh which has gone into the car. I do not get a battery level for the car, and cannot calculate it either if thats what you meant. I'd love to but Kia do not provide a api...

---

<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:10 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/36 "2023-08-23T11:10:14Z")

</div>

Sorry.

My bad.

So you are getting these message every 2 seconds.

At midnight, you want to store any values from the day just ended and get ready to store new values for the new day.

Is that right?

Sorry it isn't I am trying to be difficult. I just want to check. Nothing worse than me working on a wrong idea and only confusing the other person more.

So I like to check, check and double check sometimes.

---

<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:36 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/37 "2023-08-23T11:36:56Z")

</div>

Yes you are right. I think it would be good to store yesterdays values (in case I want them, I'll probably email them to myself at the end of day)  
The values which would need storing are

payload[0].gen  
payload[0].grd  
payload[0].che

---

<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:39 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/38 "2023-08-23T11:39:54Z")

</div>

Ok.

Let me think about it.

I'll make a sample flow for you. It will show the mechanics of what you could do but you will have to change some of the names, etc.

---

<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:43 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/39 "2023-08-23T11:43:37Z")

</div>

My car has just started charging. What I wanted to do doesnt really work. I think the problem will be tomorrow morning, when payload[0].che is not being sent, like in the first debug I have sent you.  
This is a debug for when it has charged today

```auto
{"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}

```

---

<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:46 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/40 "2023-08-23T11:46:06Z")

</div>

Ok, you've told me the values that you want to keep.

```auto
payload[0].gen
payload[0].grd
payload[0].che

```

Are they also stored in global variables and are those global variables set before the message gets here?  
If so: what are the glabal variable names?

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

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