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/" + msg.data.name + "/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 :
sensor.voltage
sensor.power
sensor.current
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 !