Mqqt json and shelly RGBW2

Hi, i have shelly rgbw 2 that have spec for mqtt like this

Zajeta%20slika

I have manged to make it work if I set brightnes but ON or OFF button do not work.

I have configuration like this

  - platform: mqtt
    schema: json
    name: Pod pultom
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    command_topic: "shellies/shellyrgbw2-2B90ED/white/0/set"
    brightness: true
    brightness_scale: 100

If I add

payload_on: "ON"
payload_off: "OFF"

Then i get error in config.

What am I doing it wrong?

I have also found put that if i close web of hassio and turn back on. Status is wrong. Seams like something is wronk set at status line?

According to the documentation you posted, this topic:

shellies/shellyrgbw2-2B90ED/white/0/set

Accepts a JSON payload with two possible keys:

  • brightness sets output level 0 to 100
  • turn can be on or off, acts just like publishing to .../command

So it accepts two keys: brightness and turn.

I may be wrong about this (I don’t use the JSON schema version of MQTT Light) but according to the documentation for JSON schema it generates MQTT messages that look like this:

{
  "brightness": 255,
  "color_temp": 155,
  "color": {
    "r": 255,
    "g": 180,
    "b": 200,
    "x": 0.406,
    "y": 0.301,
    "h": 344.0,
    "s": 29.412
  },
  "effect": "colorloop",
  "state": "ON",
  "transition": 2,
  "white_value": 150
}

It uses the keys brightness and state whereas the Shelly uses brightness and turn. They use the same term for brightness but different ones for state (state vs turn). I think that’s why you can control the light’s brightness but can’t control its state (on/off).

I think that it must somemthing like that in config, but i get error :confused:

Invalid config for [light.mqtt]: [command_on_template] is an invalid option for [light.mqtt]. Check: light.mqtt->command_on_template. (See ?, line ?). Please check the docs at https://home-assistant.io/components/light.mqtt/

  - platform: mqtt
    schema: json
    name: Pod pultom
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    command_topic: "shellies/shellyrgbw2-2B90ED/white/0/set"
    command_on_template: "on"
    command_off_template: "off"
    brightness: true
    brightness_scale: 100

I believe you’re getting an error because you have the wrong schema type.

According to the documentation, command_on_template is supported by schema: template

Yes this is work now for on and off, but i can not set brightness.
I think there must be some easy way to set up this

I think that problem is because brightness is in JSON format but On and off is in “template”.

Also ON and OFF is send to shellies/shellyrgbw2-2B90ED/white/0/
but brightness is send to shellies/shellyrgbw2-2B90ED/white/0/set

It is also strange that status update do not work, maybe becase it need some settings because it is in JSON format? If i suscribe to it get status update like this

{"ison":false,"mode":"white","brightness":1,"power":0.00,"overpower":false}

I read the RGBW2’s documentation.

I said this earlier but I’ll say it again. The format of an RGBW2’s status and command is not the same as the format used by MQTT Light (schema: json). They both uses JSON format but the key names are not identical.

MQTT Light with schema: template allows you to use templates to convert the format of status and command messages. In other words, templates are used to convert Shelly’s JSON messages into the JSON format used by Home Assistant.

If this is the RGBW2’s status message:
{"ison":false,"mode":"white","brightness":1,"power":0.00,"overpower":false}
then you need a template to extract the brightness value and, because there’s no state available, you must extract the value for ison and use it to represent the status of state.

There are two examples in Home Assistant’s documentation showing how to use templates to convert status and command topics.

Ok. I will try but i did not make any template so i think i can not do this.
Thanx for help

I don’t own a Shelly RGBW2 so I can’t test anything for you. However, try this and see if anything works.

  - platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    state_template: '{{ value_json.ison }}'
    command_topic: "shellies/shellyrgbw2-2B90ED/white/0/set"
    command_on_template: '{"turn":"on", "brightness":{{brightness}}}'
    command_off_template: '{"turn":"off", "brightness":0}'
    brightness_template: '{{ value_json.brightness }}'
1 Like

Thanx a lot, now working brightnes and on off :smiley: i just must figure it how set up that status will work. For example, now if i set brightness to 50% and close web UI and open again i see 0% brightness. I think that there must be some change: state_template: '{{ value_json.ison }}' On mqtt i get this status
{"ison":false,"mode":"white","brightness":1,"power":0.00,"overpower":false}

I have found one more problem.
Whit this settings hassio sent brightnes up to 255, but shelly accept only to 100.

So at 33% i have full ON on shelly

I think you can use the brightness_scale and set it to 100 as mentionet here https://www.home-assistant.io/components/light.mqtt/

If i add brightness_scale then i get error in configuration. Then i have configuration like this

  - platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    state_template: '{{ value_json.ison }}'
    command_topic: "shellies/shellyrgbw2-2B90ED/white/0/set"
    command_on_template: '{"turn":"on", "brightness":{{brightness}}}'
    command_off_template: '{"turn":"off", "brightness":0}'
    brightness_template: '{{ value_json.brightness }}'
    brightness_scale: 100

Invalid config for [light.mqtt]: [brightness_scale] is an invalid option for [light.mqtt]. Check: light.mqtt->brightness_scale. (See ?, line ?). Please check the docs at https://home-assistant.io/components/light.mqtt/

you are right cause it’s a schema: template and not a json

Is there some solution?
If i set json schema then brightness and on off not working

Yes, the command_on_template and brightness_template have to be modified. They must convert from Home Assistant’s brightness scale (0-255) to the Shelly’s scale which I assume is 0-100? Or is it 1-100?

Assuming it’s 0-100 we need to multiply Home assistant’s values by 100/255 = 0.3922.

command_on_template: '{"turn":"on", "brightness":{{brightness | float | multiply(0.3922) | round(0)}}}'

See if you can figure out how to perform the reverse conversion for brightness_template.

1 Like

It is 0-100.
But if i add brightness_scale: 100 then i get error.

Where can i set scale?

@123
If it will be help to you i can forward port for mqtt (1883) and 80 from shelly and you can try at your hassio?

That’s not a valid option when you are using schema: template (read the documentation).

Yes, now scale is work well.
I(or meybe you) :slight_smile: must just find out what is correct state template.

Try the test again. Using Home Assistant, set the brightness to 50% (leave the UI open), then tell me the MQTT status message. It should report ison:true and brightness:50.