Configuring energy meter with W data coming from node-red + integration

hi, I have just started with home assistant and I git crazy to push W reading data from a house energy meter from node-red to the home assistant energy.
at the same time, I got fun so I am here to contribute to someone else who could have the same problems as mine,

so let’s start:
my home W power usage comes from a clamp reader, long story short, such a meter sends data using 433 Mhz, I get them with rtl433, I push them to mqtt moschito, I than work them using node-red.
I know it is a long way, but I have learnt a lot…
anyway, first thing first you have to install the module: node-red-contrib-home-assistant-websocket in nodered.

then get the data and push them in the entity entry node.

i give you and example here:

[{"id":"920d2bee83b7e4ce","type":"tab","label":"HA-test","disabled":false,"info":"","env":[]},{"id":"29405a733b05159f","type":"inject","z":"920d2bee83b7e4ce","name":"consumo","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"consumption\":900}","payloadType":"json","x":340,"y":260,"wires":[["b6a44a9d60cf6303"]]},{"id":"b6a44a9d60cf6303","type":"ha-entity","z":"920d2bee83b7e4ce","name":"consumption","server":"4e1cf6bcc4833615","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"consumo"},{"property":"device_class","value":"power"},{"property":"icon","value":"mdi:counter"},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":"measurement"},{"property":"last_reset","value":"1970-01-01T00:00:00+00:00"}],"state":"payload.consumption","stateType":"msg","attributes":[{"property":"state_class","value":"measurement","valueType":"str"},{"property":"last_reset","value":"1970-01-01T00:00:00+00:00","valueType":"str"},{"property":"unit_of_measurement","value":"W","valueType":"str"},{"property":"device_class","value":"power","valueType":"str"},{"property":"icon","value":"mdi:counter","valueType":"str"},{"property":"friendly_name","value":"consumo","valueType":"str"}],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":630,"y":260,"wires":[["5873a1ddd4c2d71b"]]},{"id":"5873a1ddd4c2d71b","type":"debug","z":"920d2bee83b7e4ce","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":260,"wires":[]},{"id":"41c689f6045b885e","type":"link in","z":"920d2bee83b7e4ce","name":"","links":["8ed60d429e2c3f4d"],"x":305,"y":180,"wires":[["a57f6d72d337a864"]]},{"id":"a57f6d72d337a864","type":"function","z":"920d2bee83b7e4ce","name":"","func":"if (msg.payload.id==2926){\n    pot=parseInt(msg.payload.power0_W);\n    msg.payload={\"consumption\":pot};\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":180,"wires":[["b6a44a9d60cf6303"]]},{"id":"4e1cf6bcc4833615","type":"server","name":"Home Assistant","version":2,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

at the same time on home assistant you have to:

  1. install HACS (goggle for it)
  2. go to HACS integrations and install ** Node-RED Companion**

at this point you are pushing your data from node-red to home assistant, but you are pushing “W” data for Energy you need Wh or kWh.

for that you need to interate them, but not only, you have to define a unique_id otherwise you gat an error, for doing it you have just to add aat the bottom of the configuration.yaml file the following lines:

sensor:
  - platform: template
    sensors:          
      grid_consumption_w:
        friendly_name: "Grid Consumption (W)"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.consumption') | float |  round(4) }}"
        unique_id: sensor.pippo
    
   
  - platform: integration
    name: energy_grid_consumption
    source: sensor.grid_consumption_w
    unit_prefix: k
    unit_time: h

now you can select the energy_grid_consumption in the energy panel.

OK, I hope this can be useful, or at least I can reuse it in case my home assistant crashes.

have fun and please correct me if I wrote something wrong or if something changes in next versions.

cheers from Italy, beppe