Help needed to pass a Node-Red value to HA

I’m trying to send a value from node red to HomeAssistant.

First I created a dummy sensor:

  - platform: template
    sensors:
      solar_power1:
        friendly_name: "Solis1"
        value_template: "{{ states('sensor.power_consumption') }}"
        unit_of_measurement: 'kW'

I have a value ready in Node-Red (scraped from a website) and want this value to be passed to this entity.
I’m new to node red and can;t figure out which node I need and how to configre this. In need some example but can;t find any with some explanation.

For some weird fact there is already a value displayed for Solis1 ??
Even when I do not have anything configured yet in Node Red

PS: sensor.power_consumption was also existing and the value template (copied from example) was refering to that. I now configured it as:

  - platform: template
    sensors:
      solar_power1:
        friendly_name: "Solis1"
        value_template: ""
        unit_of_measurement: 'kW'

But I cant find this entity from within Node Red. (Others I can find)

At first, I don’t understand why you are trying to create template sensor. Why not just sensor.

To pass value from NodeRed to any entity, you have to use Call Service node. it allows you to select any HA configured entity to send to.

Last but not least, you don’t need to create sensor in HA. Node Red is able to create needed a sensor and feed it with data. Use Entity node. If you lacking of this Entity, you need to add node-red-contrib-home-assistant-websocket extension

I think this is only the case if you have the custom component included as an integration. AFAIK this is not the standard Node Red palette that allows this without having the custom integration as well.

Either way, I agree with you that it’s pointless to create a template sensor and have it read a real sensor value if you are just wanting to send a sensor a value.

Personally, I use MQTT sensors for things like this.

Yes. It’s true. But there is no reason to not add it. Especially considering that any change in sensors managed from HA requires HA restart.

Really appreciate the help!
Why I used a template sensor was based on nothing than my inexsperience. I moved from domoticz and there it is called a virtual sensor. So I searched how to create and then the template sensor showed up.
So based on your remark I’m also able to create a “normal” sensor. Tried searching how to configure a sensor without a reference to a platform, that but still unclear.

And I also figured out that I need the additional add-on when I used an “entity” node to create an entity in HA automatically. However I wasn’t able to get this working either. I installed the extra components but always receive a debug message like:
"Node-RED custom integration needs to be installed in Home Assistant for this node to function correctly.
As I’m unsure if this has to do with the installation or my usage of the entity node and because I only need 1 value (for now) I was trying to create one manually.

This is my attempt from Node Red

I had the same issue. In the end I had node red use a MQTT node and publish to MQTT and then created a sensor in HA using mqtt. It seemed to me it was adding something else to the system but I ended up using MQTT to get my cctv camera motion detect into HA as well so all good

Sounds as overkill to add another additional component just to transfer one value to HA?
There has to be an easier way.

Most examples I find are all “binary sensors” / actions to HA. I want to trend a value over time. So send a numeric value every 5 mins.

But if I’m stuck here I will look into MQTT.

Ok, as I cant get (or don’t know how) to send a value from Node-Red to HA I installed MQTT.
It running, and I added

mqtt:
  discovery: true
  discovery_prefix: homeassistant

Next I created this flow in Node Red
(Don’t mind the error 7 below the exec “test” node, the output is OK.

The output of the func node is:

msg.payload.id : string[4]

"1040"

The idea is to put this 1040 into HA sensor called “Solis Power” (kW)
However, just this last part is not working. Probalby the last bit of config is wrong. Can somebody help me with this?

Do you understand how MQTT works?

First of all, if you want an MQTT sensor, you have to define an MQTT sensor in HA.

Secondly, if you want to pass a message to MQTT you need to format the topic correctly.

No, actually I have no idea :slight_smile: I’m just using it for the first time but after a lot of reasearch I have something working. (webscrape, data regex, ping check, etc) I tried to search for an easy example but can’t get the bit and pieces together. MQTT is also new to me but I need to learn that anyway for next projects… So, be gentle with me, I’m all pretty new to this :slight_smile:

However, I’m confused why I should create a sensor in HA first? The whole meaning using MQTT was to avoid that. My first question here was how to create a sensor that I could write to from Node Red directly. The answer was: use MQTT. So I was expecting that MQTT would create a sensor called SolisTest2 with the value defined in the topic. I was under the impression the setting mqtt - discovery:true would auto-add the sensor to HA.

But I’m still learning, please give me some advise how to create this sensor in HA. Ideally the device has an on/off setting that I could trigger based on the ping response and a value that I want to update every 5 min.

How do you expect to get the data into HA? It needs somewhere to store that data. Just enabling MQTT doesn’t do that.

Not at all, actually.

Yes. An MQTT SENSOR, like I stated I use. I didn’t say that was the ONLY way to do it either. I said that’s what I use.

OK.

Read this then. https://www.home-assistant.io/docs/mqtt/discovery/

You will need to format the initial “config” to generate the auto discovered MQTT component, in this case, a sensor.

No matter what you do, if you want to display the data in HA, you need to create a sensor. Your notion of a template sensor was flawed, because a template sensor relies on data coming from another source…usually defined within HA itself. You can have the MQTT sensor auto-created, but just enabling the discovery: true doesn’t do anything. You have to send the specialized topic and data to create the sensor.

None of this is automagical. You are required to put in some effort to accomplish things like this, and there is more than one way of dealing with it. If you are going to use MQTT, you need to understand the basics of MQTT, and not just follow some random instructions provided by someone on the forums. I refuse to just walk someone through the steps without them understanding what it is they are doing.

A potential other option do this this is to make a call to HA API from nodered. If you set a state with an API call for a non existent sensor, then, HA will create it on the spot.
That was the method I used before the “sensor” node was created by Kermit. I don’t know if what I am doing is acceptable (according the HA “rules”), but, it works.
I even do that with attributed…

GV

Thanks both of you for your replies.
I did try the HomeAssitant nodes API before but it throws a "“API call attempted without connection to server.” Stragne, because the connection is OK because I can read statusses from HA. (light status etc).

So, I’m now focussing on MQTT, as I probably need this anyway for future ideas.

Ofcourse, I know there are many many ways and I;m more than willingto put effort in :slight_smile: I’m trying to make sense of it all. So many new terms, and lot of examples takes a lot for granted.

But alright. I did some more invesitgation.

Currently I added this to the configuration.

mqtt:
  discovery: true
  discovery_prefix: homeassistant
  
sensor:
   - platform: mqtt
    state_topic: "Solis_Current_Power"
    name: "SolarPower"
    unit_of_measurement: "kW"

The MQTT broker is configured from within Node Red, The node reports “connected”.
I don’t see any issues in the log.

And updated my flow to:

It’s not working (yet) and I believe I have 2 issues:

1 > I assume that after the config change and addition of the MQTT sensor it should already appear in HA. However it does not. In the notifications screen I noticed:

Invalid config

The following integrations and platforms could not be set up:

* mqtt
* mqtt.sensor

Please check your config.

However, nothing in the log and I do not know what is wrong with the config.

2 > De debug msg output sets a valid topic but I assumed to see a value after the payload.

Your yaml is spaced incorrectly.

expand the object and you will see it. The problem is that you are trying to pass an object as the payload. Why are you trying to set msg.payload.id? The MQTT node just takes a payload. Just set payload to power value

Actually I fixed issue Nr1 because I had two brokers running…

The

mqtt:
  discovery: true
  discovery_prefix: homeassistant

Seems to enable the internal broker and I also installed mosquitto. Found out broker issues from the main HA log file.

At least I’'m now able to write dummy value to HA :slight_smile:
Unfortunately I can;t test further today because it’s dark right now and therefor the Solar convertor is shutdown…

I will use your tip tommorow. Thanks so far!

You can always test. There is an inject node for a reason.

It’s alive!
Got everything working right now. :slight_smile: Thanks all for your support.

Last issue was indeed to format the message right before sending it.
I’m now sending three different values in three different messages. Good for now, but I believe here the value_template comes in when I want to pack all related values in one message.