rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.86k stars 549 forks source link

Setup Instructions for `SMART socket 2` #768

Open erwin opened 2 years ago

erwin commented 2 years ago

It took me a little while to get what tuya-cli reports as SMART socket 2 working with tuya-local.

In my case, when using TuyaDebug,

I got the following DPS: ``` Device at 192.168.1.217 key protocol 3.3 dev_type type_0a: DPS [1] VALUE [True] DPS [9] VALUE [0] DPS [17] VALUE [21] DPS [18] VALUE [222] DPS [19] VALUE [423] DPS [20] VALUE [2305] DPS [21] VALUE [1] DPS [22] VALUE [613] DPS [23] VALUE [34400] DPS [24] VALUE [19446] DPS [25] VALUE [2150] DPS [26] VALUE [0] DPS [38] VALUE [memory] DPS [39] VALUE [False] DPS [40] VALUE [relay] DPS [41] VALUE [False] DPS [42] VALUE [] DPS [43] VALUE [] DPS [44] VALUE [] ```

If you're unable to connect, it's usually that the IP, Device ID or Local Key isn't right. Sometimes while setting up a device, I've had to reset the Local Key multiple times to get it right. I actually like the method of just using the iot.tuya.com website and the tuya app better than tuya-cli for the SMART plug 2 because there are some stats stored in the other DPS and accessing directly via the Tuya App will make it easier to verify configuration for those in the future.

Here's the configuration that got my SMART socket 2 working:

- host: 192.168.X.Y
  device_id: '<device id>'
  local_key: '<local_key>'
  friendly_name: <Which_One> SMART socket 2
  protocol_version: "3.3"
  scan_interval: 30
  entities:
    - platform: binary_sensor
      friendly_name: <Which_One> Plug
      id: 1
      device_class: power
      state_on: "true"
      state_off: "false"
    - platform: switch
      friendly_name: <Which_One> Switch
      id: 1
    - platform: sensor
      friendly_name: <Which_One> Current
      unit_of_measurement: "mA"
      id: 18
    - platform: sensor
      friendly_name: <Which_One> Power
      unit_of_measurement: "W"
      scaling: 0.1 
      id: 19
    - platform: sensor
      friendly_name: <Which_One> Voltage
      unit_of_measurement: "V"
      scaling: 0.1
      id: 20
    - platform: sensor   
      friendly_name: <Which_One> Total
      unit_of_measurement: "kWh"
      scaling: 0.001
      id: 21
#   - platform: sensor
#     friendly_name: DP9
#     id: 9
#   - platform: sensor
#     friendly_name: DP22
#     id: 22
#   - platform: sensor
#     friendly_name: DP23
#     id: 23
#   - platform: sensor
#     friendly_name: DP24
#     id: 24
#   - platform: sensor
#     friendly_name: DP25
#     id: 25
#   - platform: sensor
#     friendly_name: DP26
#     id: 26

I wrote <Which One> here because you want give each of these a unique name... Maybe it's "Living Room TV" or "Office Monitor" or "Coffee Maker"...

DP9, and DP22-26 have values, but I'm not sure of the format. Looking at what's shown in the Tuya App, I think these values may be a running total of electricity usage, but that's just a guess.

Here's what I've got in the Tuya App:

Here's what shows up for me now in Home Assistant with the config above:

The rounding of numbers is a little bit different on each value, but for my use this is close enough.

The Office Display Total when shown in HA is currently too small a value, 0.15 so I think HA is rounding that down to 0... I'm not sure though. May be that I need to configure DP21 differently.

And as I mentioned before, the totals aren't showing up.

Overall works pretty good though. Already for me it's even more functionality than I was hoping for.

Saentist commented 2 years ago

There is some calculations made in cloud, with is made to show in app. Amp*Volt=Watt with you see locally. But for kWh you need array of data containing W for per time, ~30 sec interval for 1 hour. Smart plugs don't have dedicated chip as some Wattmeter's to see it locally as on ZMAi-90, or similar. image

erwin commented 2 years ago

If anyone else is using the Smart Socket 2 on their network, and noticed that it's EXTREMELY unstable (like constantly disconnects from network) and frequently doesn't respond, I've found that using the Reconnect feature in my Unifi Controller, the device comes back online 99% of the time.

image

Currently I have a cron job running every 2 minutes that just checks if the device is responding to icmp (ping) requests. If it is, then everything should be fine. If it's not responding, then use the network's disconnect function.

You can use the Python unificontrol library to automatically do this reconnect.

client = UnifiClient(host="unifi.localdomain",
    username=UNIFI_USER, password=UNIFI_PASSWORD, site=UNIFI_SITE)
client.reconnect_client('MAC ADDRESS')
Unifi Reconnect Client if Ping fails working script (python) ```python #!/usr/bin/python3 import time import os import unificontrol from pythonping import ping from pprint import pprint # print("PATH:\n", os.environ['PATH'].replace(':', '\n')) print("Starting unifi_reconnect at ", time.ctime() ) pid = os.getpid() print("UID: ", os.getuid(), " PID: ", pid, " PGID: ", os.getpgid(pid), "\n") targetList = [ {"ip":"device.one.ip", "mac":"38:1f:xx:xx:xx:xx", "name": "PlugOneName"}, {"ip":"device.two.ip", "mac":"38:1f:xx:xx:xx:xx", "name": "PlugTwoName"}, ] pingCount = 4 client = unificontrol.UnifiClient( host="CONTROLLER YOUR IP", port=8443, username="CONTROLLER YOUR USERNAME", password="CONTROLLER YOUR PASSWORD") def isHostUp(target): response_list = ping(target, count=pingCount) response_iterator = iter(response_list) for r in range(pingCount): try: if (str(next(response_iterator))[:5] == "Reply"): return True except: pass return False for target in targetList: print(target['ip'], " -> ", target['name'], " ", end="") if (isHostUp(target['ip'])): print('Success') else: client.reconnect_client(target['mac']) print('Unifi Reconnected') ```

Lately I've been connecting my computer via wifi instead of ethernet, and it seems to make the Smart Plug located next to my computer disconnect even more frequently. Hopefully I'll soon move back to ethernet and hopefully the device will stop disconnecting so frequently.

If it does continue to disconnect frequently, I'll probably make a toggle state script that combines the above ping / reconnect with the nodejs tuya-cli to check the status and set the status.

Something like:

# Get the current Status
tuya-cli get --dps 1 --id xxx --key xxx --ip xxx --protocol-version 3.3

# Turn Power On
tuya-cli set --dps 1 --set true --id xxx --key xxx --ip xxx --protocol-version 3.3

# Turn Power Off
tuya-cli set --dps 1 --set false --id xxx --key xxx --ip xxx --protocol-version 3.3