Mqqt json and shelly RGBW2

Post your configuration for light.armadio_1.

Thanks… Solved…

I would like to add RGBW2 2 settings to my home assistant file but I have no clue how can I do that , would you help me and tell me what I have to paste to my config file ?? I have read this topic but did not help enough

Start with this and then modify the state_topic and command_topic so it matches what you are using.

  - platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
    brightness_template: "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"
    command_topic: "shellies/shellyrgbw2-2B90ED/white/0/set"
    command_on_template: '{"turn":"on", "brightness":{{brightness | float | multiply(0.3922) | round(0)}}}'
    command_off_template: '{"turn":"off", "brightness":0}'

for Color Leds this works apart from the effect_list

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

In this situation, it’s inadvisable to use retain: true.

It instructs Home Assistant to publish retained messages to the command_topic which makes the MQTT broker retain a copy of the command message.

The resulting behavior is not always desirable. If either the MQTT Broker or RGBW2 disconnect/reconnect, the RGBW2 receives the retained command again. This can produce unwanted behavior such as lights turning themselves on seemingly unexpectedly.

Sometimes this is considered to be a desired behavior. Effectively, the device is restored to its last-known state after restarting. However, wherever possible, this should not be managed via the MQTT Broker (via retained messages) but by the device itself (for example, Tasmota lets you specify the device’s power-on behavior).

The rule of thumb is:

  • Publish messages to state_topics using retain=true.
  • Publish messages to command_topics using retain=false.

this one works great with color change so thanks a lot for your help

I have succeded with color settings but on other place I need settings for white led but to control at the same time 2 led strips on 2 different port

You need to do that via an automation as it will be 3 seperate MQTT lights

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”?