Mqtt light default schema for Shelly RGBW2 (RGBW and CCT)

that’s not a template

        rgbw_command_template: >
          {
            "red": {{ red }},
            "green": {{ green }},
            "blue": {{ blue }},
            "white": {{ white }}
          }

of course you’re right.
But even with the use of your snippet, it remains not working.

Current version:

pck_hallways_lights:
  mqtt:
    light:
      - name: "Hall Floor X"
        command_topic: "shellies/light-hall2/color/0/set"
        state_topic: "shellies/light-hall2/color/0/status"
        rgbw_state_topic: "shellies/light-hall2/color/0/status"
        brightness_state_topic: "shellies/light-hall2/color/0/status"
        availability_topic: "shellies/light-hall2/online"
        payload_available: "true"
        payload_not_available: "false"
        retain: false
        qos: 1
        rgbw_command_template: >
          {
            "red": {{ red }},
            "green": {{ green }},
            "blue": {{ blue }},
            "white": {{ white }}
          }
        brightness_command_template: >
           {
            "gain": {{ value | float | multiply(0.3922) | round(0) }}
           }
        payload_on: >-
          {"turn": "on"}
        payload_off: >-
          {"turn": "off"}
        state_value_template: >-
          {% if value_json.ison %}{"turn": "on"}
          {% else %}{"turn": "off"}
          {% endif %}
        brightness_value_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
        rgbw_value_template: "{{value_json.red}},{{value_json.green}},{{value_json.blue}},{{value_json.white}}"

I don’t see why it wouldn’t work. Try clearing your cache

Don’t know if this helps - but doing a quick google around:

command_on_template: >
      {"turn": "on"
      {%- if brightness is defined -%}
      , "gain": {{brightness | float | multiply(0.3922) | round(0)}}
      {%- endif -%}
      {%- if red is defined and green is defined and blue is defined -%}
      , "red": {{ red }}, "green": {{ green }}, "blue": {{ blue }}
      {%- endif -%}
      {%- if white is defined -%}
      , "white": {{ white }}
      {%- endif -%}
      {%- if effect is defined -%}
      , "effect": {{ effect }}
      {%- endif -%}
      }

EDIT: Nevermind I see that is the template style.

So far the only thing I have been seeing that people are saying works - is:

Using the integration.

The integration works as long as not broken by subsequent HA releases. I cannot afford such unreliability.
MQTT gives me the best I can get. Supports all shelly features (speaking of that, the Shelly integration doesn’t support updating light properties when the light is turned off).

So I’m going to stay with MQTT as long as it is possible.

Got it working.
There were missing declarations for rgbw_command and brightness_command topics.

Thank you all of you for help and support in this topic. I’m going to prepare one more example, this time creating cct light with use of red and green channels of rgbw2 device.
Its usage with template schema is very popular approach (because the only one known)

RGBW light for Shelly RGBW2 device

mqtt:
  light:
    - name: "Hall Floor X"
      command_topic: "shellies/light-hall2/color/0/set"
      rgbw_command_topic: "shellies/light-hall2/color/0/set"
      brightness_command_topic: "shellies/light-hall2/color/0/set"
      state_topic: "shellies/light-hall2/color/0/status"
      rgbw_state_topic: "shellies/light-hall2/color/0/status"
      brightness_state_topic: "shellies/light-hall2/color/0/status"
      availability_topic: "shellies/light-hall2/online"
      payload_available: "true"
      payload_not_available: "false"
      retain: false
      qos: 1
      rgbw_command_template: >
        {
          "red": {{ red }},
          "green": {{ green }},
          "blue": {{ blue }},
          "white": {{ white }}
        }
      brightness_command_template: >
        {
          "gain": {{ value | float | multiply(0.3922) | round(0) }}
        }
      payload_on: >-
        {"turn": "on"}
      payload_off: >-
        {"turn": "off"}
      state_value_template: >-
        {% if value_json.ison %}{"turn": "on"}
        {% else %}{"turn": "off"}
        {% endif %}
      brightness_value_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
      rgbw_value_template: "{{value_json.red}},{{value_json.green}},{{value_json.blue}},{{value_json.white}}"

CCT light for Shelly RGBW2 device by combining of red and green channels

mqtt:
  light:	  
    - name: "Hall Floor Y CCT"
      command_topic: "shellies/light-hall2/color/0/set"
      color_temp_command_topic: "shellies/light-hall2/color/0/set"
      brightness_command_topic: "shellies/light-hall2/color/0/set"
      state_topic: "shellies/light-hall2/color/0/status"
      color_temp_state_topic: "shellies/light-hall2/color/0/status"
      brightness_state_topic: "shellies/light-hall2/color/0/status"
      availability_topic: "shellies/light-hall2/online"
      payload_available: "true"
      payload_not_available: "false"
      retain: false
      qos: 1
      color_temp_command_template: >
        {
          "red": {{ ( (value | float ) - 153 ) | multiply(0.734870317) | round(0) }},
          "green": {{ ( 500 - (value | float ) ) | multiply(0.734870317) | round(0) }},
          "blue": 0,
          "white": 0
        }
      brightness_command_template: >
        {
          "gain": {{ value | float | multiply(0.3922) | round(0) }}
        }
      payload_on: >-
        {"turn": "on"}
      payload_off: >-
        {"turn": "off"}
      state_value_template: >-
        {% if value_json.ison %}{"turn": "on"}
        {% else %}{"turn": "off"}
        {% endif %}
      brightness_value_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
      color_temp_value_template: "{{ ( ( value_json.red | float | multiply(1.36078431373) ) + 153 ) | round(0) }}"

Remark
The most “confusing” part in both above is to understand relationship between payload_on/off attributes and state_value_template one.
The payload_on and payload_off have to contain content needed to be sent to MQTT in order to turn the light on or off.
While state_value_template is dedicated for retrieving current state, it MUST render into values defined in payload_on or payload_off. Otherwise, the entity will break.
It’s illogical and pointless but it is as it is. We cannot do anything with it.
So, if one would like to extend the content of on/off commands for example to achieve gradual toggling of the light it can be achieved this way:

        payload_on: >-
          {"turn": "on", "transition":200}
        payload_off: >-
          {"turn": "off", "transition":400}
        state_value_template: >-
          {% if value_json.ison %}{"turn": "on", "transition":200}
          {% else %}{"turn": "off", "transition":400}
          {% endif %}

I’m pretty sure the matching is executed using string values, regardless we put jsons there. So watch out spaces etc.

2 Likes

Wonderful, thanks for the work!

Just a slight adjustment I’ve made to the brightness template:

brightness_command_template: >-
  {"gain": {{ 0 if value <=3 else (value | float | multiply(0.3922) | round(0)) }}}

It’s not the prettiest thing, but this allows you to adjust the RGB (main) brightness all the way down to 0 while preserving the white brightness. Messing with the “Color brightness” slider has always led to unintended results for me, so I ignore the thing.

The main disadvantage here is that you lose the lowest brightness setting for RGB to the 0 value, but I’ve never had an issue.

And in the interest of ternary operators, I shortened your state_value_template for you.

state_value_template: >-
  {"turn": "{{ 'on' if value_json.ison else 'off' }}"}

These are just minor improvements, but big time hats off to you for doing the hard part!

1 Like

I updated post #9 as follows:

  1. added CCT light entity declaration for Shelly RGBW2 using a combination of red and green channels.
  2. reformated yaml snippets rooting them to mqtt: (initially those were part of a package)
  3. Added remark about payloads and state_value_template

I also changed the subject of the discussion thread to better match the result. Also selected post #9 as a solution and mentioned it at the beginning of the first post.

@khicks thanks for the alternative syntax. It’s cute, and I have to confess I didn’t know it. However, for sake of showing the relationship between payloads and the state_value_template attribute more obvious, I left this part in its original form.

Hi all, many thanks for this topic.

Following an example with RGBW light for Shelly RGBW2 device with effect

mqtt:
  light:
    - name: "Light Hall 2"
      unique_id: light-hall2
      retain: false
      qos: 1

# Stato Online
      availability_topic: "shellies/light-hall2/online"
      payload_available: "true"
      payload_not_available: "false"

# ON/OFF
      command_topic: "shellies/light-hall2/color/0/command"
      state_topic: "shellies/light-hall2/color/0"
      payload_on: >-
        on
      payload_off: >-
        off

# RGBW
      rgbw_command_topic: "shellies/light-hall2/color/0/set"
      rgbw_command_template: >
        {
          "red": {{ red }},
          "green": {{ green }},
          "blue": {{ blue }},
          "white": {{ white }}
        }
      rgbw_state_topic: "shellies/light-hall2/color/0/status"
      rgbw_value_template: "{{value_json.red}},{{value_json.green}},{{value_json.blue}},{{value_json.white}}"

# BRIGHTNESS
      brightness_command_topic: "shellies/light-hall2/color/0/set"
      brightness_command_template: >
        {
          "gain": {{ value | float | multiply(0.3922) | round(0) }}
        }
      brightness_state_topic: "shellies/light-hall2/color/0/status"
      brightness_value_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"

# EFFECTS
      effect_command_topic: "shellies/light-hall2/color/0/set"
      effect_command_template: >
        {
        {% if value == "Off" %}
        "effect": "0"
        {% elif value == "Meteor Shower" %}
        "effect": "1"
        {% elif value == "Gradual Change" %}
        "effect": "2"
        {% elif value == "Flash" %}
        "effect": "3"
        {% endif %}
        }
      effect_state_topic: "shellies/light-hall2/color/0/status"
      effect_value_template: >-
        {% if value_json.effect == 0 %}
          Off
        {% elif value_json.effect == 1 %}
          Meteor Shower
        {% elif value_json.effect == 2 %}
          Gradual Change 
        {% elif value_json.effect == 3 %}
          Flash
        {% endif %}
      effect_list:
        - "Off"
        - "Meteor Shower"
        - "Gradual Change"
        - "Flash"

1 Like

In fact it’s even smarter approach than mine in regard of retrieving on/off state directly from color/0 topic and setting it in color/0/command

It makes impossible to control transition time from the yaml but it might be not needed since transition can be set in device settings too

Obviously at time I was working on my examples I wasn’t aware I can use state color/0 and color/0/command with the same values

BTW I like how you split the yaml into sections

Hi there all,
and sorry if I write in this thread with a little problem.
I must change all mqtt (in order to do it with the new configuration for the last HA update)
I have a shelly that now I put it on the file switch.yaml under mqtt integration


### Shelly - camera da letto
  light:
    - name: luce camera letto
      schema: template
      command_topic: "shellies/shellydimmer/light/0/set"
      state_topic: "shellies/shellydimmer/light/0/status"
      command_on_template: >
        {"turn": "on"
          {% if brightness is defined %}
            , "brightness": {{ brightness | float | multiply(0.3922) | round(0) }}
          {% endif %}
        }
      state_template: "{%- if value_json.ison %}on{% else %}off{% endif %}"
      brightness_template: "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"
      command_off_template: '{"turn":"off"}'
      qos: 0
  

It give me Configuration invalid!:
“Invalid config for [mqtt]: [light] is an invalid option for [mqtt]. Check: mqtt->mqtt->switch->13->light. (See /config/configuration.yaml, line 46).”

I still can’t understand in witch format I must write it
Anyone can help me please ?
Thanks a lot
Denis

Seems like you put the light in the switch section of mqtt/switch

Thanks for the help… but can you tell me please where I can put it ?
Maybe have you e piece of initial code please ?
Thanks again

it’s a light, it goes in the mqtt light section

mqtt:
  light:

vs

mqtt:
  switch:
    light:

so all together…

mqtt:
  switch:
    ...
  light:
    ...
  sensor:
    ...

etc

1 Like

YESSSSS !!!
it was so simple :slight_smile:
I jut made e new file (like switch.yaml) with name light.yaml
and it’s work

Thanks so much for the help.
Denis

Hi - I’ve used this code for my shelly RGBW2 but in Home Assistant it’s still showing as “unavailable”. Any ideas?

Below is what’s coming from the MQTT Explorer

{"ison":false,"source":"http","has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"mode":"color","red":254,"green":125,"blue":38,"white":0,"gain":45,"effect":0,"transition":0,"power":0.00,"overpower":false}

it’s like the availability_topic isn’t be set? I have the most recent HA and Firmware on the shelly.

please post your exact yaml as well as all topics published by your rgbw2 (use mqtt explorer for this)

I used the same code as above. Here’s what’s in the mqtt.yaml


light:
  - name: "Lounge LEDs"
    unique_id: lounge-leds
    retain: false
    qos: 1
    # Online
    availability_topic: "shellies/shellyrgbw2-E747F2/online"
    payload_available: "true"
    payload_not_available: "false"

    # ON/OFF
    command_topic: "shellies/shellyrgbw2-E747F2/color/0/command"
    state_topic: "shellies/shellyrgbw2-E747F2/color/0"
    payload_on: >-
      on
    payload_off: >-
      off

    # RGBW
    rgbw_command_topic: "shellies/shellyrgbw2-E747F2/color/0/set"
    rgbw_command_template: >
      {
        "red": {{ red }},
        "green": {{ green }},
        "blue": {{ blue }},
        "white": {{ white }}
      }
    rgbw_state_topic: "shellies/shellyrgbw2-E747F2/color/0/status"
    rgbw_value_template: "{{value_json.red}},{{value_json.green}},{{value_json.blue}},{{value_json.white}}"

    # BRIGHTNESS
    brightness_command_topic: "shellies/shellyrgbw2-E747F2/color/0/set"
    brightness_command_template: >
      {
        "gain": {{ value | float | multiply(0.3922) | round(0) }}
      }
    brightness_state_topic: "shellies/shellyrgbw2-E747F2/color/0/status"
    brightness_value_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"

    # EFFECTS
    effect_command_topic: "shellies/shellyrgbw2-E747F2/color/0/set"
    effect_command_template: >
      {
      {% if value == "Off" %}
      "effect": "0"
      {% elif value == "Meteor Shower" %}
      "effect": "1"
      {% elif value == "Gradual Change" %}
      "effect": "2"
      {% elif value == "Flash" %}
      "effect": "3"
      {% endif %}
      }
    effect_state_topic: "shellies/shellyrgbw2-E747F2/color/0/status"
    effect_value_template: >-
      {% if value_json.effect == 0 %}
        Off
      {% elif value_json.effect == 1 %}
        Meteor Shower
      {% elif value_json.effect == 2 %}
        Gradual Change 
      {% elif value_json.effect == 3 %}
        Flash
      {% endif %}
    effect_list:
      - "Off"
      - "Meteor Shower"
      - "Gradual Change"
      - "Flash"

And this is the output from the MQTT Explorer

I cannot see online topic on your screenshot.
what version of rgbw2 firmware you use?

you can comment the line witg online topic (add # at line beginning) to disable availability check

Thanks - I did wonder about that one. I’ve removed it and restarted HA. The light is now showing as “off”.

The firmware is 20220809-124336/v1.12-g99f7e0b which says it’s the latest.

Removing that online check has made it work. Thank you.