Cannot set global variable to 0

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

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.

[{"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":[]}]

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.

1 Like

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.

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.

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

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?

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

[
    {
        "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"
            ]
        ]
    }
]

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

1 Like

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.)

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

Ok, I'll ask. :wink:

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.

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..

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

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.

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

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?

yes, except for after midnight, I want the value to be 0 until it starts charging

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?

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.