Mqqt json and shelly RGBW2

Hi Ron, I’m trying to use your config but I’m having an issue. HomeAssistant is just sending the “ON” command on the command_topic instead of the JSON data. When changing color/brightness from the Shelly app, no data is coming into HASS. I’m running HASS 0.81.1, maybe the template schema was not released in this version? Which HASS version are you running?

Hi David

Yes you need to be at n 86 as a minimum as far as I remember ( I needed to upgrade as well and on 87 now)
Before 86 there was a mqtt_template component but I never got that completely working, only on and off…
Good luck

1 Like

Hi Ron, okay, than I know what I will be doing this evening. :grin:

Got it all working in HomeAssistant 0.88.1 with these settings to control a RGBW LEDstrip.

The effect does indeed not work, this has something to do with the shelly firmware. No workaround at the moment.

  - platform: mqtt
    schema: template
    name: "LEDstrip MQTT"
    command_topic: "shellies/shellyrgbw2-<device_id>/color/0/set"
    state_topic: "shellies/shellyrgbw2-<device_id>/color/0/status"
    availability_topic: "shellies/shellyrgbw2-<device_id>/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

I also tried adding the availability topic, but the problem is that after a HASS restart the online MQTT message in not retained or resend periodically by the Shelly RGBW2. I ended up using an automation which forces an update for all shellies:

  - alias: Startup - Force Shellies update
    hide_entity: True
    trigger:
      - platform: homeassistant
        event: start
    action:
      - delay: '00:01:00'
      
      - service: mqtt.publish
        data:
          topic: "shellies/command"
          payload: "update"

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.