Create sensors from node-red payload

I finally managed to read my SMA solar inverter with node-red.
Now I want to create a sensor from this values.

0: DC power string 1
1: DC power string 2

These will probably have the following attributes :

state_class: measurement
unit_of_measurement: W
device_class: power

I want also some kind of ‘state_class: total_increasing’ of those 2 values, so I can use them in my energy dashboard, so I can see what both strings are doing.

Can this be done ?
This is my payload :
image

object
String_DCpower: array[2]
0: 521
1: 1295
AC_power: 1673
AC_Freq: 50.01
AC_Current: 7.103
AC_Voltage: 236.43
available_sessions: 1

Use the entity node.

1 Like

I was checking that one, but at this moment, I have no clue how to get the array from String_DCpower inside this, and how to achieve the ‘total_increasing’ entity

msg.payload.String_DCpower[0]

How to make it increasing, not sure.
I assume you need to fetch the old value with current state node and add this value first.

Try this:


[{"id":"e1cb3f9a8619356c","type":"ha-api","z":"b5df08b19668074e","name":"create sensor","server":"541ade28.b4a62","version":1,"debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":1150,"y":510,"wires":[[]]},{"id":"fa97e65b78db9a10","type":"function","z":"b5df08b19668074e","name":"set payload","func":"currentcounter=flow.get('storedcounter')+1;\nflow.set('storedcounter',currentcounter);\nmsg.entity_id = `sensor.entitysensor`;\nmsg.payload = {\n    data: {\n        state: Date.now(),\n        attributes: {\n            'name':'somename',\n            'counter':currentcounter,\n            'somearray': [{'key1':'value1'},{'key2':'value2'}],\n            friendly_name: 'Some Name',\n            icon: 'mdi:pulse'\n        }\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":965,"y":510,"wires":[["e1cb3f9a8619356c"]]},{"id":"98730a7111643178","type":"inject","z":"b5df08b19668074e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":675,"y":510,"wires":[["741923f94529b2b2"]]},{"id":"c1f56763236e671c","type":"function","z":"b5df08b19668074e","name":"init storedcounter","func":"flow.set('storedcounter',0);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":825,"y":375,"wires":[[]]},{"id":"16a257076c5d70ca","type":"inject","z":"b5df08b19668074e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":615,"y":375,"wires":[["c1f56763236e671c"]]},{"id":"741923f94529b2b2","type":"api-current-state","z":"b5df08b19668074e","name":"check","server":"541ade28.b4a62","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sun.sun","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":815,"y":510,"wires":[["fa97e65b78db9a10"]]},{"id":"541ade28.b4a62","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]

This already works great.image
Now I have to find a way to get this cumulated value.
This is my N-R flow :

And a little explanation.

The init storedcounter initialize and set the value to 0. storedcounter is a flow value that will be kept from run to run.
The reason why the current state node is in the flow is that Node-Reds Entity node often fails if no other access have been made to Home Assistant lately. Its like the node can not make the connection itself, but if other nodes make it, then it can reuse it.

@WallyR

I get an error ?
image

Maybe it was some autocorrect that did it.
I made it into a code block, so try now

And the click the inject on the create sensor flow to see the counter increase.

It’s importing now, I will take a look, don’t have experience with this kind of flow yet. Thanks !

Erwin

Hello

Could you send the node red flow to see how you did it?

thank you