# 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:** 1

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

</div>

I am using a global variable to track my EV charge. At midnight, I am resetting it to 0 by using a change node, set global.evcharge to the value of 0. I am also using this variable to display in my dashboard. When I did this with a flow variable, it worked, after midnight the dashboard shows 0. With the change to global, it displays nothing. When I look at the context data for global variables, it does not show the variable at all until the variable changes to something other than 0

---

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

</div>

Hi.

Ok, so if you do it as a `flow` it works.

But NOT if you do it as `global`.

Dumb question/s:  
Are you sure you are looking at the `global` variable?

For the sake of showing people make a small flow that:  
sets the `global` variable to `1` or `0` (two `inject` nodes and 1 `change` node.)  
another `inject` node that connects to another `change` node that reads the global variable that goes to a `debug` node.

This way:  
You press an `inject` node to set it to `0` or `1`.  
Press the third `inject` node to display this.  
Change the global variable to the other one and read it again.

Here's a _better_ version but PLEASE change the name to what you are using.

```auto
[{"id":"0e2edc9aa3913160","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":2920,"y":270,"wires":[["699e6fb12fec3aa3","73c277a9942a2778"]]},{"id":"26558338df11d684","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":2920,"y":310,"wires":[["699e6fb12fec3aa3","73c277a9942a2778"]]},{"id":"699e6fb12fec3aa3","type":"change","z":"d188b95f33e5f7e4","name":"","rules":[{"t":"set","p":"your_variable","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3150,"y":290,"wires":[["ed18f7751484c569"]]},{"id":"73c277a9942a2778","type":"delay","z":"d188b95f33e5f7e4","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3100,"y":340,"wires":[["20356097461f44db"]]},{"id":"20356097461f44db","type":"change","z":"d188b95f33e5f7e4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"your_variable","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":3260,"y":340,"wires":[["d8904b4777916a0b"]]},{"id":"ed18f7751484c569","type":"debug","z":"d188b95f33e5f7e4","name":"SET","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3430,"y":290,"wires":[]},{"id":"d8904b4777916a0b","type":"debug","z":"d188b95f33e5f7e4","name":"READ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3430,"y":340,"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 08:37 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/3 "2023-08-23T08:37:29Z")

</div>

Dont ask me why, but you can see what happens after injecting 0. The read debug shows undefined, and in context data, the variable does not show. Actually the same happens with 1 now... weird.

 ![global variable](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62756a885185df4ba1ede6e5db3be7028f8baad7.jpeg)

---

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

</div>

Well that is at least good that _we_ can see the problem.

So you changed BOTH of the `change` nodes global variable's names.... Sorry gatta ask.  
Alas it is the small things that trip you up if you aren't careful.  
No offense.

---

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

</div>

Export those nodes and paste them here please. Select them all and use the menu Export to copy to clipboard. When pasting use the `</>` button at the top of the forum entry window to stop the forum from messing with the format. Then we can try your flow for ourselves.

---

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

</div>

None taken. Yes I did. I think I know one problem. My global.evcharge gets updated every second by my flow. And it doesnt like the 0. But in your test flow, I changed the name of the variable to global.evcharge2, and then it works

---

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

</div>

Well, if the low is updating your `global variable` every second, that isn't helping.

Another _dumb_ question:  
Why are you using GLOBAL rather than FLOW?

---

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

</div>

I need global as I am using the variable in another flow.

```auto
[
    {
        "id": "22ea5d674eda978d",
        "type": "switch",
        "z": "a58af580a096077f",
        "name": "",
        "property": "payload[0].che",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "evcharge",
                "vt": "global"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1850,
        "y": 440,
        "wires": [
            [
                "b1174e7b21b6e391"
            ],
            [
                "b1174e7b21b6e391"
            ]
        ]
    },
    {
        "id": "b1174e7b21b6e391",
        "type": "change",
        "z": "a58af580a096077f",
        "name": "payload to 0",
        "rules": [
            {
                "t": "set",
                "p": "payload[0].che",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "evcharge",
                "pt": "global",
                "to": "payload[0].che",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "evchargeold",
                "pt": "global",
                "to": "payload[0].che",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2070,
        "y": 440,
        "wires": [
            [
                "b442b601ac73b984"
            ]
        ]
    },
    {
        "id": "f04d3826330fa38b",
        "type": "change",
        "z": "a58af580a096077f",
        "name": "set evcharge",
        "rules": [
            {
                "t": "set",
                "p": "evcharge",
                "pt": "global",
                "to": "payload[0].che",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "evchargeold",
                "pt": "global",
                "to": "payload[0].che",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2070,
        "y": 480,
        "wires": [
            [
                "b442b601ac73b984"
            ]
        ]
    },
    {
        "id": "047fb54adc8eec28",
        "type": "switch",
        "z": "a58af580a096077f",
        "name": "connected ?",
        "property": "evstate",
        "propertyType": "flow",
        "rules": [
            {
                "t": "else"
            },
            {
                "t": "eq",
                "v": "disconnected",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1870,
        "y": 480,
        "wires": [
            [
                "f04d3826330fa38b"
            ],
            [
                "f04d3826330fa38b"
            ]
        ]
    },
    {
        "id": "b37acd80bdde8457",
        "type": "time-range-switch",
        "z": "a58af580a096077f",
        "name": "",
        "lat": "",
        "lon": "",
        "startTime": "23:58",
        "endTime": "23:59",
        "startOffset": 0,
        "endOffset": 0,
        "x": 1680,
        "y": 460,
        "wires": [
            [
                "22ea5d674eda978d"
            ],
            [
                "047fb54adc8eec28"
            ]
        ]
    }
]

```

---

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

</div>

Ok I found the issue. When the value is 0 (no charge), the node inputting the data into the time switch, simply does not have msg.payload[0].che, its not being set. So I need to set msg.payload[0].che to 0 if its not set I think

---

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

</div>

That's good you found the problem.

I was going to ask you something but won't if you found the problem.  
(If you are confident it is _resolved_ mark your last post as the `solution`.)

---

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

</div>

Nearly there, but my solution isnt working. I am using a switch node, and if the messge is empty (tried null too) I set msg.payload[0].che to 0. But that doesnt work, probably as when there is not charge to report, the node simply doesnt send msg.payload[0].che, just other data

---

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

</div>

Ok, I'll ask. 😉

1 - Why not use an `inject` node set to inject messages at given times? I am not understanding why you are using the `time range` node.  
2 - The top `switch` node: doesn't make sense (to me) what it does. Both outputs (results) go to the same place, so it doesn't really matter what the value is.  
3 - The lower `switch` node: ~~The `otherwise` is at the top and you haven't~~ Ok sorry. You have the box set at the bottom to `check all rules`. But again: as both outputs go to the same place: what difference does it make?  
4 - `payload to 0` node: you set `payload[0].che` to `0` then you set global `evcharge` to `payload[0].che`  
Why not just do it directly? set `global evcharge` to `0`?

Not having a go at you, but this sort of stuff CAN trip you up sometimes.

---

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

</div>

Could you put a `debug` (set to show COMPLETE MESSAGE) and paste the message with the `msg.payload[0].che` part in it?

And explain the parts that you are using to control 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 09:21 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/14 "2023-08-23T09:21:44Z")

</div>

To be honest I was thinking the same about the switch nodes, just didnt change it. I have now.  
I also use a inject node to reset my values to 0 at midnight. Still, as there is no value being sent (unless msg.payload[0].che has a value), every message resets (deletes) global.evcharge

---

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

</div>

Only if you want to:

Could you try to explain what the big picture goal is - again?

This is what I understand - right or wrong:

Your EV needs a charge.  
You plug it in to charge it (at unknown time)

Why do you need to set _something_ to `0` at midnight?  
What does it .... do?

Sorry, just to help me (and my monkey brain) understand better what you are trying to do.

I'm guessing the EV tells you when it is _full_/charged.

---

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

</div>

I am displaying how many kwh of charge has gone into my EV. (I also display if the charger is connected, rate of charge in watts etc). I do not have fixed times for plugging in, and also as I am charging mainly of my PV panels which are all linked to the EV charger, there is data coming from the chargeer about PV production, house consumption etc.  
If I have been charging during the day, and charging has stopped, it would not display any value. Until midnight, I would like to see the total charge for the day. That works currently. Then, at midnight, I want it to display 0, until charging starts.  
Hope that makes sense

---

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

</div>

Ok, with you for what you want to do.

So you are getting messages from the car every.... regular intervals. Giving you a message if it is charged or not. Anything else?

Meanwhile: Your charger is monitoring how much power is going into the car.

You plug in the car and it starts charging - conditions determined by the charger.

You would like to see how much power went into the car for that 24 hour period.  
At midnight a new day starts (obviously) and you want to know _yesterdays_ values. (Being it is now a new day)

Is that about right?

---

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

</div>

yes, except for after midnight, I want the value to be 0 until it starts 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 09:47 UTC](https://discourse.nodered.org/t/cannot-set-global-variable-to-0/80737/19 "2023-08-23T09:47:13Z")

</div>

What happens if the car is charging at/over/past midnight?

Ok, I didn't explain that good enough.

So at midnight things are reset for the new day.

But what does the car tell you and how do you know how much power has gone into it?

The message: Does it contain stuff from the car AND the charger or do you get two separate messages. One from the car and one from the charger?

---

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

</div>

I have never charged the car over midnight, so havent thought about that. I think I would like to display the charge up to midnight (I could save that value in case I needed it), reset to 0 at midnight, and then display the value of charge after midnight. I do not get any values or data from the car. It is all coming from the charger. And the charger reports everything from PV values, power going into my hot water from PV panels, how much I draw from the grid etc. This date I update every 2 seconds.

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