Sign inSign up

dododuke/smarthomeconverter

By dododuke

Updated over 1 year ago

Converts TUYA and ZigBee devices interface to Shelly interface. Used for integration to SunnyHome.

Image
Networking
Internet of things
0

309

dododuke/smarthomeconverter repository overview

This software converts TUYA and ZigBee devices interface to Shelly interface. Can handle local (LAN) or cloud TUYA interface. ZigBee devices are handled via Zigbee2MQTT https://www.zigbee2mqtt.io/ Project was created mainly for integration of other devices to SunnyHome (snyll/sunnyhome), because SunnyHome handles only Shelly devices.

Important note for TUYA local mode:

  • Many Tuya devices do not work properly in Local mode do not handle multiple commands sent in quick succession.

  • Some will reboot, possibly changing state in the process,

  • others will go offline for 30s to a few minutes if you overload them.

  • there is some rate limiting to try to avoid this, but it is not sufficient for many devices,

  • and may not work across entities where you are sending commands to multiple entities on the same device.

  • The rate limiting also combines commands, which not all devices can handle.

  • The exact timing depends on the device.

  • and probably many more...

  • For first use and testing, I recommend to use Tuya-cloud, then Tuya-local if needed.

Preconditions for TUYA:

  1. Download Tuya Smart mobile app
  2. Register your devices using this app.
  3. Create account on Tuya Smart Developer Account https://auth.tuya.com/register
  4. Get the Tuya device Access ID ,Access Secret and DeviceId - How to do it, for example, here https://clusterm.github.io/tuyanet/
  5. Get Local IP address of device in your home network

Preconditions for ZigBee:

  1. Goto https://www.zigbee2mqtt.io/
  2. Install Zigbee2MQTT
  3. You can use dockers for Zigbee2MQTT and for MQTT Eclipse Mosquitto https://www.zigbee2mqtt.io/guide/installation/02_docker.html
  4. Zigbee2MQTT provides also web fronted interface on port 8080
  5. Connect USB ZigBee adapter and obtain device name (in my case /dev/ttyUSB0) or Get Local IP address of ZigBee gateway device in your home network
  6. Obtain ZigBee device IEEE address e.g. 0xa5b158e28109c984 or Friendly Name

Tested ZigBee adapters with ZigBee2Mqtt:

  1. Home assistant SkyConnect USB

    https://rpishop.cz/usb-brany/5665-home-assistant-skyconnect-usb.html

Connect the device to the Raspberry and find out what device name (in my case /dev/ttyUSB0). The device name can be found in the /dev directory or after connection with the dmesg command. The listing will be something like:

cp210x 1-1.2:1.0: cp210x converter detected
usb 1-1.2: cp210x converter now attached to ttyUSB0

Snippet example of my Zigbee2MQTT config file configuration.yaml

permit_join: true
mqtt:
 base_topic: zigbee2mqtt
 server: mqtt://192.168.1.160:1883
 keepalive: 60
 reject_unauthorized: false
 version: 4
serial:
 adapter: ezsp
 port: /dev/ttyUSB0
frontend:
 port: 8080

Mapping device to docker requires to add parameter for docker run command: --device=/dev/ttyUSB0:/dev/ttyUSB0

Example of docker run command: docker run -p 8080:8080 --restart unless-stopped --name zigbee2mqtt -v /opt/zigbee2mqtt-data/:/app/data/ --device=/dev/ttyUSB0:/dev/ttyUSB0 -d koenkk/zigbee2mqtt

  1. Silvercrest IAN 367549_2101 (Lidl)

Find out the IP address on the home router and then enter it in the configuration (e.g. 192.168.1.198).
Snippet example of my Zigbee2MQTT config file configuration.yaml

permit_join: true
mqtt:
 base_topic: zigbee2mqtt
 server: mqtt://192.168.1.160:1883
 keepalive: 60
 reject_unauthorized: false
 version: 4
serial:
 adapter: ezsp
 port: tcp://192.168.1.198:8888
frontend:
 port: 8080

SmartHomeConverter Installation:

Note: change platform to amd64 if needed

  1. Create a directory for the configuration file eg /opt/smarthomeconverter/config

  2. Download and run docker run -p 8801:8801 --rm --name smarthomeconverter --platform=linux/arm64/v8 -v /opt/smarthomeconverter/config/:/app/config/ -d dododuke/smarthomeconverter:arm64

For automatic start after reboot add parameter --restart unless-stopped

  1. The default configuration file smarthomeconverter.json will be created in given path eg /opt/smarthomeconverter/config if not found

  2. Edit and save configuration file smarthomeconverter.json. (Restart docker to load the new configuration file docker restart smarthomeconverter) For configuration details, see chapter Configuration.

  3. Now the application should run on https://your-ip:8801, for example if your-ip is 192.168.0.1, got to the URL http://192.168.0.1:8801/app/status or if you want to see more details go to http://192.168.0.1:8801/app/status?detail=true

It shows a device and switch table status.

SmartHomeConverter Updating:

docker stop smarthomeconverter

docker rm smarthomeconverter

docker pull dododuke/smarthomeconverter:arm64

and then again the main run command eg:

docker run -p 8801:8801 --rm --name smarthomeconverter --platform=linux/arm64/v8 -v /opt/smarthomeconverter/config/:/app/config/ -d dododuke/smarthomeconverter:arm64

For automatic start after reboot add parameter --restart unless-stopped

Configuration file smarthomeconverter.json

  1. Edit config file smarthomeconverter.json

  2. Enter then Tuya device Access ID , Access Secret, DeviceId, Local IP, and LocalKey to the config. If you don't know LocalKey for Tuya device, leave it empty. It will be downloaded automatically from the Cloud. Then, you can copy it from web table http://192.168.0.1:8801/app/status?detail=true and paste to the config file. Then the Cloud won't be contacted for local keys. Each time the device is deleted and re-registered to the Cloud, the LocalKey changes.

  3. or for ZigBee, Enter then ZigBee device information Protocol, DeviceId , Local IP, and LocalPort to the config.

 {
   "DebugLevelFrom": 0,
   "DebugLevelTo": 8,
   "MaxLogHistory": 1000,
   "Devices": [
   	{
   		"Id": "MyShelly",
   		"Name": "My Virtual Shelly",
   		"Active": true,
   		"Protocol": "ShellyPro3",    
   		"LocalIp": "",
   		"LocalPort": 8801
   	},
   	{
   		"Id": "MyTuya",
   		"Name": "My thermostat",
   		"Active": true,
   		"Protocol": "TUYA-local",
   		"AccessId": "yyyyyyyyyyyyyyyyyyyy",
   		"ApiSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
   		"DeviceId": "zzzzzzzzzzzzzzzzzzzzzz",
   		"LocalKey": "",
   		"Version": "3.3",
   		"Region": "CentralEurope",
   		"LocalIp": "192.168.1.123",
   		"LocalPort": 0
   	},
   	{
   		"Id": "MyZigBeeSocket",
   		"Name": "Socket in kitchen",
   		"Active": true,
   		"DeviceId": "0xa5b158e28109c984",
   		"LocalIp": "192.168.1.160",
   		"LocalPort": 1883,
   		"Protocol": "ZigBee2Mqtt"
   	}
   ],
   "Map": [
   	{
   		"Pin1": {
   			"DeviceId": "MyShelly",
   			"Switch": "0"
   		},
   		"Pin2": {
   			"DeviceId": "MyTuya",
   			"Switch": "1",
   			"SwitchName": "switch",
   			"TemperatureName": [
   				"upper_temp"
   			],
   			"TemperatureFactor": 0.5,
   			"CurrentName": [
   				"cur_current",
   				"18"
   			],
   			"CurrentFactor": 0.001,
   			"PowerName": [
   				"cur_power",
   				"19"
   			],
   			"PowerFactor": 0.1,
   			"VoltageName": [
   				"cur_voltage",
   				"20"
   			],
   			"VoltageFactor": 0.1,
   			"EnergyName": [
   				"",
   				""
   			],
   			"EnergyFactor": 0.1
   		}
   	},
   	{
   		"Pin1": {
   			"DeviceId": "MyShelly",
   			"Switch": "3"
   		},
   		"Pin2": {
   			"DeviceId": "MyZigBeeSocket",
   			"Switch": "1"
   		}
   	}
   ]
} 

Configuration JSON description

  • Logging
    • DebugLevelFrom - minimum level of information for logging

    • DebugLevelTo - maximum level of information for logging

    • MaxLogHistory - number of lines for log history

    • Devices array - array of devices. Must contain at least one virtual Shelly.

      • Id - device unique identification e.g. MyShelly
      • Name - device friendly name (for display)
      • Active - true for active device, if false, device is ignored
      • Protocol - allowed values are:
        • ShellyPro3 - for Shelly device
        • ShellyPro4PM - for device which can measure Power value
        • Shelly1PMPlus - for device which can measure Power value
        • TUYA-local - for TUYA device controlled locally over the LAN
        • TUYA-cloud - for TUYA device controlled over the Cloud
        • TUYA - for TUYA device controlled locally and - if it fails - then over cloud
        • ZigBee2Mqtt - for ZigBee devices over MQTT
      • AccessId - access id for Tuya, can be obtained as described above
        - for ZigBee, enter MQTT base topic or leave empty for default zigbee2mqtt
      • ApiSecret - Api secret id for Tuya, can be obtained as described above
        - not needed for ZigBee
      • DeviceId - id of the specific TUYA device, can be obtained as described above
        - for ZigBee, enter device IEEE address e.g. 0xa5b158e28109c984 or Friendly Name e.g. Kitchen socket 1
      • LocalKey - local key for device controlled over the LAN (not required for the cloud devices). Can be obtained as described above. If it is empty, it will be automatically dowloaded from the Cloud.
        - not needed for ZigBee
      • Version - version of Tuya protocol. The most common is 3.3. Version 3.4 is not tested yet (not required for the cloud devices).
        - not needed for ZigBee
      • Region - the region of the Tyua Cloud that you have chosen in your Cloud account (not required for the local devices). A valid value can take on only one of these values China,WesternAmerica,EasternAmerica,CentralEurope,WesternEurope,India
        - not needed for ZigBee
      • LocalIp - local (LAN) IP address of your device (not required for the cloud devices).
        - for ZigBee, enter MQTT server IP address.
        It is recommended to leave blank for Shelly devices.
      • LocalPort - optional - local (LAN) port of your device (not required for the cloud devices). Can be 0 for automatic port selection (6668).
        - for ZigBee, enter MQTT server port or leave empty for default 1883
      • RateLimit - optional - can be 0 - the minimum time in milliseconds for which the device is polled. The number of device queries can be limited (especially for problematic local devices). Queries to status of device that will be more frequent than this interval will not be performed or they will be loaded from the cache.
        - not needed for ZigBee
      • WaitAfterConnect - optional - can be 0 - the time in milliseconds to wait after connecting a device to start communication (especially for problematic local devices).
        - not needed for ZigBee
      • PermanentConnection - optional - default is false - indicates whether the connection to the device should be maintained permanently (especially for problematic local devices).
        - not needed for ZigBee
      • MessageTimeout - optional - message timeout in milliseconds for ZigBee, default is 8000
        - not needed for TUYA
    • Map array - array of pin pairs. A pin map that tells how individual device pins (switches) will map to virtual Shelly switches.

      • Pin1 - pin of virtual Shelly

        • DeviceId - The virtual Shelly device Id to which the pin relates
        • Switch - The virtual Shelly device switch (relay) number - usually starting from 0
      • Pin2 - pin of real (Tuya) device

        • DeviceId - The real (Tuya, ZigBee) device Id to which the pin relates from Devices section
        • Switch - The real (Tuya, ZigBee) device switch number - usually starting from 1

        Other ZigBee parameters are automatically obtained from MQTT, but can be entered manually.
        Other TUYA parameters depends on device and can be found on Tuya cloud account in device details or here https://developer.tuya.com/en/docs/iot/product-standard-function-introduction?id=K9tp15ceh63gr

        • SwitchName - device switch name - usually switch or switch_1, or for device with more switches switch_2 etc.
        • CurrentName - device electric current value name - usually cur_current or 18 (only if device supports)
        • CurrentFactor - the number by which the CurrentName value is multiplied. You must enter the value so that the result should be in units of amperes (A). So if the device returns a value in milliamps (mA), enter 0.001. Example: device returns 2300 mA, 2300*0.001 = 2.3 A
        • PowerName - device electric power value name - usually cur_power or 19 (only if device supports)
        • PowerFactor - the number by which the PowerName value is multiplied. You must enter the value so that the result should be in units of watts (W). So if the device returns a value in in hundreds of milliwatts, enter 0.1. Example: device returns value 15000 , 15000*0.1 = 1500 W
        • VoltageName - device electric voltage value name - usually cur_voltage or 20 (only if device supports)
        • VoltageFactor - the number by which the VoltageName value is multiplied. You must enter the value so that the result should be in units of volts (V). So if the device returns a value in in hundreds of millivolts, enter 0.1. Example: device returns value 2370 , 2370*0.1 = 237 V
        • TemperatureName - device temperature value name - depends on device (example upper_temp or 3 - only if device supports)
        • TemperatureFactor - the number by which the TemperatureName value is multiplied. You must enter the value so that the result should be in units of degrees Celsius (°C). So if the device returns a value in half degrees, enter 0.5. Example: device returns value 41 , 41*0.5 = 20.5 °C
        • EnergyName - (only if device supports) - device total energy consumed (watt-hours) value name - currently don't know usual value name (you have to try to find it).
        • EnergyFactor - the number by which the EnergyName value is multiplied. You must enter the value so that the result should be in units of watt-hours (Wh). So if the device returns a value in in hundreds of watt-hours, enter 0.1. Example: device returns value 5000, 5000*0.1 = 500 Wh

Integration to SunnyHome

Click Add Device

  • choose type of device
    • ShellyPro3 for general device
    • ShellyPro4PM - for device which can measure Power value
    • Shelly1PMPlus - for device which can measure Power value
  • select appropriate relay /switch.
  • enter IP address of your virtual Shelly server and port separated by colon in format IPAddress:Port e.g: 192.168.0.1:8801

Testing device ON/OFF:

Assume that application is running on IP 192.168.0.1, port 8801.

  • To get switch status of first device (device 0), enter to www browser:

http://192.168.0.1:8801/rpc/Switch.GetStatus?id=0

  • To set switch status to ON:

http://192.168.0.1:8801/rpc/Switch.Set?id=0&on=true

  • To set switch status to OFF :

http://192.168.0.1:8801/rpc/Switch.Set?id=0&on=false

Troubleshooting:

  • To display the last 500 lines of Docker container logs, you can use the docker logs command with the --tail option. This option allows you to specify the number of lines from the end of the log file that you want to display. Here's an example:

docker logs --tail 500 smarthomeconverter

(the most recent entries are at the top)

Tag summary

Content type

Image

Digest

sha256:1f7996dda

Size

71.9 MB

Last updated

over 1 year ago

docker pull dododuke/smarthomeconverter:32bit