Read a value from node red and use it on home assistant

Hi, I would like to read some Node-red’s values in Home Assistant. These are the values ​​of my photovoltaic system. On NR everything is OK but I have no idea how to get them to HA. You can help me in detail. I have recently been using HA and I need a step-by-step help. Thank you.

you can use an entity node and pass that info as a state or attribute of a sensor.

1 Like

can you please give me an example?

An example…

If you are running Node-RED as the add-on in HA, then Node-RED will already come with the node-red-contrib-home-assistant-websocket nodes in the pallet, and with the necessary server for the websocket connection between Node-RED and HA already set up.

You need to add one of the new HA sensor nodes. The ‘entity’ nodes have been removed in a recent update, and the ‘sensor’ node now does the work of creating a new sensor in HA, and then updating this sensor.

The sensor node usually takes the value you want to move to HA in the msg.payload. Here is my Node-RED flow for my inverter data from Modbus. You can see the home assistant entities and the sensor node in the node selection on the left.

I have many sensor nodes, one each for each value I want to pass to HA.
The sensor nodes take the msg.payload value (which needs to be a string, or an integer / float number and not an object or array). The sensor node only really needs a name (which is what will show on the node in the flow).

Next, the sensor node needs an ‘entity config’ node to be set up. This is the important bit that sets up the connection between the sensor node in the flow, and HA.

You set this up by selecting ‘add new HA entity config’ in the sensor ‘entity config’ box - this opens a new ha-entity-config node editor, which needs to be set up carefully.

The name should be something related to the ‘sensor node’ so you can find this again.
The ‘server’ must be Home Assistant - and this should be automatically filled in if you are using Node-RED as an add-on.
The device is optional.
The type is ‘sensor’

The friendly name must be filled in - this is the name by which the sensor will appear in HA. It does not have to be the same as the sensor node name, but do ensure that you fill this in!

The icon is optional. If not set, a default icon will be used in HA.
The device class is optional but useful as this allows HA to display the sensor correctly. For solar power, for example, setting ‘power’ will mean that the power sensor can be used in a Reimann sum integration to get energy.
The unit of measurement should be set for the same reasons.
The state class is almost always ‘measurement’.
Last reset should not be used.

If your server connection to HA is working, and
if you have set up the sensor configuration node correctly,
then on flow deployment the sensor node status will show ‘registered’. HA now has the new sensor!

If you then pass a value to the sensor node in msg.payload, the node status will show the value and the update time, and the value will be updated in the sensor in HA.

Here is the sensor in HA.


Thanks for the effort on the above, this helped me a lot.

Another approach is to get NR to update a pre-existing sensor in HA. Do you know how to do this?

Any preexisting entity (including sensors) in HA is there because an integration registered (created) it, and that integration is responsible for updating the state value.
Manually updating the state from another place (eg Node-RED) is not good practice, and generally not supported.

HA exposes all services via API, and these API calls can be accessed via the websocket API node. The POST api/states/ seems a good choice. You can probably do this directly using an http node as long as you have the bearer code authorisation setup correctly.

Hi and thanks to all

Mi problem is that I need to update the value of sensor, it’s a temperature sensor, but it’s passed without comma “225” and I need to divide by 10 to get the actual “22.5”.

Is there a method to do it on the same sensor node or is it only possible with a function node?

Very easy to do (when you know how).


  • the sensor node is used to set the associated entity (sensor) state value from Node-RED. This is the value obtained from the ‘State’ field (usually msg.payload)
  • the attribute key section allows you to set additional (optional) attributes as well
  • the ‘output properties’ I have never used, but are for setting the outcome of the node, which is after it has done its job of setting the HA entity state

Assuming you have msg.payload incoming to the node, and that this has a value 225, you can perform operations on this value as part of setting the state value.

You can’t do operations in the basic ‘msg’, but if you select J: expression in the state option list, then you can use a JSONata expression, the result of which is used as the value to set the state.

JSONata performs expressions on the message, hence


is all you need to take the value in payload and divide by 10.

In my example above I have a more complex JSONata expression, but the principle is the same. This saves using a function node (or a change node with the JSONata expression in it).

WARNING if your payload holds a number then the above will work, however if your payload is actually a string, then it needs to be turned into a number first.


would probably do the job (as long as the string is just a number and does not have other characters in it)

I’ve tried JSONata before but I’ve had an error, as you said it’s a string and turning it into a number, it’s working fine now.

Thank you very much.

To be correct, it is JSONata, which is a programming ‘language’ used to manipulate JSON structures.

I now use it a lot - it is ‘different’ being a declarative functional language, but it can be used in many places in Node-RED to great effect.

To learn JSONata go to

There is a great exerciser that I use all the time to build JSONata expressions.

It is possible to save any exerciser state, so just for you, check out the following link!

Thanks again.

I’ve edited my previous post and I will learn a lot with your hyperlinks :+1:

I was wondering, is there more elegant solution when I have all the data in JSON in Node Red?
I have 20 devices, each with around 10 values.
I can live with probably 3 to 4 values per device only, still we are talking about 50 to 100 sensors to be created. Is there any way to pass the whole JSON and parse it in HA?

Ah now there is a question and a half.

Yes, you can pass massive blocks of JSON from Node-RED to Home Assistant. That is quite straightforward. The problem becomes one of how you deal with JSON structures from inside HA.

The ‘designed approach’ for using HA is to have one sensor with one value in the state for each bit of data required. Since state values can only be primitives, we have numbers, Boolean, and strings (to 255 characters max). This is great, but when we have arrays and dictionary objects, usually as JSON when in Node-RED, getting this into Home Assistant requires the use of attributes.

At the moment, we can store anything we like into any number of sensor attributes, and this is certainly the way I send arrays and complex values as objects to HA.

I have Node-RED flows that generate, for example, solar forecasts. In simple terms, I have “total energy tomorrow” as a number, and this goes into a sensor state value. Along side that, I have an array of hourly periods, each with an object holding time, forecast, shading flag etc.

With an array of just 16 hourly forecast values, do we create 16 sensors, one for each hour, or send one sensor (the state value being the day total) with an attribute holding the array of hourly values?

The HA development team would, I guess, officially suggest 16 separate sensors, however how do you pick out of 16 entities the ‘highest value for today’?

As you question, in general it is not as easy to work with attributes, and with arrays and objects in HA. Jinja can do processing, but even the official documentation suggests that too much logic in a template is not a good idea.

For myself, I usually try and cram as much into attributes as possible, and aim for a smaller number of entities.

I start by working out what I want to use the data for in HA, and then do as much pre-processing in Node-RED as possible, and present the data ‘use-ready’. In some cases, I will have a sensor with one attribute holding an array, another attribute holding a different array, and a third attribute holding a combined array with both sets of data - simply because it is easier to send more data ready for use than less data and do complex processing in HA.

Most arrays I pass end up as tables or graphs, so usually no difficulty there.
ApexCharts allows for a bit of JS code in the data generator, so for example the following reads the attribute ‘fchours’ as an array and turns it into a time series.

  - entity: sensor.fc_table
    data_generator: |
      return, index) => {
        return [new Date(fchr).getTime(), entity.attributes.fcwatts[index]];

The Flex Table card has similar capabilities, so here the attribute ‘array’ is an array of objects, and I am reading ‘Time’ and ‘Error’, again using the JS coding in the modify option.

  include: sensor.mb_failure_details
  - data: array
    modify: (new Date(x.Time)).toLocaleString("en-GB")
    name: Time
  - data: array
    modify: |-
      let err=parseInt(x.Error.substring(5));
      let cr="f0a0a0";
      if   (x.Error>"Error4") cr="a0f0a0";
      else if (x.Error>"Error2") cr="f0f0a0";
      '<div style="background-color: #' + cr + ';">' + "E " + err + '</div>';

Attributes can be displayed directly in entity cards.
Simple template sensors can select parts of attributes, array, or object, and therefore it is relatively easy to unpack JSON into parts that way.

More complex logic is a little more challenging, but nevertheless it is possible.

I don’t know your particular use-case, but I would like to suggest that it is indeed worth experimenting with passing JSON objects via a sensor attribute and then see what you can do with it.

The limitations on attribute sizes relate to the recorder, and for objects over (I think 2.5 kb) the recorder stops adding the entity state & attributes to the database.

If you are looking to automatically create entities from json, take a look at mqtt auto discovery. Parse the data in NR, format the message structure, and send it out over mqtt. HA will automatically create an entity.

Thanks guys.
In my case, I am collecting data from solar panel optimizers. I can of course live with just the single value per panel - generated power. That is the most important one to ensure all is honky dory, but I am pulling others such as voltage, current also RSSI of the optimizers communicating with the master node.
This comes from HTTP GET which I am post-processing in NR now.

So I have 21 panels, each panel with optimizer, each optimizer providing 6 or 7 fields worth of storing.
Now I have 21 sensors and passing the power only and rest as attributes. But those attributes I can’t really work with.

From inverter, I am pulling out JSON using curl, I can’t get JSON from the optimizers, but can prepare JSON in NR. So idea was to somehow process that JSON from NR.

MQTT I can for sure try, would be easier in than creating 100+ sensors in NR.

Will give it a shot over the weekend.