node-red-contrib-tuya-local icon indicating copy to clipboard operation
node-red-contrib-tuya-local copied to clipboard

Not fetching total energy from plug

Open polyphia opened this issue 5 years ago • 20 comments

Hi, thanks for this component it works flawlessly for fetching my plugs instant readings .

This is probably not a real issue (api limitation ?) but I noticed it's getting all the values in the app except total KW.h (accumulated energy consumption) .

image

18 is current (it lacks the .), 19 is Power(W) (also lacks a . before last digit) and 20 is Voltage (also lacks a . before last digit) . All these seem to get fetched correctly (except for the lack of the decimal separator) except for the last variable in the tuya app (total KW.h) .

Any chance for this to get updated or is it an API limitation ?

Thanks

polyphia avatar Dec 10 '19 17:12 polyphia

Not an api limitation, is just what the device returns. I think the energy number is fetched from the cloud tuya service, but not a 100% sure. I have to look through tuyapi issues to confirm that. Regarding the the dot is the same is what device returns, you should be able to round it or convert to whatever power unit you want in nodered or home assistant.

subzero79 avatar Dec 10 '19 20:12 subzero79

There was a previous tuya node (node-red-contrib-tuya-smart) that used to get the total daily power consumption from the tuya api . It currently does not work as it needs to be updated for the changes in 3.3 but since it was using tuyapi it should be possible ? Perhaps the only value in the cloud is the total overall power consumption ?

polyphia avatar Dec 10 '19 21:12 polyphia

if the device itself is returning a the dps index with overall daily energy it would come in the output. The device I used to develop this was one with power meter and I only got voltage, current and power. I perform the energy calculations in hass using integration sensor and utility meter. In the code I don’t filter any results unless is specified in the node settings.

subzero79 avatar Dec 10 '19 21:12 subzero79

I think maybe in the old tuya firmware the device outputted the energy calculations in a dps index. I don’t have an old fw device to check that.

subzero79 avatar Dec 10 '19 22:12 subzero79

Hum, perhaps it's omitted in the new firmware version then . Can I know how you are transforming each dps value from the index in a different sensor ? That would be very helpful in helping me to integrate it with HA .

Thanks !

polyphia avatar Dec 11 '19 10:12 polyphia

I have to clarify that i don't use this node anymore since tuya convert managed to crack again the firmware OTA. Out of the three medion(ALDI) plugs i have, two of them i flash them with esphome, the third one still remains with tuya but not using it. So, i still have the old flow but disabled, so in one of my functions node i have this

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;

So voltage and power divided by 10, and current stays in mA, guess if you want amps then divided by 1000

subzero79 avatar Dec 11 '19 10:12 subzero79

Thanks buddy, that was really helpful of you, appreciated ! :)

Best of luck for the long journey down the road of automation !

polyphia avatar Dec 11 '19 11:12 polyphia

Sorry , just 1 more question !

The way you have the function node setup sends only the status to HA, but wouldn't you need the sensors to be separated (sensor.voltage, sensor.watts, sensor.current) for HA and other integrations like utility meter ?

I added this topic to my HA :

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

This ofcourse gets me the status of all parameters properly :

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

How are you getting the sensors for each of these ? Adding a template sensor for each seems overly long and complicated considering I have around 12 plugs .

Thanks again!

polyphia avatar Dec 11 '19 21:12 polyphia

Yes I was sending the power as attribute then extracting the sensor as template. But yes I guess is more efficient to send it to a single topic or input text and create a sensor from there.

subzero79 avatar Dec 11 '19 21:12 subzero79

Also the nodered hass plugin developer has just released a custom component to integrate these things easier into home assistant

https://github.com/zachowj/hass-node-red

subzero79 avatar Dec 12 '19 02:12 subzero79

Also the nodered hass plugin developer has just released a custom component to integrate these things easier into home assistant

https://github.com/zachowj/hass-node-red

Hehe I actually came across that a few hours ago :)

image

Just haven't figured how to merge all the plugs in a single flow (like in your example using mqtt) using the 3 sensor templates (power, voltage, current), my node red knowledge is very basic .

ALready saved me tons of work though since I would need to add dozens of sensor templates otherwise .

polyphia avatar Dec 12 '19 04:12 polyphia

I think the problem is in the node I don’t send the node name. So I kept an array of objects where I had names associated with the keys and ids. Then matching the node returned id with a name. That could definitely be improved.

subzero79 avatar Dec 12 '19 07:12 subzero79

It's been working brilliantly so far, had no other options for my teckin SP22 plugs as they are not flashable even with tuya local 2.0(realtek chip) , this saved my life ! :D

Just one last question, any way to add a switch which I can turn on and off ? I have 2 switches which I am unable to integrate directly using HA so I need to control these from Node Red .

polyphia avatar Jan 04 '20 16:01 polyphia

What do you mean to have a toggle button for the switch in nodered? If that’s the case you can use the node red dashboard.

subzero79 avatar Jan 04 '20 21:01 subzero79

No, I meant switching the power on and off on the switch . I ended up using the set false and set true dps nodes together with a switch entity and it's working great !

The only thing left is reading the current state of my plug so I can update my input_boolean to on or off accordingly in case instead of my HA switch I manually power off / on my plug .

How can I get a reading from the plug current state ?

Thanks

polyphia avatar Jan 04 '20 22:01 polyphia

If you send a payload with string request it should return the current state. Also any changes when the physical button is pressed should come out from the node.

subzero79 avatar Jan 04 '20 23:01 subzero79

Thanks got it working now, msg.payload.dps.1 was what I was looking for .

Apparently there can only be 1 tcp connection at a time to the tuya api so the tuya HA integration wasn't really working out for me , as I already had the TCP connection open in Node Red for the power / voltage / current readings.

Created some switches using input_booleans and now I can get them to show the power status of my plugs either by using the switch or toggling the power button directly on the plugs .

This is hands down the best working solution if one is stuck with tuya original firmware as me, since my SP22 Tecking plugs have realtek chip instead of ESP and can't be flashed in any way !

polyphia avatar Jan 05 '20 02:01 polyphia

I have similar issue and I have to integrate 11 sockets. After not sleeping 2 nights, I managed to get the status of the socket in HomeAssistant. Unfortunatelly in single entity, so I can not use them for any automation rule (in NodeRed).

TriptonPt could you please explain more in detail what you did or share the whole NR code and configuration.yaml part. I have even problem creating switch...

Monacoslo avatar Feb 19 '20 21:02 Monacoslo

configure all sockets in node red use inject and debug nodes in node red to get your desired control if that works, replace the inject and debug with whatever communication protocol you want node -red dashboard mqtt msgs or the nodered hass plugin

break down your problem in smaller parts and build from there first get control over your plugs with node-red only

bogeuh avatar Feb 21 '20 22:02 bogeuh

https://github.com/subzero79/node-red-contrib-tuya-local/issues/18#issuecomment-564841076

@polyphia , can you show me your function-nodes?

MrThiemann avatar Nov 04 '22 05:11 MrThiemann