Getting grid_total from HA

Im trying to get the grid total value from HA energy tab. But if i understand correct this is not a value you can get within HA, so i wanted to do that myself.

for that i made 4 utility meter Helpers. and in nodered i made the following function node

var tarif1 = global.get('homeassistant.homeAssistant.states["sensor.consumption_tarif1"].state');
var tarif2 = global.get('homeassistant.homeAssistant.states["sensor.consumption_tarif2"].state');
var tarif3 = global.get('homeassistant.homeAssistant.states["sensor.production_tarif1"].state');
var tarif4 = global.get('homeassistant.homeAssistant.states["sensor.production_tarif2"].state');

let c1 = parseFloat(tarif1);
let c2 = parseFloat(tarif2);
let p1 = parseFloat(tarif3);
let p2 = parseFloat(tarif4);

msg.consumption = (c1 + c2);
msg.production = (p1 + p2);
msg.gtt = msg.consumption - msg.production;

return msg;

But no matter what i do i keep getting NaN as msg. and i dont know why.

Anybode? thanks!

You would need to check what the values in tarif1-4 are
Add an
node.warn({tarif1,tarif2,tarif3,tarif4})
to line 5 and run the code, you will then see the HA global values that are being returned in the debug sidebar.

Hi E1cid, thanks! now i'm a little more lost. :rofl:

node.warn gives the follwing:

function : (warn)
{ tarif1: undefined, tarif2: undefined, tarif3: undefined, tarif4: undefined }

If i make 4 'current state node' each come with the correct value.

exp. below

msg : Object
{ _msgid: "90bb32029ec3a49e", payload: "2.068", data: object }

Just to be sure, in the command global.get('homeassistant.homeAssistant.states.....

what are homeassistant and homeAssistant representing? the name off the configuration node or?, maybe i'm messing something up there (i'm running everything in docker containers, not supervised).

Thanks for thinking with me!

So they are undefined, which will result in a NaN.
Can you show us your context store in the sidebar showing the HA global vars.

This is really a HA question and you may find a quicker answer on the HA forum.

I hope this is the correct info that you asked for?

image

If you clicked refresh in the global section, it would appear that there are no global context vars, that would be why they are undefined.

I am not a HA user.

So you need to use the state change nodes to get the readings and add them to context. Set up 4 state change nodes and use change node to set them to a context (global) var.
e.g
set global tarif1
to value of msg. payload.

Then use global.get("tarif1")

Thanks.

I will try with your hints and ask further in the HA forums.

May i quote your last answer there?

Thanks again!

You are free to do whatever you wish. You may find there is a way to get states direct to node-red context. As I am not a HA user my answer is Just what info I have seen over the years, HA users may have more info for you

I think i made work with below flow (you now see that i feed the output to a matrix). I'm so proud on that. :yum:

but, the msg node outputs 4x the same message. is that correct? or is there still something missing?

var tarif1 = global.get('consumption_tarif1');
var tarif2 = global.get('consumption_tarif2');
var tarif3 = global.get('production_tarif1');
var tarif4 = global.get('production_tarif2');

let c1 = parseFloat(tarif1);
let c2 = parseFloat(tarif2);
let p1 = parseFloat(tarif3);
let p2 = parseFloat(tarif4);

var consumption = (c1 + c2);
var production = (p1 + p2);
var gtt = (consumption - production).toFixed(2);

//msg.payload = price;

msg.payload = {
    //   "text": value + ' KW', 
    "text": gtt + ' ' + 'kWh',
    "icon": 32930,
    "repeat": 1,
    "pushIcon": 0
};

node.status({ fill: 'green', shape: 'ring', text: gtt + ' ' + 'kWh' });

return msg;

Export your flow. How to import/Export a flow and paste here and i will give you a join example.

You just need to join the payload, probably don't require context at all, unless you want the readings elsewhere in another flow.

[fixed by moderator]

[{"id":"3d3ac876aebd8472","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"51baa685a0efa654","type":"inject","z":"3d3ac876aebd8472","name":"","props":[{"p":"payload"}],"repeat":"60","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":150,"y":540,"wires":[["598eee99d92290b9","537c9b9708543493","cd142b43a9c0c375","ed6aae5687ab99d2"]]},{"id":"1c7e2e601b6ef29b","type":"debug","z":"3d3ac876aebd8472","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":660,"wires":[]},{"id":"598eee99d92290b9","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get production_tarif1","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.production_tarif1","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":540,"wires":[["7ac52375126756ec"]]},{"id":"537c9b9708543493","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get production_tarif2","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.production_tarif2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":600,"wires":[["c21d3b73b37e1446"]]},{"id":"cd142b43a9c0c375","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get consumption_tarif1","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.consumption_tarif1","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":660,"wires":[["f0996d22acc979d4"]]},{"id":"ed6aae5687ab99d2","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get consumption_tarif1","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.consumption_tarif2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":720,"wires":[["b54dbb9ab83c8da4"]]},{"id":"e9ba902d7ff71e91","type":"function","z":"3d3ac876aebd8472","name":"","func":"var tarif1 = global.get('consumption_tarif1');\nvar tarif2 = global.get('consumption_tarif2');\nvar tarif3 = global.get('production_tarif1');\nvar tarif4 = global.get('production_tarif2');\n\nlet c1 = parseFloat(tarif1);\nlet c2 = parseFloat(tarif2);\nlet p1 = parseFloat(tarif3);\nlet p2 = parseFloat(tarif4);\n\nvar consumption = (c1 + c2);\nvar production = (p1 + p2);\nvar gtt = (consumption - production).toFixed(2);\n\n//msg.payload = price;\n\nmsg.payload = {\n    //   \"text\": value + ' KW', \n    \"text\": gtt + ' ' + 'kWh',\n    \"icon\": 32930,\n    \"repeat\": 1,\n    \"pushIcon\": 0\n};\n\nnode.status({ fill: 'green', shape: 'ring', text: gtt + ' ' + 'kWh' });\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":540,"wires":[["1c7e2e601b6ef29b","6f96734b1b49efb4"]]},{"id":"c21d3b73b37e1446","type":"change","z":"3d3ac876aebd8472","name":"production_tarif2","rules":[{"t":"set","p":"production_tarif2","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":600,"wires":[["e9ba902d7ff71e91"]]},{"id":"f0996d22acc979d4","type":"change","z":"3d3ac876aebd8472","name":"consumption_tarif1","rules":[{"t":"set","p":"consumption_tarif1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":660,"wires":[["e9ba902d7ff71e91"]]},{"id":"b54dbb9ab83c8da4","type":"change","z":"3d3ac876aebd8472","name":"consumption_tarif2","rules":[{"t":"set","p":"consumption_tarif2","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":720,"wires":[["e9ba902d7ff71e91"]]},{"id":"7ac52375126756ec","type":"change","z":"3d3ac876aebd8472","name":"production_tarif1","rules":[{"t":"set","p":"production_tarif1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":540,"wires":[["e9ba902d7ff71e91"]]},{"id":"6f96734b1b49efb4","type":"mqtt out","z":"3d3ac876aebd8472","name":"","topic":"awtrix_6ffae0/custom/power_usage","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ab43d7d9dd038e9e","x":1200,"y":540,"wires":[]},{"id":"8c12cc5593f9646e","type":"server","name":"homeassistant","version":5,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":": ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"default","statusTimeFormat":"h:m","enableGlobalContextStore":false},{"id":"ab43d7d9dd038e9e","type":"mqtt-broker","name":"Mosqitto","broker":"192.168.2.203","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

You seem to have neglected to read @E1cid comment on how to post a flow.

We cannot import flows that are just 'Cut and Paste', the forum reformats some of the characters and so we lose some of the content.

You can edit your post by clicking the pencil icon.

I don't think my link mentions using </> button, but I did think OP knew this, as his js code is posted correctly.

@riddertommie here is a join example with no context

[{"id":"51baa685a0efa654","type":"inject","z":"3d3ac876aebd8472","name":"","props":[{"p":"payload"}],"repeat":"60","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":150,"y":540,"wires":[["598eee99d92290b9","537c9b9708543493","cd142b43a9c0c375","ed6aae5687ab99d2"]]},{"id":"598eee99d92290b9","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get production_tarif1","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.production_tarif1","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":540,"wires":[["7ac52375126756ec"]]},{"id":"537c9b9708543493","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get production_tarif2","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.production_tarif2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":600,"wires":[["c21d3b73b37e1446"]]},{"id":"cd142b43a9c0c375","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get consumption_tarif1","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.consumption_tarif1","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":660,"wires":[["f0996d22acc979d4"]]},{"id":"ed6aae5687ab99d2","type":"api-current-state","z":"3d3ac876aebd8472","name":"Get consumption_tarif1","server":"8c12cc5593f9646e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.consumption_tarif2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":720,"wires":[["b54dbb9ab83c8da4"]]},{"id":"7ac52375126756ec","type":"change","z":"3d3ac876aebd8472","name":"production_tarif1","rules":[{"t":"set","p":"topic","pt":"msg","to":"production_tarif1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":540,"wires":[["0525f009e6dda23f"]]},{"id":"c21d3b73b37e1446","type":"change","z":"3d3ac876aebd8472","name":"production_tarif2","rules":[{"t":"set","p":"topic","pt":"msg","to":"production_tarif2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":600,"wires":[["0525f009e6dda23f"]]},{"id":"f0996d22acc979d4","type":"change","z":"3d3ac876aebd8472","name":"consumption_tarif1","rules":[{"t":"set","p":"topic","pt":"msg","to":"consumption_tarif1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":660,"wires":[["0525f009e6dda23f"]]},{"id":"b54dbb9ab83c8da4","type":"change","z":"3d3ac876aebd8472","name":"consumption_tarif2","rules":[{"t":"set","p":"topic","pt":"msg","to":"consumption_tarif2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":720,"wires":[["0525f009e6dda23f"]]},{"id":"0525f009e6dda23f","type":"join","z":"3d3ac876aebd8472","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":620,"wires":[["e9ba902d7ff71e91"]]},{"id":"e9ba902d7ff71e91","type":"function","z":"3d3ac876aebd8472","name":"","func":"var tarif1 = msg.consumption_tarif1;\nvar tarif2 = msg.consumption_tarif2;\nvar tarif3 = msg.production_tarif1;\nvar tarif4 = msg.production_tarif2;\n\nlet c1 = parseFloat(tarif1);\nlet c2 = parseFloat(tarif2);\nlet p1 = parseFloat(tarif3);\nlet p2 = parseFloat(tarif4);\n\nvar consumption = (c1 + c2);\nvar production = (p1 + p2);\nvar gtt = (consumption - production).toFixed(2);\n\n//msg.payload = price;\n\nmsg.payload = {\n // \"text\": value + ' KW', \n \"text\": gtt + ' ' + 'kWh',\n \"icon\": 32930,\n \"repeat\": 1,\n \"pushIcon\": 0\n};\n\nnode.status({ fill: 'green', shape: 'ring', text: gtt + ' ' + 'kWh' });\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":540,"wires":[["1c7e2e601b6ef29b","6f96734b1b49efb4"]]},{"id":"1c7e2e601b6ef29b","type":"debug","z":"3d3ac876aebd8472","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":660,"wires":[]},{"id":"6f96734b1b49efb4","type":"mqtt out","z":"3d3ac876aebd8472","name":"","topic":"awtrix_6ffae0/custom/power_usage","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ab43d7d9dd038e9e","x":1200,"y":540,"wires":[]},{"id":"ab43d7d9dd038e9e","type":"mqtt-broker","name":"Mosqitto","broker":"192.168.2.203","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

Untested as I do not have HA installed.

Hi E1cid FYI, I did run both flows and now the following happens:

Because off the bad weather here in Holland, the solar panels are not providing anything, (giving -0).

Your (joined) version is now showing NaN
and my (old) version is still showing correct butt still sending 4 messages

I tried to copy your join node in my flow but then its complete not working, but i also see that you did something else with getting the var trafik1-4.

If i changes this in your flow

var tarif1 = msg.consumption_tarif1;
var tarif2 = msg.consumption_tarif2;
var tarif3 = msg.production_tarif1;
var tarif4 = msg.production_tarif2;

to

var tarif1 = global.get('consumption_tarif1');
var tarif2 = global.get('consumption_tarif2');
var tarif3 = global.get('production_tarif1');
var tarif4 = global.get('production_tarif2');

it works as a charm, one message with the correct value. (it does send 2 messages the first time, but no problem there i think).

Thanks!

If you want help please provide the output of the join node, can not work on guess work. -0 should not produce a NaN. The join requires a topic on each wire. Context should be avoided if not really needed.

I had 4 typo's in my example

var tarif1 = msg.consumption_tarif1;
var tarif2 = msg.consumption_tarif2;
var tarif3 = msg.production_tarif1;
var tarif4 = msg.production_tarif2;

should be

var tarif1 = msg.payload.consumption_tarif1;
var tarif2 = msg.payload.consumption_tarif2;
var tarif3 = msg.payload.production_tarif1;
var tarif4 = msg.payload.production_tarif2;

Hi, with your last add, everything is working great now.

Thanks!

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