Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not fetching total energy from plug #18

Open
polyphia opened this issue Dec 10, 2019 · 20 comments
Open

Not fetching total energy from plug #18

polyphia opened this issue Dec 10, 2019 · 20 comments

Comments

@polyphia
Copy link

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

@subzero79
Copy link
Owner

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.

@polyphia
Copy link
Author

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 ?

@subzero79
Copy link
Owner

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
Copy link
Owner

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.

@polyphia
Copy link
Author

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 !

@subzero79
Copy link
Owner

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

@polyphia
Copy link
Author

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

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

@polyphia
Copy link
Author

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!

@subzero79
Copy link
Owner

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
Copy link
Owner

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

@polyphia
Copy link
Author

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 .

@subzero79
Copy link
Owner

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.

@polyphia
Copy link
Author

polyphia commented Jan 4, 2020

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 .

@subzero79
Copy link
Owner

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.

@polyphia
Copy link
Author

polyphia commented Jan 4, 2020

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

@subzero79
Copy link
Owner

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.

@polyphia
Copy link
Author

polyphia commented Jan 5, 2020

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 !

@Monacoslo
Copy link

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...

@bogeuh
Copy link

bogeuh commented Feb 21, 2020

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

@MrThiemann
Copy link

#18 (comment)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants