AMoo-Miki / homebridge-tuya-lan

Homebridge plugin for IoT devices that use Tuya Smart's platform
MIT License
200 stars 51 forks source link

Current Power Consumption of an outlet #8

Open DSchumacher2104 opened 5 years ago

DSchumacher2104 commented 5 years ago

Hello, fist of all, thank you for creating this wunderful plugin. I'm using it with tuya outlets. The plugin would be perfect, if it would be possible to add the power consumption feature of the outlets and let HomeKit read the current power consumption. This would be great for creating automations e. g. for a washing machine. So it would be possible to determine the end of the washing process. You even can use fakegato (https://github.com/simont77/fakegato-history) to create a statistic of the power consumption.

Can the tuya-API Support this Feature?

xatrix78 commented 5 years ago

Yes, that would be great!

AMoo-Miki commented 5 years ago

Does the outlet advertise the power consumption in the Tuya's own app? If yes, can you please send me your identification signature and I will add the functionality.

xatrix78 commented 5 years ago

Yes it does. Which identification do you mean?

DSchumacher2104 commented 5 years ago

My Plugs have this signatures:

[2019-2-19 16:09:20] [TuyaLan] Ready to handle OxaOxe 2-fach 16A 1 with signature {"1":true,"2":true,"7":95,"8":48,"9":2267} [2019-2-19 16:09:20] [TuyaLan] Ready to handle gosund 16A 1 with signature {"1":true,"2":0,"4":19,"5":8,"6":2280} [2019-2-19 16:09:21] [TuyaLan] Ready to handle vodool 10A 1 with signature {"1":true,"2":0,"4":40,"5":45,"6":2280} [2019-2-19 16:09:21] [TuyaLan] Ready to handle AOFO 4-fach mit USB 1 with signature {"1":true,"2":true,"3":true,"4":false,"7":false,"101":0,"102":0,"103":0,"104":0,"105":0}

The frist device in a Smart Double-Outlet Plug, "1" and "2" are outlet states, "7" is current in mA (=95 mA), "8" is Power in W 10 (=4.8 W) and 9 in Voltage in V 10 (=226.7 V).

The second und third devices are Smart (Single-Outlet) Plug from different vendors, "1" is outlet state, "2" unknown, "4" is current in mA (=19 mA/40 mA), "5" is Power in W 10 (=0.8 W/4.5 W) and 6 in Voltage in V 10 (=228.0 V/228.0 V).

The last device is a Smart Power Strip with USB, 1-4 are outlets und 7 is USB-Port. Is has no additional functions.

I hope this helps to implement the additional information in HomeKit including the great fakegato-features.

Thanks for your great work!

PS: I also have 2 Smart Plugs with RGB-Lightrings and power measurement. I will send there signature later. Perhaps it is also possible to control the RGB-Lightrings in HomeKit?!?

DSchumacher2104 commented 5 years ago

Here is the signature for the "WiFi Smart Socket with LED Indicator 16A":

[2019-2-19 18:01:24] [TuyaLan] Ready to handle RGB-Steckdose 16A 2 with signature {"1":false,"2":"colour","3":180,"4":255,"5":"00b7ff00c5ffff","6":"00ff0000000000","7":"ffff500100ff00","8":"ffff8003ff000000ff000000ff000000000000000000","9":"ffff5001ff0000","10":"ffff0505ff000000ff00ffff00ff00ff0000ff000000","101":true,"104":0,"105":0,"106":2273}

"1" is power state of the RGB Indicator, "2-"10" are additional light parameters (e. g. brightness, Colour, hue …), i think. "101" is outlet power state, "104" is current in mA (=0 mA), "105" is Power in W 10 (=0.0 W) and "106" in Voltage in V 10 (=227.3 V).

The settings/states of the LED light are independent from outlet power state

AMoo-Miki commented 5 years ago

Thanks for the details. On it!

Since I am shooting in the dark here, would you be able to help test the changes? If yes, update the plugin with npm i -g AMoo-Miki/homebridge-tuya-lan and modify the config for one of the single outlets to have these extra params:

{
    "voltsId": 9,
    "ampsId": 8,
    "wattsId": 7
}

You might not need to clear your cachedAccessories file but if you see the new parameters not showing up, please do. If this works, I will handle all the others and add some bells and whistles to it.

DSchumacher2104 commented 5 years ago

Thank you, i'll try it later and give a report!

DSchumacher2104 commented 5 years ago

Great news!!! I have tried it and it works except the Ampere characteristic. 9753d29b-3aec-4d87-8225-eb3370f0721c And i had to swap the wattsId and ampsId, but thats no deal. I hope you can fix the ampsId, then it should be perfect.

DSchumacher2104 commented 5 years ago

And i used the RGBTLight-Setting for my Smart-Plug with lights and HomeKit exposes the light and i could switch the light and change the colour. However, the colour temperature feature doesn't work cause it is not supported by the light. It is only RGB and not RGBT. And i can't add the switch for the outlet, cause it uses Id 101.

DSchumacher2104 commented 5 years ago

And it would be great if you can add the history feature for power consumption (total consumption) too, like here:

https://github.com/simont77/fakegato-history/issues/55

AMoo-Miki commented 5 years ago

I think the Amps is not showing up because it is 95mA; can you please put some more load on it and see if it shows anything? I have a feeling that it is not rounding 0.095 to 0.01 and is instead just chopping the extra digits. I made the resolution for Amps as 0.001 which is probably too much for the app. If all is the same to you, I will round up and limit it to A instead of mA.

I plan on defining a new RGBOutlet. It will show up as two different devices in the app.

I will add all those bells and whistles for the energy as soon as we make sure all is well.

DSchumacher2104 commented 5 years ago

No, to avoid this i put a lot power on the outlet as you see on the picture -> nearly 500 W, so roud about 2,1 A (2100 mA). There has to be another problem. I don't know.

DSchumacher2104 commented 5 years ago

I tried it again today, now everything works!!! f5fbae12-1652-4bc0-b80c-cc4db1a45f5f This is really a great feature, thank you very much!

Is there a way to add the second outlet of my double-plug without loosing the power consumption feature?

DSchumacher2104 commented 5 years ago

I put the power consumption feature on my other single plugs, everything works fine! So, open to-do's are:

This is a great step ahead with Tuya Smart Plugs, thanks so much!

pitleh commented 5 years ago

Hi @AMoo-Miki

I also just got the great power consumption working on my GoSund Smart Plugs ( another version of the Tuya) This is how I had to adopt my config to make it work:

"platform": "TuyaLan", "devices": [ { "name": "Ladegeraet", "manufacturer": "Tuya", "model": "Outlet", "type": "Outlet", "id": "xxxxxxxxxxxxxxx", "key": "xxxxxxxxxxxxxxx", "voltsId": 20, "ampsId": 18, "wattsId": 19 } ] },

Please if you could add the fakegato history feature @DSchumacher2104 mentioned above, that would be so cool. I am very open to test this for your of course.

Thanks

AMoo-Miki commented 5 years ago

@DSchumacher2104 and @pitleh, thanks for testing this out. I will add the energy params to all the other types next. I will also add the fakegato calculations that you have asked for. I might not be able to complete these in the next few days (due to other commitments) but I will do my best to wrap it up in 10 days.

Redlah commented 5 years ago

Hi,

as suggested by codetheweb, I tried to use this plugin (because he will no longer support the original one and won't add functions to it) . But unfortunately it doesn't works for me. :-( I'm not sure if it is really this plugin that is the problem or anything else, because the plugin homebridge-tuya plugin, also doesn't work anymore. But it stopped working since I installed this one, it think.

After many attempts to uninstall and install the plugins I decided to restore my raspberry completely and installed everything new. But with the same result. I have the IFTTT plugin installed. This works fine. So Homebridge in general or the connection should not be the problem.

The outlets are displayed in Homekit but the hardware does not react, when I switch them on and off. The following is displayed when I start Homebridge:

[2019-3-1 16:47:50] [TuyaLan] Starting discovery... [2019-3-1 16:47:50] Homebridge is running on port 51826. [TuyaAccessory] Discovery started. [2019-3-1 16:47:50] [TuyaLan] Discovered Waschmaschine (XXXXXXUUIDXXXXXXXX) [2019-3-1 16:47:50] [TuyaLan] Connected to Waschmaschine [2019-3-1 16:47:50] [TuyaLan] Ready to handle Waschmaschine with signature {"1":false,"9":0,"18":0,"19":0,"20":2257,"21":1,"22":740,"23":32062,"24":21688,"25":960} [2019-3-1 16:47:51] [TuyaLan] Discovered Trockner (XXXXXXUUIDXXXXXXXX) [TuyaAccessory] Discovery ended. [2019-3-1 16:47:51] [TuyaLan] Connected to Trockner [2019-3-1 16:47:51] [TuyaLan] Ready to handle Trockner with signature {"1":true,"9":0,"18":0,"19":0,"20":2263,"21":1,"22":750,"23":31687,"24":21242,"25":980}

When I switch to "on" in Homekit, the following is displayed: [TuyaAccessory] Sending Trockner {"1":true}

And when I switch it off the outlet (hardware button the following): ^[c[TuyaAccessory] Odd message from Trockner with command 8: { cmd: 8, data: '†ÃÉ\u0013\u001bÉ\u0014\u0006®\u0007}\u0013#[þ¶6ƒàÑ\u000f×|¿\u0005\bh1\u0017¿µöJ\fcG/ýI<\u0002Ñ/±\u001d' }

Any idea what else can I do, to get the outlets working? I searched hours of hours in the internet and installed and uninstalled everything. I'm starting to lose my mind. :-p

Thanks in advance!!

Redlah commented 5 years ago

Hi,

the power consumption is also not visible in the Eve app. img_1774

This is my config file:

{ "platform": "TuyaLan", "devices": [{ "name": "Waschmaschine", "manufacturer": "Gosund", "model": "Outlet", "type": "Outlet", "id": "XXXXXXXXXXXXXXXXXX", "key": "XXXXXXXXXXXXXXXXXX", "voltsId": 20, "ampsId": 18, "wattsId": 19 }, { "name": "Trockner", "manufacturer": "Gosund", "model": "Outlet", "type": "Outlet", "id": "XXXXXXXXXXXXXXXXXX", "key": "XXXXXXXXXXXXXXXXXX", "voltsId": 20, "ampsId": 18, "wattsId": 19 }] }

pitleh commented 5 years ago

Have you installed the plugin via this command: npm i -g AMoo-Miki/homebridge-tuya-lan

Not sure, if this will install a beta version with the additional functions. Perhaps @AMoo-Miki can let you know.

Redlah commented 5 years ago

Hi,

@pitleh ups i missed that, but when I try to use this installation command I get the following :-( image

Maybe my problem with the not responding Gosund devices is something completely different. I don't considered that I have a new router since a few days . But since everything else worked fine, I didn't think that might be the cause. :-s Also the Gosund devices are working with the original app (Smart Life).

Maybe something with the ip addresses?! On my router one of the outlet has the following IP address. But when I add it to the config.json also nothing happens. :-/ image

The strange thing is, that when Homebridge is running and I do the following, different ip addresses are displayed: image

But also no success when I use this ip addresses in the config.json. :-(

Redlah commented 5 years ago

Sorry for "spamming" in this issue/thread but I have no more idea what I can change or do to solve my problem. Don't know if I'm on a wrong way with the network thing, because if I unplug the outlet, and restart Homebridge, it recognize that the outlet isn't there and doesn't add it. And as we see in my post above, if they are plugged correctly, Homebridge says that the outlets are successfully connected. Unfortunately I haven't any other hardware which I can connect to Homebridge, to do additional tests to exclude individual components as causes. :-/

Redlah commented 5 years ago

Another strange behavior. While I was typing my last post. Homebridge logging the following.

image

DSchumacher2104 commented 5 years ago

I really have no problems using this plugin. I can control all plugs and the light of the plug with RGB LED Indicator. Perhaps it's is due to different dps-adresses. The plugin supposes that to plug uses dps 1 for power switching a single plug. My RGB LED Indicator plug uses dps=101 for power switching so i cannot control it with this plugin. Perhaps it would be the best way if @AMoo-Miki create a free configurable devicetyp for those exotic devices where users can self-assign the dps-services to plugin functions like this for the LED Plug:

{ "name": "Ladegeraet", "manufacturer": "Tuya", "model": "LED Plug", "type": "Custom", "id": "xxxxxxxxxxxxxxx", "key": "xxxxxxxxxxxxxxx", "RGB": 1, "plug": 101, "voltsId": 106, "ampsId": 104, "wattsId": 105 }

twistedindustries commented 5 years ago

@Redlah

Another strange behavior. While I was typing my last post. Homebridge logging the following.

image

This happened to me and I had to remove the device from the tuya app and re add it. This will generate a new key. I also deleted /persist and /accessories and re-added the homebridge accessory with a new pin and username just in case.

xatrix78 commented 5 years ago

I made a complete reinstall of homebridge and it seems like the outlets are working now. @Redlah I think you should test this to. The only thing is that I have switch all outlets of and on to see the current state. Is there no automation for the state?

When I add my bulb and switch it on and off I get a crash: Mär 05 10:31:59 hbridge2 homebridge[5189]: /usr/local/lib/node_modules/homebridge-tuya-lan/lib/BaseAccessory.js:135 Mär 05 10:31:59 hbridge2 homebridge[5189]: const [, h, s, b] = value.match(/^.{6}([0-9a-f]{4})([0-9a-f]{2})([0-9a-f]{2})$/i) || [0, 255, 255]; Mär 05 10:31:59 hbridge2 homebridge[5189]: ^ Mär 05 10:31:59 hbridge2 homebridge[5189]: TypeError: Cannot read property 'match' of undefined Mär 05 10:31:59 hbridge2 homebridge[5189]: at RGBTWLightAccessory.convertColorFromTuyaToHomeKit (/usr/local/lib/node_modules/homebridge-tuya-lan/lib/BaseAccessory.js:135:35) Mär 05 10:31:59 hbridge2 homebridge[5189]: at RGBTWLightAccessory._registerCharacteristics (/usr/local/lib/node_modules/homebridge-tuya-lan/lib/RGBTWLightAccessory.js:31:106) Mär 05 10:31:59 hbridge2 homebridge[5189]: at TuyaAccessory.BaseAccessory.device.once (/usr/local/lib/node_modules/homebridge-tuya-lan/lib/BaseAccessory.js:22:18) Mär 05 10:31:59 hbridge2 homebridge[5189]: at Object.onceWrapper (events.js:317:30) Mär 05 10:31:59 hbridge2 homebridge[5189]: at emitTwo (events.js:126:13) Mär 05 10:31:59 hbridge2 homebridge[5189]: at TuyaAccessory.emit (events.js:214:7) Mär 05 10:31:59 hbridge2 homebridge[5189]: at TuyaAccessory._change (/usr/local/lib/node_modules/homebridge-tuya-lan/lib/TuyaAccessory.js:240:18) Mär 05 10:31:59 hbridge2 homebridge[5189]: at TuyaAccessory._msgHandler (/usr/local/lib/node_modules/homebridge-tuya-lan/lib/TuyaAccessory.js:166:30) Mär 05 10:31:59 hbridge2 homebridge[5189]: at /usr/local/lib/node_modules/homebridge-tuya-lan/node_modules/async/dist/async.js:4096:9 Mär 05 10:31:59 hbridge2 homebridge[5189]: at Object.process (/usr/local/lib/node_modules/homebridge-tuya-lan/node_modules/async/dist/async.js:2344:17) Mär 05 10:31:59 hbridge2 systemd[1]: homebridge.service: Main process exited, code=exited, status=1/FAILURE Mär 05 10:31:59 hbridge2 systemd[1]: homebridge.service: Unit entered failed state. Mär 05 10:31:59 hbridge2 systemd[1]: homebridge.service: Failed with result 'exit-code'. Mär 05 10:32:09 hbridge2 systemd[1]: homebridge.service: Service hold-off time over, scheduling restart. Mär 05 10:32:09 hbridge2 systemd[1]: Stopped Node.js HomeKit Server. Mär 05 10:32:09 hbridge2 systemd[1]: Started Node.js HomeKit Server.

My config: { "name": "Kuechenlampe", "type": "RGBTWLight", "manufacturer": "Tuya", "model": "RGBTWLight", "id": "xx", "key": "xx", "ip": "192.168.69.120" }

Redlah commented 5 years ago

Hey,

@twistedindustries Thank you so much, that was the problem. I had already started to read the key etc. again, after I switched my router. But as Homebridge display "Device connected" in the log. I stopped and continued with the old ones. Now my outlets react again on input form the apps.

But: The new options for the power consumption are not displayed. If I try the plugin via: sudo npm i -g AMoo-Miki/homebridge-tuya-lan

I get the following error:

image

@AMoo-Miki Do we have to use the command with your name in front to get these power consumption characteristics?

DSchumacher2104 commented 5 years ago

Perhaps you should try to install as root, so run 'sudo su -' before installing

Redlah commented 5 years ago

Perhaps you should try to install as root, so run 'sudo su -' before installing

wuhuuu, you are right!!!! Thank you!

pitleh commented 5 years ago

When I did it this way it worked for me:

sudo su - npm i -g AMoo-Miki/homebridge-tuya-lan

ghost commented 5 years ago

Hi,

I just played a little bit with and now I see these information:

Mär 10 16:59:53 homeserver homebridge[5949]: [2019-3-10 16:59:53] [TuyaLan] Ready to handle TV with signature {"1":true,"2":0,"4":207,"5":363,"6":2297} Mär 10 16:59:53 homeserver homebridge[5949]: [2019-3-10 16:59:53] [TuyaLan] Ready to handle Wasserkocher with signature {"1":true,"2":0,"4":10,"5":5,"6":2315} Mär 10 16:59:53 homeserver homebridge[5949]: [2019-3-10 16:59:53] [TuyaLan] Ready to handle Lautsprecher with signature {"1":true,"2":0,"4":0,"5":0,"6":2308} Mär 10 16:59:53 homeserver homebridge[5949]: [2019-3-10 16:59:53] [TuyaLan] Ready to handle Netatmo with signature {"1":true,"2":0,"4":0,"5":0,"6":2314}

Whats really weird is, that on ALL plugs are devices connected and on. But "Netatmo" and "Lautsprecher" does not indicate any consumption... On Netatmo, of course, I had my Netatmo Weatherstation connected and it is ON. On Lautsprecher, I have some small PC speaker connected for AirPlay over the PI. I can not believe, that these two devices to not consume anything... But okay...

Is the order for mA, W, V everytime the same? So on my case:

{ "voltsId": 6, "ampsId": 4, "wattsId": 5 } ??

Thanks in advance for any help!

EDIT: Okay, got it, I added these values and now I have the additional information visible in HomeKit. But, my Outlet for my TV (Tv and Sky Box connected) shows me only 0,2A... So something is not correct here, haha.

xatrix78 commented 5 years ago

@misanthrophy I have the same problem. Sometimes its working sometimes not.

AMoo-Miki commented 5 years ago

I have been pretty stomped with work which is almost over. I will be back this weekend to add the power consumption and all.

DSchumacher2104 commented 5 years ago

@AMoo-Miki Do you think it would be possible to create an auto-recognition for the correct power consumption Id's? Assuming, a plug is connected to power and switched on the "voltId" has to be around 2200-2500 in EU, so it should be possible, to find this Id with a json query by Plugin-automatic. And the ampsId and wattId always seem to be voltId-2 (=ampsId) and voltId-1 (=wattsId). So, if the devicetype indicates, that the device provides power measuring, the Id's can be easily found by Plugin, if the plug is switched on!

AMoo-Miki commented 5 years ago

@Redlah, since your are getting Ready to use, we know that your id is valid. Since sending a command is not working, there are two possibilities: (1) the key value is not correct, or (2) the encryption/decryption algorithm used by this device is not standard.

I would recommend you first remove the device from the Tuya app and then try to extract the id and key from scratch using the Setup Instructions.

Looks like you did this and it solved your problem.

AMoo-Miki commented 5 years ago

@AMoo-Miki Do you think it would be possible to create an auto-recognition for the correct power consumption Id's? Assuming, a plug is connected to power and switched on the "voltId" has to be around 2200-2500 in EU, so it should be possible, to find this Id with a json query by Plugin-automatic. And the ampsId and wattId always seem to be voltId-2 (=ampsId) and voltId-1 (=wattsId). So, if the devicetype indicates, that the device provides power measuring, the Id's can be easily found by Plugin, if the plug is switched on!

@DSchumacher2104 , I am sure the logic can be implemented but with all the different values and parameter configs, I would expect to see way too many false recognition to make the automation worth doing.

Redlah commented 5 years ago

Looks like the plugin is not running stable. :-/ I'm gonna post a screenshot of homebridge's log before I reboot it next time.

DSchumacher2104 commented 5 years ago

@AMoo-Miki Could you find any time to add the missing fakegato features, support for Double Smart Plug with power comsumption feature and RGB-Smart-Plug with Power Consumption?

Redlah commented 5 years ago

Hey,

I still get this error after a while (hours/days) that crashes my home bridge. :-( @twistedindustries do it never happened to you again?

image

DurrM commented 5 years ago

@AMoo-Miki first of all I want to thank you for your plugin. It works just as it's supposed to do. But is there any way you could implement the fakegato feature. It would be the cherry on the cake. Regards

AMoo-Miki commented 5 years ago

@DurrM and @DSchumacher2104, I promise I will. With the new API almost solved and my day job almost slown down, I will have a bit more time to spend here.

@Redlah I pushed a change (stable release v1.4.0) a few days ago that should slow down the retries. A better option would be the latest rc release; get it by doing npm i -g homebridge-tuya-lan@rc. It includes logic to understand this newer API.

A known issue is that the current state is not reported by some devices; they however work fine as soon as you interact with them. I have reached out to Tuya for a solution.

The Setup Instructions have changed in case you need to obtain a fresh id and key.

DSchumacher2104 commented 4 years ago

@AMoo-Miki This are really good and great news! Thank you so much!!!

DSchumacher2104 commented 4 years ago

I tried your npm-rc version and nearly everything worked fine, execpt that i cannot add my double-outlet-plug with power measurement with signature {"1":true,"2":true,"7":95,"8":48,"9":2267} as an MultiOutlet or CustomMultiOutlet using:

            {
              "name": "OxaOxe 2-fach 16A 1",
              "type": "CustomMultiOutlet",
              "manufactor": "OxaOxe",
              "model": "OxaOxe 2-fach 16A",
              "id": "X",
              "key": "X",
                          "outlets":
                           [
                            {
                             "name": "OxaOxe 2-fach 16A 1 Steckdose 1",
                             "dp": 1
                            },
                           {
                            "name": "OxaOxe 2-fach 16A 1 Steckdose 2",
                            "dp": 2
                            }
               ],
                      "ampsId": 7,
              "wattsId": 8,
              "voltsId": 9
            },

or

            {
              "name": "OxaOxe 2-fach 16A 1",
              "type": "MultiOutlet",
              "manufactor": "OxaOxe",
              "model": "OxaOxe 2-fach 16A",
              "id": "X",
              "key": "X",
                          "outletCount": 2,
                  "ampsId": 7,
              "wattsId": 8,
              "voltsId": 9
            },

Adding one of this in config.json makes the plugin chrashing! Is it possible to add that feature too?

AMoo-Miki commented 4 years ago

@DSchumacher2104, I haven't added the energy characteristics to those two types yet. I will as soon as I wrap up some work on another project.

DSchumacher2104 commented 4 years ago

@AMoo-Miki Okay, thank you! BTW, is it possible to add the total consumption/coast feature in EVE-Characteristics?

muratcorlu commented 4 years ago

@AMoo-Miki Can you share status for this issue? Or maybe listing missing part. Maybe I can help to add missing parts. I'm especially interested in to see consumption history in Eve Home app.

BassXT commented 2 years ago

@AMoo-Miki Is there any new progress concerning the powermeasurementfeature with multioutlets?

goergiosdaniil commented 1 year ago

Exactly what I need. Do we have any news? Ready to handle My Power Strip (CustomMultiOutlet:3.3) with signature {"1":true,"2":true,"3":true,"7":false,"9":0,"10":0,"11":0,"15":0,"18":401,"19":881,"20":2194,"101":false,"102":false}

This is my signature. 1,2,3 are the outlets, 7 is the usb, 18 is the ampsid, 19 is the wattsid and 20 is voltsid. So we have a sum of every outlet. how can we display that in home bridge