bieniu / ha-shellies-discovery

Script that adds MQTT discovery support for Shellies devices
Apache License 2.0
295 stars 90 forks source link

Support for SHCB-1 (Shelly Bulb RGBW GU10) #424

Closed tc-maxx closed 1 year ago

tc-maxx commented 1 year ago

Is your feature request related to a problem? Please describe.

The "Shelly Bulb RGBW GU10" is not recognized by the script.

Describe the solution you'd like

Please add support for model: SHCB-1

Additional context

{ "id": "Gaden_01_Blub", "model": "SHCB-1", "mac": "349454xxxxxx", "ip": "192.168.1.16", "new_fw": false, "fw_ver": "20230503-095009/v1.13.0-g9aed950" }

bieniu commented 1 year ago

MQTT configuration for SHCB is not fully compatible with HA MQTT integration. At least I didn't manage to create a properly working configuration.

tcsmaxx commented 1 year ago

Hello @bieniu,

thanks for the information. Is there a way to implement the following code? This works for me, but I would have preferred the device to be detected automatically by the script.

mqtt:
  light:
    - schema: template
      unique_id: "Garten_01_Blub_oben"
      name: "Garten_01_Blub_oben"
      state_topic: "shellies/Garten_01_Blub_oben/color/0/status"
      command_topic: "shellies/Garten_01_Blub_oben/color/0/set"
      command_on_template: >-
        {
          "turn": "on",

          {%- if red is defined and green is defined and blue is defined -%}
            "mode": "color",
            "red": {{ red }},
            "green": {{ green }},
            "blue": {{ blue }},
          {%- endif -%}

          {%- if brightness is defined -%}
            "gain": {{brightness | float | multiply(0.3922) | round(0)}},
            "brightness": {{brightness | float | multiply(0.3922) | round(0)}},
          {%- endif -%}

          {%- if effect is defined -%}
            {%- if effect == 'Meteor Shower' -%}
              "effect": 1
            {%- elif effect == 'Gradual Change' -%}
              "effect": 2
            {%- elif effect == 'Breath' -%}
              "effect": 3
            {%- elif effect == 'Flash' -%}
              "effect": 4
            {%- elif effect == 'On/Off Gradual' -%}
              "effect": 5
            {%- elif effect == 'Red/Green Change' -%}
              "effect": 6
            {%- else -%}
              "effect": 0
            {%- endif -%}
          {%- else -%}
            "effect": 0
          {%- endif -%}

          {% if color_temp is defined %}
            "mode": "white",
            "temp":{{ (1/(color_temp | float)) | multiply(1000000) | round(0) }},
          {% endif %}
        }
      effect_list:
        - "Off"
        - Meteor Shower
        - Gradual Change
        - Breath
        - Flash
        - On/Off Gradual
        - Red/Green Change

      command_off_template: '{"effect": 0,"turn":"off"}'
      state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
      brightness_template: "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"
      color_temp_template: "{{ 1000000 | multiply(1/(value_json.temp | float)) | round(0) }}"
      red_template: "{{ value_json.red }}"
      green_template: "{{ value_json.green }}"
      blue_template: "{{ value_json.blue }}"
      effect_template: "{{ value_json.effect }}"
      qos: 2
      retain: true
      max_mireds: 333
      min_mireds: 154

Thank you BR Max

bieniu commented 1 year ago

I'll check that, thanks.

tc-maxx commented 1 year ago

The payload for the MQTT Discovery looks like this:

{
 "unique_id": "Garten_01_Bulb_oben",
 "name": "Garten_01_Bulb_oben",
 "state_topic": "shellies/Garten_01_Blub_oben/color/0/status",
 "command_topic": "shellies/Garten_01_Bulb_oben/color/0/set",
 "command_on_template": "{ \"turn\":\"on\", {%if red is defined and green is defined and blue is defined%} \"mode\":\"color\", \"red\":{{red}}, \"green\":{{green}}, \"blue\":{{blue}}, {%endif%} {%if brightness is defined%} \"gain\":{{brightness | float | multiply(0.3922) | round(0)}}, \"brightness\":{{brightness | float | multiply(0.3922) | round(0)}}, {%endif%} {%if effect is defined%} {%if effect==\"Meteor Shower\"%} \"effect\":1 {%elif effect==\"Gradual Change\"%} \"effect\":2 {%elif effect==\"Breath\"%} \"effect\":3 {%elif effect==\"Flash\"%} \"effect\":4 {%elif effect==\"On/Off Gradual\"%} \"effect\":5 {%elif effect==\"Red/Green Change\"%} \"effect\":6 {%else%} \"effect\":0 {%endif%} {%else%} \"effect\":0 {%endif%} {% if color_temp is defined %} \"mode\":\"white\", \"temp\":{{(1/(color_temp | float)) | multiply(1000000) | round(0)}}, {% endif %} }",
 "effect_list": [
 "Off",
 "Meteor Shower",
 "Gradual Change",
 "Breath",
 "Flash",
 "On/Off Gradual",
 "Red/Green Change"
 ],
 "command_off_template": "{\"effect\":0,\"turn\":\"off\"}",
 "state_template": "{% if value_json.ison %}on{% else %}off{% endif %}",
 "brightness_template": "{{value_json.brightness | float | multiply(2.55) | round(0)}}",
 "color_temp_template": "{{1000000 | multiply(1/(value_json.temp | float)) | round(0)}}",
 "red_template": "{{[value_json.red](http://value_json.red/)}}",
 "green_template": "{{[value_json.green](http://value_json.green/)}}",
 "blue_template": "{{[value_json.blue](http://value_json.blue/)}}",
 "effect_template": "{{value_json.effect}}",
 "qos": 2,
 "retain": true,
 "max_mireds": 333,
 "min_mireds": 154,
 "device": {
    "connections": [
      [
        "mac",
        "34:94:54:6e:83:0a"
      ]
    ],
    "name": "Shelly Bulb RGBW Garten_01_Bulb_unten",
    "model": "Shelly Bulb RGBW",
    "sw_version": "20230503-095009/v1.13.0-g9aed950",
    "hw_version": "gen1 (SHCB-1)",
    "manufacturer": "Allterco Robotics",
    "configuration_url": "http://192.168.1.26/"
  } 
}
bieniu commented 1 year ago

I tested your configuration a few days ago and it works but unfortunately I don't have time to implement it in the script. Maybe at the weekend...

bieniu commented 1 year ago

I see a big problem with your configuration. If you set a color for the light and then switch back to white mode, the light icon still shows the last used color. So far I have no idea how to get around this.

tc-maxx commented 1 year ago

I tried to add switches to filter the temperature and RGB values in the response in a way that HA understands. But unfortunately I don't know how HA wants the value for the temperature.

      color_temp_template: "{% if value_json.ison and value_json.mode == 'white' %} {{ 1000000 | multiply(1/(value_json.temp | float)) | round(0) }} {% else %} 0 {% endif %}"
      red_template: "{% if value_json.ison and value_json.mode == 'color' %} {{value_json.red}} {% else %} 0 {% endif %}"
      green_template: "{% if value_json.ison and value_json.mode == 'color' %} {{value_json.green}} {% else %} 0 {% endif %}"
      blue_template: "{% if value_json.ison and value_json.mode == 'color' %} {{value_json.blue}} {% else %} 0 {% endif %}"

But with this adjustment, when switching to white mode, the light icon will be white.

bieniu commented 1 year ago

Yup, I tried exatly the same yesterday. I think we should calculate the correct RGB values in white mode.

tc-maxx commented 1 year ago

Have you ever used this somewhere, I'd like to take a look?

bieniu commented 1 year ago

No, I haven't used such calculations anywhere, I suspect that we will find something like that in the MQTT integration code.

github-actions[bot] commented 1 year ago

Stale issue message

Xinayder commented 3 months ago

I was messing around with this, trying to implement it by myself and found out that the template schema doesn't support color modes: https://www.home-assistant.io/integrations/light.mqtt/#comparison-of-light-mqtt-schemas

So, I used the work from the OP and finally implemented a light that works with my Shelly Duo RGBW GU10. The trick was to pass none to red_template, green_template and blue_template fields if mode is set to white, so Lovelace can correctly display the white temperature of the lamp if it's not using RGB.

schema: template
qos: '0'
red_template: '{%if value_json.mode == "color"%}{{value_json.red}}{%else%}{{ none }}{%endif%}'
green_template: >-
  {%if value_json.mode == "color"%}{{value_json.green}}{%else%}{{ none
  }}{%endif%}
blue_template: >-
  {%if value_json.mode == "color"%}{{value_json.blue}}{%else%}{{ none
  }}{%endif%}
state_topic: shellies/shellycolorbulb-485519DA73A9/color/0/status
origin:
  name: Shellies Discovery
  sw_version: 5.1.0
  support_url: https://github.com/bieniu/ha-shellies-discovery
min_mireds: 154
command_on_template: >-
  {"turn":"on","effect":0{%if red is defined and green is defined and blue is
  defined%},"mode":"color","red":{{red}},"green":{{green}},"blue":{{blue}}{%endif%}{%if
  brightness is
  defined%},"brightness":{{brightness|float|multiply(0.3922)|round}},"gain":{{brightness|float|multiply(0.3922)|round}}{%endif%}{%if
  color_temp is
  defined%},"mode":"white","temp":{{(1000000/(color_temp|int))|round(0,"floor")}}{%endif%}{%if
  transition is defined%},"transition":{{min(transition|multiply(1000),
  5000)}}{%endif%}}
name: Abajur
state_template: '{%if value_json.ison%}on{%else%}off{%endif%}'
device:
  name: Shelly Duo RGBW 485519DA73A9
  connections:
    - - mac
      - 48:55:19:da:73:a9
  hw_version: gen1 (SHCB-1)
  sw_version: 20231107-162425/v1.14.1-rc1-g0617c15
  model: Shelly Duo RGBW
  configuration_url: http://192.168.1.138/
  manufacturer: Allterco Robotics
command_topic: shellies/shellycolorbulb-485519DA73A9/color/0/set
command_off_template: >-
  {"turn":"off","effect":0{%if transition is
  defined%},"transition":{{min(transition|multiply(1000),5000)}}{%endif%}}
unique_id: shellycolorbulb-485519da73a9-light-0
color_temp_template: '{{((1000000/(value_json.temp|int,2700)|max)|round(0,"floor"))}}'
availability:
  - payload_not_available: 'false'
    topic: shellies/shellycolorbulb-485519DA73A9/online
    payload_available: 'true'
  - value_template: '{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}'
    topic: shellies/shellycolorbulb-485519DA73A9/info
brightness_template: '{{value_json.brightness|float|multiply(2.55)|round}}'
max_mireds: 333
platform: mqtt