Mqqt json and shelly RGBW2

Thanks David for Posting your config. I have done the same for the availability topic. Wonder though is it will change the status when they become unavailable again due to a power issue… Need to still test this

I do see some errors in Hass to do with extraction of the json response. Haven’t looked into this yet as had no time… do you see the same?

Hi Ron, can’t see any errors in my logfile. Can you post some of the errors? Maybe I have a different logging config.

sure will do but away from home ATM
Anyway I see you have used white_value_template and I had for example blue_template… Not sure how the two are different but maybe there is something

Hello,

I tried your example code for the shelly rgbw2. Everything is working except the effects. The effects are working over the webinterface from the shelly.

This is my code:

- platform: mqtt
  schema: template
  name: "LEDstrip MQTT"
  command_topic: "shellies/shellyrgbw2-5A3BB8/color/0/set"
  state_topic: "shellies/shellyrgbw2-5A3BB8/color/0/status"
  availability_topic: "shellies/shellyrgbw2-5A3BB8/online"
  payload_available: "true"
  payload_not_available: "false"
  effect_list:
    - 0
    - 1
    - 2
    - 3
    - 4
    - 5
    - 6
  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_value is defined -%}
    , "white": {{ white_value }}
    {%- endif -%}
    {%- if effect is defined -%}
    , "effect": {{ effect }}
    {%- endif -%}
    }
  command_off_template: '{"turn":"off"}'
  state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
  brightness_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
  red_template: '{{ value_json.red }}'
  green_template: '{{ value_json.green }}'
  blue_template: '{{ value_json.blue }}'
  white_value_template: '{{ value_json.white }}'
  effect_template: '{{ value_json.effect }}'
  qos: 2

Using the command_on_template you have defined, when you change the effect (using the Lovelace UI) this is the payload the template produces:

{"turn": "on", "effect": 4}

Maybe that’s not enough information for the RGBW2? Perhaps it also needs the RGB values as well? I don’t have an RGBW2 so I can’t answer those questions. However, you can easily experiment with the payload’s contents by using Home Assistant’s MQTT Publish page.

MQTT%20Publish%20RGBW2

It’s faster to use the Publish page to experiment with the payload. Assuming you hit upon the magic combination that makes it work, you can then modify the template to produce the same payload format.

When using this config I am unable to turn the channels on using the regular switch. If I manually set the brightness to 100% it turns on as it should. Any idea how to fix this?

I don’t understand your statement. What are you referring to when you say “channels”?

I am referring to the “white mode”, where you can have four separate channels controlling four strips individually, instead of the RGBW-mode. Sorry for being unclear :slight_smile:

No need to apologize. I don’t own one of these devices so I am unfamiliar with its terminology and capabilities.

I can’t perform any experiments to help find an answer to your question. All I can do is suggest the procedure I would use to solve the problem:

  • Use an MQTT client to observe the status payload when the RGBW2 is operated manually.
  • The contents of the status payload may give you some clues for what the RGBW2 needs to receive as a command.
  • You can then use MQTT Publish to send these commands (as a JSON string) and confirm the RGBW2 operates as expected.
  • The last step is to modify the command_on_template to produce the correct commands as a JSON string.

Alternately, someone who owns an RGBW2 can answer your question. :slight_smile:

Hi,

I have experimented with the effect function on the Shelly RGBW2 over MQTT, I have send all the necessary json data over mqtt to the Shelly. However it seems to be an issue with the Shelly firmware, it’s looks like it is ignoring the effect function.

I posted the question in the Shelly support group on Facebook.

Kind regards,
David

Thanks David. I do not have a Facebook account :smiley:. Hopefully they will fix it. I also tried many different combinations but none of them did changed the effect.

Best regards,

Robin

I’m using the following code with two Shelly RGBW2’s to control two strings of white LEDs each on channels 0 and 1:

  - platform: mqtt
    schema: template
    name: basement_north_whiteLED1
    command_topic: "shellies/shellyrgbw2-5A3BB3/white/0/set"
    state_topic: "shellies/shellyrgbw2-5A3BB3/white/0/status"
    state_template: '{%- if value_json.ison==1 %}on{% else %}off{% endif %}'
    command_on_template: '{"turn":"on",{%- if brightness is defined -%}"brightness":{{ (brightness|float * 0.3922)|round(0) }}{%- else -%}"brightness":100{%- endif -%} }'
    command_off_template: '{"turn":"off"}'
    brightness_template: '{{ value_json.brightness | float * 2.55 | round(0) }}'
    optimistic: false
    qos: 1

I started with 123’s code posted above (thanks for that, btw!) but it wasn’t handling brightness at all for me so I did some tweaking. This code improved the situation: I can now properly control brightness when the light is on. However it’s not perfect yet because it doesn’t turn the lights back on at the previous level, even though I can see in MQTT that the it is still stored. As a fallback, this will turn the lights back on at 100%. I’m sure this is due to my inexperience with JSON– this is literally my first attempt at using it.

I’m interested to see how others will improve on this. Thanks in advance!

1 Like

Good job!

I offer this minor refinement to help improve clarity (i.e. it doesn’t work any better than what you already have). I’ve tested and it works in the Template Editor (you can copy the two lines into the editor and confirm it for yourself). However, the ultimate test is if it works when part of the RGBW2’s configuration.

    command_on_template: >-
      {% set x = 100 if brightness is not defined else (brightness|float * 0.3922)|round(0) %}
      {"turn":"on", "brightness":{{x}}}

For this refinement, I’m not 100% certain that single-quotes are needed to delimit the last line. You’ll know soon enough by either examining the payload’s appearance using an MQTT client or if the RGBW2 fails to accept the command. If it fails, delimit the last line with single-quotes.

1 Like

The MQTT effect command works now with the latest firmware. As well as smooth transitioning.

Thx for info. I could successfully test it :ok_hand:

Here’s the final, fully functional version:

  - platform: mqtt
    schema: template
    name: basement_north_whiteLED1
    command_topic: "shellies/shellyrgbw2-ABC123/white/0/set"
    state_topic: "shellies/shellyrgbw2-ABC123/white/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: 1

That I finally figured out the retained brightness level on the same day Robert Tait’s video review and matching code dropped on YouTube is a complete coincidence. :grin:

Thanks again to all who helped with this. My crown moulding is looking great.

It is here…

Can anyone adapt it to the shellybulb though?

Working off of this config I’m now stuck.

What do I need to publish, and to what, to control the colours and effects?

How are you configuring the HA front end to make this device easily useable?

Any yaml shares will be much appreciated.

I just updated to HomeAssistant 0.94.1 and I’m having a little issue with my Shelly RGBW2 modules.

When I’m sending this command, HomeAssistant sends out the MQTT message {“turn”:“off”} to shellies/shellyrgbw2-2B9246/color/0/set

- service: light.turn_on
  entity_id: light.ledstrip_office_mqtt
  data:
    rgb_color: [0,0,0]
    brightness: 0
    white_value: 150
    effect: 0

Before the update (version 0.90.1) this worked like expected, now it’s broken.

It seems to be something with “brightness”:“0”, when removing this line, the LEDstrip turns on, but with the RGB reverting to previous setting.

Sending “brightness”:“1” instead of 0 seems to fix the problem for now.

Was there a (breaking) change made to the mqtt light component?

My RGBW2 config:

light:
  - platform: mqtt
    schema: template
    name: "LEDstrip Office MQTT"
    command_topic: "shellies/shellyrgbw2-2B9246/color/0/set"
    state_topic: "shellies/shellyrgbw2-2B9246/color/0/status"
    availability_topic: "shellies/shellyrgbw2-2B9246/online"
    payload_available: "true"
    payload_not_available: "false"
    effect_list:
      - 0
      - 1
      - 2
      - 3
    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_value is defined -%}
      , "white": {{ white_value }}
      {%- endif -%}
      {%- if effect is defined -%}
      , "effect": {{ effect }}
      {%- endif -%}
      }
    command_off_template: '{"turn":"off"}'
    state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
    brightness_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
    red_template: '{{ value_json.red }}'
    green_template: '{{ value_json.green }}'
    blue_template: '{{ value_json.blue }}'
    white_value_template: '{{ value_json.white }}'
    effect_template: '{{ value_json.effect }}'
    qos: 2
    retain: false

Hi David,

Under the schema: template I don’t see any configuration to handle the red_template blue_template or green_template but rather rgb_command_topic. Your problem may lie within here.

I have a RGBW2 working and I’ll upload my config for you later. I have each of the RGBW set up as different lights so I have control for each separately.

David