Shelly RGBW2 automations MQTT

Hello, I was wondering which code I have to use for Shelly RGBW2 automations through MQTT

I used this code for adding the LED in HomeAssistant, but which variables do I use to change the brightness value or color? What would the ‘action’ part of an automation look like?

  - platform: mqtt
    schema: template
    name: "LED 1"
    command_topic: "shellies/shellyrgbw2-XXXXX/color/0/set"
    state_topic: "shellies/shellyrgbw2-XXXXX/color/0/status"
    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

Are you able to control the light correctly through the Home Assistant interface ?

1 Like

Sure, it all works perfectly, but I don’t know the syntax for RGBW2 automations.

I jus want to create automations, like when there is movement, there is more brightness, when it’s after 10PM, less brightness, when I watch a movie, the LED gives a different colour, etc

1 Like

The Developer Tools are your friend !!

Turn on your light and set to the desired brightness and colour.
Go to the Developer Tools / States page.
Select the entity_id of your light and bask in the glory of all its attributes.

Select and copy all of the state attributes.

Now go to the Developer Tools / Services page, select light.turn_on from the service drop down box and your lights entity_id from the entity drop down box.

Paste the attributes you copied into the service data field.
Press the CALL SERVICE button and watch for any errors at the bottom of the screen.

They disappear quite quickly so don’t be afraid to spam the call service button until you understand the error.
In my case I was getting errors about too many colour keys, the friendly_name and supported_feature keys, and some others.
I deleted them one by one until there were no more errors and the light turned on.

Select and copy all of your now valid service data.

In the automation editor at the action you want to turn on your light, fill the appropriate info for Action Type, Service and Entities, and paste your service data under Service Data

Congratulations !!
You can now automate your RGBW light to any colour and brightness you’d like :upside_down_face:

:+1:

3 Likes

Thank you very much! This solves my problem!
These are the variables you can use for automating a Shelly RGBW2:

Service data:
brightness: 75
rgb_color: 
- 255
- 95
- 207
white_value: 0
effect: 0
entity_id: light.led_1
1 Like

THANK YOU !

1 Like