Help with MQTT sensor status and Node RED

Hi guys,

Have been going around in circles trying to make some energy sensors for voltage, power (watts) and current that works by sending the MQTT payloads from Node Red to HA .

I have the following function node in Node Red :

const dps =  msg.payload.dps;
let state = null;
let power = null;
let voltage = null;
let current = null;
if ("1" in dps) {
    state = (dps['1']) ? "ON" : "OFF";

msg.payload = {
    state: state,
    attributes: {power: "0 W",current:"0 mA",voltage:"0 V"}

if ("18" in dps) {
    msg.payload.attributes.current = (dps['18']).toString() + " mA";
    if (dps['18'] > 0) {
        msg.payload.state = "ON"

if ("19" in dps) {
    msg.payload.attributes.power = (dps['19']/10).toString() + " W";
    if (dps['19'] > 0) {
        msg.payload.state = "ON"

if ("20" in dps) {
    msg.payload.attributes.voltage = (dps['20'] /10).toString() + " V";

if (msg.payload.state === null) {
    delete msg.payload.state;

msg.topic = "tuya/" + + "/status"
return msg;

And in Home Assistant I have the status topic setup this way :

- platform: mqtt
    state_topic: 'tuya/switch_1/status'
    name: switch1 status
    value_template: '{{ value_json.attributes | tojson }}'

This gives me the following output from the sensor.switch1_status :

{"current": "0 mA", "power": "0 W", "voltage": "235.9 V"}

My goal is having this in separate sensors this way :


What’s the best way to go about this ? Should I edit my functions node in order to send a different sensor payload for each parameter or should I convert these status attributes to template sensors ?

Either way, I don’t know how to achieve it for any of the options .

Can someone please shed some light on this and give an example on how to achieve what I’m looking for?

Thanks !

If you’re not set on using MQTT to get the values to HA, you might want to try the new sensor node.


[{"id":"67da67ba.913c88","type":"inject","z":"9ce184b.0f5bb78","name":"MQTT","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":816,"wires":[["2847fb.5baee806"]]},{"id":"2847fb.5baee806","type":"function","z":"9ce184b.0f5bb78","name":"","func":"const dps =  msg.payload.dps;\nlet current =  { payload: \"0 mA\"};\nlet power = { payload: \"0 W\"};\nlet voltage =  { payload: \"0 V\"};\n\n\nif (\"18\" in dps) {\n    current.payload = `${dps['18']} mA`;\n}\n\nif (\"19\" in dps) {\n    power.payload = `${dps['19']/10} W`;\n}\n\nif (\"20\" in dps) {\n    voltage.payload = `${dps['20'] /10} V`;\n}\n\nreturn [current, power, voltage];","outputs":3,"noerr":0,"x":430,"y":816,"wires":[["9e892d73.8ec07"],["b4927d03.f34b1"],["5e44d4e9.60de9c"]]},{"id":"9e892d73.8ec07","type":"ha-entity","z":"9ce184b.0f5bb78","name":"power","server":"913f8aa4.627b78","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":594,"y":768,"wires":[[]]},{"id":"b4927d03.f34b1","type":"ha-entity","z":"9ce184b.0f5bb78","name":"current","server":"913f8aa4.627b78","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"mA"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":604,"y":816,"wires":[[]]},{"id":"5e44d4e9.60de9c","type":"ha-entity","z":"9ce184b.0f5bb78","name":"volate","server":"913f8aa4.627b78","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","x":594,"y":864,"wires":[[]]}]

Thanks, will give it a try for sure !

How are these sensors added to HA ? Do they get directly integrated into HA after installing the custom component ?

Oh, just noticed it’s meant to be for using with node red running as a component of HA :neutral_face:

I am running Node Red in a separate server for saving resources in my RPi3, any way to make sensors work in a different node red ?

Node-red-contrib-home-assistant-websocket can be used on a Node-RED installation that’s not on the same machine as Home Assistant. The custom integration needs to be installed in Home Assistant.

1 Like

@Kermit thanks so much, this is such a life saver !

Absolutely amazing node, just saved me from adding 48 templates in my HA config !! :grinning: :pray: :pray:

@Kermit sorry just 1 question, isn’t there a way to set the sensor names directly in Node Red ?
I added a bunch of sensors that were updated in HA but they all are showing in this format : sensor.nodered_bfe2424c_9e347 . This makes it quite troublesome to find which sensors are each .

Here’s the tip from the release notes:

1 Like

Thanks a lot, @Kermit !!!

Today, I was fighting for hours to get a sensor created in NodeRED with MQTT visible in HA.
Your integration is fantastic!!! Much easier :slight_smile: