Sunday, 23 February 2020

Kogan Smart Plugs

The Physical Device

These are piggyback plugs with a toggle button, on-off LED (ring around the button) and status LED. They plug into a wall, with the device you want to control plugged into the back, which can be switched on and off by the button, or you can use the smartphone app.

The Smarts

The Wi-Fi module for this device comes from Tuya, along with the app and server infrastructure.  In addition to off/on functionality, the app also provides you with power consumption information, and timer and more complex programming capabilities.   The smart automation gives you access to data from other Tuya based devices you own, Weather, Sunset/Sunrise, Location, so fairly complex automation is possible. Location is from the smartphone, which could be useful to turn things on when you are arriving home, and off when you are leaving, provided you live alone.  Anyone with a family or flatmates though probably needs a presence detection that covers multiple people, which this does not provide.  The app also interfaces with Alexa and Google Home, as well as IFTTT, to provide access to basic functionality and automation scenes.

The Details

To free yourself from the Tuya ecosystem, you first need to know more details about how the device is wired internally.  This applies whether you are using something like tuyapi to control the device without changing the firmware, or reflashing alternate firmware like Tasmota or ESPHome.


Tuya Protocol

To control the device locally with libraries such as tuyapi, you need to know how the different features map to "dps" (data point state).

These are the dps that are reported at startup (based on eavesdropping the protocol before encryption starts):

dps 1 this is the power status. You can write true to turn the switch on, false to turn it off. Reading it will report the current status, including when it has been changed by the button on the plug.

dps 2 this I suspect has something to do with timers, and if so, it should be read/write.
   Edit: confirmed that this is the timeout in seconds until the state is toggled.
             if set to 10 when the switch is on, it will turn off after 10 seconds.
             if set to 10 when the switch is off, it will turn on after 10 seconds.
             I haven't confirmed the upper limit, but the Tuya app lets you set up to 24 hours.

The following are readonly
dps 4 this is the instantaneous current in milliamps.
dps 5 this is the instantaneous power in tenths of a watt.
dps 6 this is the voltage in tenths of vol

This seems to be a common assignment for other brands as well, so this device is likely based on an off the shelf whitelabel product, or reference design from Tuya.

Hardware Connections

To use alternate firmware, the Tuya protocol is not so much of interest, instead you need to know what is connected where on the ESP8266 module.

The Tuya TYWE3S module is connected to a HLW8012 power monitoring IC and general I/O for button, LED and relay as follows on these devices:

GPIO 0: Toggle Button
GPIO 4: HLW8012 CF
GPIO 5: HLW8012 CF1
GPIO 12: HLW8012 SELi
GPIO 13: Status LED (inverted)
GPIO 14: Relay

The LED ring around the button seems to be connected to the relay, and cannot be controlled independently.

Here is an ESPHome configuration for use with Home Assistant that gives you almost the same functionality as the original product without any need for a cloud service.  The button toggles the relay directly, so can operate offline. The one missing feature is that timers are not implemented inside the device itself with this configuration.  Since ESPHome hardcodes the Wi-Fi configuration, rather than using the status LED to indicate Wi-Fi connection state, I modified it to indicate whether any power has been drawn since it was turned on.

esphome:
  name: kogan_socket_1
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "ACCESSPOINT"
  password: "PASSWORD"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Kogan Socket 1 Fallback Hotspot"
    password: "XXXXXXXXXXX"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: true
    id: power_button
    on_press:
      - switch.toggle: relay

  - platform: status
    name: "Kogan Plug 1 Status"

switch:
  - platform: gpio
    id: green_led
    pin:
      number: GPIO13
      inverted: true
    restore_mode: ALWAYS_OFF

  - platform: gpio
    name: "Kogan Plug 1"
    pin: GPIO14
    id: relay
    icon: mdi:power
    restore_mode: ALWAYS_OFF
    on_turn_off:
      - switch.turn_off: green_led

sensor:
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: true
    cf_pin: GPIO04
    cf1_pin: GPIO05
    current:
      name: "Kogan Plug 1 Current"
      unit_of_measurement: A
    voltage:
      name: "Kogan Plug 1 Voltage"
      unit_of_measurement: V
    power:
      id: kogan_plug_1_wattage
      name: "Kogan Plug 1 Power"
      unit_of_measurement: W
      on_value_range:
        above: 1
        then:
          switch.turn_on: green_led
    current_resistor: "0.00087"   ### HIGHER VALUE GIVES LOWER WATTAGE
    voltage_divider: "2030"   ### LOWER VALUE GIVES LOWER VOLTAGE
    change_mode_every: 8
    update_interval: 5s
    
  - platform: total_daily_energy
    name: "Kogan Plug 1 Daily Energy"
    power_id: kogan_plug_1_wattage
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh

time:
  - platform: homeassistant
    id: homeassistant_time

No comments: