How do I create a Node-RED "output" that can be used in an esphome device?

I’m sorry that this must be an elementary question, but being a geriatric learner, it has stumped me!
I have a flow (my first) in Node-RED that includes a function that detects if the SoC of my battery is rising or falling.

var current = msg.payload;
var previous = context.get("previous");
if (current > previous ) {
    msg.topic = "true";
}
if (current < previous ) {
    msg.topic = "false";
}
context.set("previous", msg.payload);
return msg;

How can I use this to create a Home Assistant sensor that I can read in an esphome device. for example, the end of the work-flow would be a variant of:

text_sensor:
  - platform: homeassistant
#This is just a place holder. I want to replace sensor.time with sensor.node-red-sensor
    entity_id: sensor.time
    id: battery_soc_rising
    on_value: 
      then:
        - component.update: epaperdisplay

This is my system:

Core 2024.11.1
Supervisor 2024.11.3
Operating System 13.2
Frontend 20241106.2

Regards, Martin

To create entities from Node-RED, install the Node-RED Companion custom component. Use a sensor node to create and update the sensor in Home Assistant. Update the default state in the sensor node from msg.payload to msg.topic if that’s where your value is set.

Also, there’s a minor issue in your if statement logic: if current and previous values are equal, msg.topic never gets assigned.

I think I already have this installed.

Ah… This may be the problem? When I double-click on the Sensor node in the Node-RED editor, I am presented with an empty Properties Dialogue with no clues on how to populate.


What do I do with this…
I am guessing(?) that if Node-RED “knows” all about the input because of the connection (gray line in the editor), all I have to do(!) is define the output?
What should that look like?

Regards, M.

PS -

I don’t think that that is a problem, as the input to the function is “events: state”. Which, if I understand the docs, is only “called” on a change. So, the only changes possible are “increasing” or “decreasing”. It can’t change to the same value. Or does it? One (minor) problem, is that the first change is not noted. But I can live with that.

image

Nop! Not a clue on how to progress.
I appreciate that you are the wizard that created this magic and to you it blinding obvious but without an example, I guess I’ve come to a standstill.

Regards, M.

From what I see in the first post, it would seem that you only need a binary sensor that is either true or false depending on the battery state?

Drag out a binary sensor node and double click it. When the panel opens click the + next to entity config.

image

Another page opens add a name, friendly name and choose a device class. Don’t worry about category, leave it blank. Then click the add button on the top.

You will be back to the edit binary sensor node page. Where it says state, change payload to topic. Then click done.

image