Making a Zigbee relay/switch appear as a light in HomeKit

Good Morning,

I have been dealing with the stresses of tuya integration (Tuya, Local-Tuya, Tuya-Local) disconnecting from the lights, so theres only 3 options:

  1. Flash custom firmware to the tuya lights; Risky may brick device(s).
  2. Purchases another brand lights preferably based zigbee lights.
  3. Use smart switch or smart relay to control lights. :white_check_mark:

I went with option 3 as i actually do love the tuya lights but i don’t like their connectivity to come assistant which is another conversational piece.


So here’s my solutions:

  1. Gather Equipment
    Purchase ZBMINIL2, this one does not require an additional neutral to your light switch.
    Purchase Sonoff Zigbee USB Dongle, this will be how home assistant talks to the smart relays.

  2. Install & Connect To Home Assistant
    Install the relay behind your light switch as per instruction, then connect via USB Dongle in home assistant. Both are very straight forward.

  3. Create A Light Entity
    If your like me and don’t want your switch to appear as a switch in homekit, we can create a fake light entity which state is linked to the state of the smart relay. To achieve this you cant do it by creating a helper via setting > devices>helpers etc. as this will create a binary sensor which we don’t want for homekit. We will need to do it in the config yaml. So open you config yaml and past this in :

light:
  - platform: template
    lights:
      kitchen_light:
        friendly_name: "Kitchen Light Proxy"
        unique_id: "kitchen_light_proxy"
        value_template: "{{ 'on' if states('switch.YOUR-RELAY-ENTITY-HERE') == 'on' else 'off' }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.kitchen_relay
        turn_off:
          service: switch.turn_off
          target:

Be sure to update the friendly name, unique id, and your relay’s entity id in the code.
Save & Restart Home Assistant!

  1. Add To HomeKit Bridge
    Once added and submitted your device will appear in HomeKit as a light with basic on/off functionality. Quite foolish apple doesn’t let us select what our device should be shown as in homekit so this is the work around.

Notes:
The smart relay i mentioned earlier is multi-useful you can use it to make your light switches smart or you can use it as a relay. I have an additional one of these setup on my bathroom fan functioning as a simple relay and paired to homekit as a fan. Also note please read the instruction to the of the smart relay carefully, when wired correctly your physical light switch become a state toggle meaning regardless if the relay is off or switch is off either fully function. Think of it as a 3-way switch between your light switch and the smart relay in a sense, but the smart relay is is behind your light switch and can be remotely controlled :slight_smile: .

Optionally you can do more with the code above, in this instance i have the imaginary light we created in yaml setup to only control basic on/off functions which are linked to smart relay’s state value. But additionally you can technically return the color changing functionality and embed them in the mock light we created. I did not do this because you’ll fall back into the same pit this tutorial pulled you up from when the tuya integration disconnects from the tuya lights, tho you’ll still be able to turn on/off the lights because the mock light controls on/off via the smart relay. Im currently still working on that code and currently got the color wheel to appear in homekit just needs to be properly coded so it grabs the value from one and applies it both real lights.

Edit

# Create a template light entity paired to switch.kitchen_relay for on/off and Tuya lights for color/brightness/temp
light:
  - platform: template
    lights:
      kitchen_light:
        friendly_name: "Kitchen Light"
        unique_id: "kitchen_light_mock"
        value_template: "{{ 'on' if states('switch.kitchen_relay') == 'on' else 'off' }}"
        level_template: "{{ state_attr('light.kitchen_light_rear', 'brightness') | default(255) }}"
        temperature_template: "{{ state_attr('light.kitchen_light_rear', 'color_temp') | default(153) }}"
        color_template: "{{ state_attr('light.kitchen_light_rear', 'hs_color') | default([0, 100]) }}"
        turn_on:
          - service: switch.turn_on
            data:
              entity_id: switch.kitchen_relay
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              hs_color: "{{ state_attr('light.kitchen_light_rear', 'hs_color') | default([0, 100]) }}"
              brightness: "{{ state_attr('light.kitchen_light_rear', 'brightness') | default(255) }}"
              color_temp: "{{ state_attr('light.kitchen_light_rear', 'color_temp') | default(153) }}"
        turn_off:
          - service: switch.turn_off
            data:
              entity_id: switch.kitchen_relay
        set_level:
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              brightness: "{{ brightness }}"
        set_temperature:
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              color_temp: "{{ mireds }}"
        set_color:
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              hs_color: ["{{ h }}", "{{ s }}"]

this code does the aformentioned, but does not switch back from color mode to temp mode still needs fixing.but the on/off via smart relay is spot on :slight_smile:

I’m confused, can’t you just use the helper:

i tried that, it came up as a sensor. But if you have luck that way go for it :slight_smile: additionally if you do it the manual way you get more flexibility to make it have color, temp brightness which i only have partially implemented so far. So if you look under states attributes you wont have anything there light your actual light may have color controls etc.

its only partially implemented code not done color works and uses relay for on/off function.

# Create a template light entity paired to switch.kitchen_relay for on/off and Tuya lights for color/brightness/temp
light:
  - platform: template
    lights:
      kitchen_light:
        friendly_name: "Kitchen Light"
        unique_id: "kitchen_light_mock"
        value_template: "{{ 'on' if states('switch.kitchen_relay') == 'on' else 'off' }}"
        level_template: "{{ state_attr('light.kitchen_light_rear', 'brightness') | default(255) }}"
        temperature_template: "{{ state_attr('light.kitchen_light_rear', 'color_temp') | default(153) }}"
        color_template: "{{ state_attr('light.kitchen_light_rear', 'hs_color') | default([0, 100]) }}"
        turn_on:
          - service: switch.turn_on
            data:
              entity_id: switch.kitchen_relay
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              hs_color: "{{ state_attr('light.kitchen_light_rear', 'hs_color') | default([0, 100]) }}"
              brightness: "{{ state_attr('light.kitchen_light_rear', 'brightness') | default(255) }}"
              color_temp: "{{ state_attr('light.kitchen_light_rear', 'color_temp') | default(153) }}"
        turn_off:
          - service: switch.turn_off
            data:
              entity_id: switch.kitchen_relay
        set_level:
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              brightness: "{{ brightness }}"
        set_temperature:
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              color_temp: "{{ mireds }}"
        set_color:
          - service: light.turn_on
            data:
              entity_id:
                - light.kitchen_light_rear
                - light.kitchen_light_rear_2
              hs_color: ["{{ h }}", "{{ s }}"]

If you cut the power to the light bulbs at the smart switch/relay, then you will have to wait for it to boot up and reconnect and sync with HA before you can control the color, brightness and so on.
This might take quite a few seconds or even around a minute.
Your script needs to take this in to consideration and also users being impatient and trying to adjust it multiple times in the startup period.

So not at all, at least not in my case. idk if its because im using Tuya Local and not tuya but when i cut power at the smart relay, or even the breaker when i was setting it up. I did nto experience lag in controls. In fact yeah when you toggle the breaker the tuya lights go offline and when you toggle them back they immediately come right back online.

What i do need help with tho is fixing my code so it properly controls light & temp. It seems if i change color it does not go back to temp mode

Edit @ wally do you see anything in the code that would be concerning as well. i needed to do this because originally i had my presence sensor project controlling the lights but house members would touch the switch and mess up everything lol so i made the switch smart lol, also the tuya local will mess up just less frequently as the official tuya app.

I never needed to go in that direction, so no.
I avoid WiFi for low bandwidth devices and I avoid especially Tuya WiFi devices.