Only payload[0].che is currently stored, in global.evcharge (which I reset to 0 at midnight)
What are those 3 values?
gen = ?? Generated power used?
grd = ?? Grid power used?
che = ?? Charged? (Power going/gone into the car?)
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.
[{"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":[]}]
payload[0].gen = pv generation
payload[0].grd = power drawn from the grid
payload[0].che = total charge of the ev in kwh
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:
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.
I'll have a look at that.
There is no message when the car is full, it simply will stop charging
Yeah, sorry. As I just posted.....
I hope that helps you though.
How do you detect that?
Oh, the 0
value.... Ok.
(Right?)
Could you clarify that?
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
Ok, slight tweak.
(The hamster is slipping a bit in the wheel)
[{"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:
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.
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
Ok, new day. New attempt at solving the problem.
Here is some new function 31
code to try:
// 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.
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.
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
[{"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":[]}]
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?
// 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;
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.
This is my last/latest effort at getting it working.
[{"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.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.