New in node red and Home Assistant. I need help with a flow

Hi,
I am trying to deploy a node-red project to my home assistant and I am struggled so I come here in search of help. I am pretty new to both worlds.
I have a solar inverter which generates electricity.
Target:

  • 4 entities in HA:
    1. Total solar power generated by the plant.
    2. Amount of power given to the grid. If power is being taken from grid, this value should be zero.
    3. Amount of power taken from the grid. If power is being given to the grid, this value should be zero.
    4. Total amount of power used by house. It will be Total Solar power + Amount of power taken when taking, and Total Solar power - Amount of power giving when giving.

In node red I have:

  • Modbus reading from inverter which gives the total power generated in msg.payload.ACpower
  • Another Modbus reading from inverter which gives the power being taken or given to the grid in msg.payload.GivenPower. When it is taking, value is negative, and when it is giving, it is positive.
  • One buffer connected to each, to extract the data.
    I cannot use same Modbus for both readings, no matter the reason.
    I don’t want negative numbers in HA.

How should I acomplish this?
I have done the following:

Total power generated is straight forward: just pass msg.payload.ACPower to the created HA entity directrly from the first modbus buffer.

The difficult part is the fact that I have same value for GivenPower. I have managed to make a condition, if it is negative, a function called importing changes the sign to msg.payload.GivenPower and pass it to entity Importing, and if it is positive just pass the msg.payload.GivenPower to entity Exporting. But that approach has non wanted effects: when it is importing, the exporting sensor shows as Unknown or with latest known value, and the same when it is exporting with Importing sensor. They should be zero when the other is active, and I don’t know how to manage that.

Any help or idea on different approach whould be appreciated.

I would load unmodified modbus data into HA so you have a history of what was retrieved. Then I’d have NR send MQTT messages for given- and taken-power. Always send 2 MQTT messages (with different topics) - if value is positive, send given=value and taken=0, if value is negative, send given=0, taken=-value. Then 2 MQTT sensors in HA to pick up the data for each topic. Also set retain on the MQTT messages so you have the last data if HA is restarted. But I like MQTT.

Well, thanks for the answer. I have finally managed to work it our as follows (I don’t use mqtt and I don’t even know about it, too complicated for me):

This is the solution so far:

As said, one modbus for each read, and then a buffer for each to extract the needed data.
Then, one join with both output so that I create a merged object with both payloads.
Then a function with this code:

if (msg.payload.GivenPower < 0 ) *if I am taking energy from grid
  {
    msg.payload.importando = Math.abs(msg.payload.GivenPower) *create a new element called importando with the read converted to positive
    msg.payload.exportando = 0 *create a new element called exportando and set it to 0. Now, I have two elements in payload, one with the amount of energy taking from grid and other, the one with the amount of energy given to the grid set to 0
    msg.payload.consumototal = msg.payload.AC_Power + Math.abs(msg.payload.GivenPower) *new element called consumototal which sums the amount being taken from grid plus amount being given by the panels to give the total energy consumed by house.
  }
else
  {
    msg.payload.importando = 0
    msg.payload.exportando = msg.payload.GivenPower
    msg.payload.consumototal = msg.payload.AC_Power - Math.abs(msg.payload.GivenPower)
*same elements than before, but as this time I am exporting energy to grid, importando is 0, and maths for total consumed energy changes
  }
return msg;

Now, I have a payload which gives:
AC_Power: Total energy being generated.
importando: energy being taken from grid (0 if it is being given)
exportando: energy being given to the grid (0 if it is being taken)
consumototal: energy being consumed by house

Then I create an entity for each, and take the corresponding payload.element for each.

It has been a headache, but I have finally achieved.