Mqqt json and shelly RGBW2

I will try right now.

Maybe we do not understant each other. Now settings for brightness work normally.
On shellies/shellyrgbw2-2B90ED/white/0/set i get
{"turn":"on", "brightness":50}

But on
shellies/shellyrgbw2-2B90ED/white/0/status
i get
'{"ison":true,"mode":"white","brightness":50,"power":10.00,"overpower":false}'

When i refresh web UI, then entity goes to 0% (Light are still on), i assume that entity goes to 0 because they can not resolve status json {"ison":true,"mode":"white","brightness":50,"power":10.00,"overpower":false}

So we must change something in this line
state_template: '{{ value_json.ison }}'

When you say “refresh web UI” I assume you mean you are simply pressing F5, or similar, to cause the web browser to refresh the current web page. If that’s what you mean then I don’t understand why the UI changes from 50% to 0% (while the light is still on). The status message still contains ison:true and brightness:50 so all the necessary information is available and represents the correct status. Refreshing the browser should not change the reported brightness and I can’t explain why it is changing.

You can try replacing the state_template with this but I doubt it will improve anything.

state_template: >-
  {% set b = value_json.brightness | int %}
  {% if b > 0 %}
  "on"
  {% else %}
  "off"
  {% endif %}

You can also try changing brightness_template but I don’t think it will improve it:

    brightness_template: '{{ value_json.brightness | string }}'

It set back also if i only change change room at UI of homeassistant.
I have also notice that even if light is turn on i can not turn it of by buttonZajeta%20slika

Also when light is turn on i still not see that this button in ON.

If i press to turn it on than i get response: can not call this service

Also if i change brightness on web of shelly directly, home assistant does not sync data with shelly.

I think that homeassistant does not know what each of data mean on this topic

{"ison":true,"mode":"white","brightness":50,"power":10.00,"overpower":false}

But i have not enough brain to tell him :smiley:

If I make config like this (whitout state topic)

  - platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    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 }}'

Then also working after refresh web UI
But i get error un log every minute

Tue Feb 12 2019 20:20:31 GMT+0100 (CET)

Exception in state_received when handling msg on 'shellies/shellyrgbw2-2B90ED/white/0/status': '{"ison":false,"mode":"white","brightness":0,"power":0.00,"overpower":false}'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/mqtt/light/schema_template.py", line 193, in state_received
    state = self._templates[CONF_STATE_TEMPLATE].\
AttributeError: 'NoneType' object has no attribute 'async_render_with_possible_json_value'

I think you wanted to say “without state template”. The state topic clearly still exists in your configuration example.

That is an expected failure when there’s no state_template. Home Assistant expects a state to be returned by the state_topic. It is expecting on or off (or true or false) but not what the Shelly is sending which is a JSON message: {"ison":false,"mode":"white","brightness":0,"power":0.00,"overpower":false}

That’s why you need a state_template to convert the JSON message into a valid state understood by Home Assistant. The goal is to create a state_template that produces the correct conversion. I’ve provided you with two examples but you say they have not worked. Here is one more.

state_template: >-
  {% set b = value_json.brightness | int %}
  {% if b > 0 %}
  on
  {% else %}
  off
  {% endif %}

If it still doesn’t work, given the available documentation for the RGBW2, I can’t think of anything else to try. I don’t have this device so I can’t carry out experiments to see what does or does not work.

I recommend you use an MQTT client (I believe you are already using one) to observe the MQTT messages sent and received by the RGBW2. It may provide a clue to explain how to make it work properly with Home Assistant.

1 Like

Now this is much better, but it need one more correction.

When i turn light to 100%, then light turn on to 100%, but slider jump back to for example 35%, i think that this is because this line

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

And now also state template must be multiply whit some number?

What must i add to state topic to multuiply and i also do not know whit what number? I think that it is 2.54? but i think i am wrong?

The template responsible for converting the RGBW2’s brightness value (0-100) to Home Assistant’s brightness value (0-255) is brightness_template (and not the command_on_template which is responsible for the conversion in the opposite drection).

We must multiply the received brightness value by a factor of 2.55 (we are converting from the range of 0-100 to 0-255). Try this modified brightness_template:

    brightness_template: '{{ value_json.brightness | float | multiply(2.55) | round(0) }}'

For your convenience, I’ve combined all the suggestions I’ve made so far into the following configuration:

  - platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: "shellies/shellyrgbw2-2B90ED/white/0/status"
    state_template: >-
      {% set b = value_json.brightness | int %}
      {% if b > 0 %}
      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}'
2 Likes

I can confirm that now it working.

Thanx a lot for your help

1 Like

Here’s a refinement to make the configuration more compact. To determine the state it uses the RGBW2’s “ison” instead of “brightness”. If ison=true then state=on else state=off.

    state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"

Hi I am getting the following message:

Log Details (ERROR)
Sat Feb 16 2019 20:01:21 GMT+0100 (Ora standard dell’Europa centrale)
Error handling message: {‘type’: ‘call_service’, ‘domain’: ‘light’, ‘service’: ‘turn_on’, ‘service_data’: {‘entity_id’: ‘light.armadio_1’}, ‘id’: 17}
Traceback (most recent call last):
File “/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py”, line 140, in async_render
return self._compiled.render(kwargs).strip()
File “/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/usr/local/lib/python3.6/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/usr/local/lib/python3.6/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/usr/local/lib/python3.6/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
File “/usr/local/lib/python3.6/site-packages/jinja2/filters.py”, line 668, in do_float
return float(value)
jinja2.exceptions.UndefinedError: ‘brightness’ is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/local/lib/python3.6/site-packages/homeassistant/components/websocket_api/decorators.py”, line 17, in _handle_async_response
await func(hass, connection, msg)
File “/usr/local/lib/python3.6/site-packages/homeassistant/components/websocket_api/commands.py”, line 148, in handle_call_service
connection.context(msg))
File “/usr/local/lib/python3.6/site-packages/homeassistant/core.py”, line 1130, in async_call
self._execute_service(handler, service_call))
File “/usr/local/lib/python3.6/site-packages/homeassistant/core.py”, line 1152, in _execute_service
await handler.func(service_call)
File “/usr/local/lib/python3.6/site-packages/homeassistant/components/light/init.py”, line 287, in async_handle_light_on_service
await light.async_turn_on(**pars)
File “/usr/local/lib/python3.6/site-packages/homeassistant/components/mqtt/light/schema_template.py”, line 398, in async_turn_on
self._templates[CONF_COMMAND_ON_TEMPLATE].async_render(**values),
File “/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py”, line 142, in async_render
raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: ‘brightness’ is undefined

what should I do?
Thanks

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