MQTT Light problems

Hi ALl,

Can someone tell me why this happen:

2020-05-13 18:04:37 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/0': '{"brightness":3,"level":1,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:37 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/1': '{"brightness":3,"level":1,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:37 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/2': '{"brightness":3,"level":1,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:37 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/3': '{"brightness":3,"level":1,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:38 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/0': '{"brightness":0,"level":0,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:38 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/1': '{"brightness":0,"level":0,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:39 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/2': '{"brightness":0,"level":0,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

2020-05-13 18:04:39 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on 'milight/states/0xAAAF/rgb_cct/3': '{"brightness":0,"level":0,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper
    msg_callback(msg)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 243, in state_received
    if values["state"] == "ON":
KeyError: 'state'

I tried to restart MQTT Add-On, also the Milight Hub, HA itself. But see this error many times now.

Removed all Retained MQTT msg, restart HA and now seems to be gone.
See those errors lots of times lately. Don’t know what it can be.

Regarding this message:

Exception in state_received when handling msg on ‘milight/states/0xAAAF/rgb_cct/3’: ‘{“brightness”:0,“level”:0,“color”:{“r”:255,“g”:255,“b”:255},“device_id”:43695,“group_id”:0,“device_type”:“rgb_cct”}’

It indicates it received this payload:

{"brightness":0,"level":0,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}

via this topic:

milight/states/0xAAAF/rgb_cct/3

and then somewhere it your code it was requested to do this:

if values["state"] == "ON":

However, if you look at the payload posted above, it does not contain a key called state so that’s why it produced this error:

KeyError: ‘state’

OK, thanks. So somewhere in have something wrong in the config? Thats what you wanna say?

What I’m saying is that when you created this:

if values["state"] == "ON":

you assumed the payload it received would always contain the key named "state". However, it appears that it’s sometimes possible for the payload to not have that key:

{"brightness":0,"level":0,"color":{"r":255,"g":255,"b":255},"device_id":43695,"group_id":0,"device_type":"rgb_cct"}

You may need to redesign how the payload is handled because it can’t simply assume the state key is always present.

Then I will turn on the debug logging for “homeassistant.util.logging” and see where I can find this.
Because I don’t know where to search. :slight_smile: sometimes im a noob haha

Don’t search for that specific line because that is python code in schema_json.py:

File “/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py”, line 243, in state_received
if values[“state”] == “ON”:

My guess is that you have an entity (an MQTT sensor or switch or light?) that is subscribed to:

milight/states/0xAAAF/rgb_cct/3

and it has a value_template. Find that entity and review how you configured it.

This is only I have 1 MQTT Light:

###########################################################################################
#
# MQTT LIGHT
#
# https://www.home-assistant.io/integrations/light.mqtt/
#
###########################################################################################
---
platform: mqtt
name: "Dressoir 3"
schema: json
command_topic: "milight/0xAAAF/rgb_cct/3"
state_topic: "milight/states/0xAAAF/rgb_cct/3"
qos: 1
optimistic: "false"
retain: "false"
brightness: "true"
rgb: "true"
color_temp: "true"
effect: "true"
effect_list: !include /config/package/templates/light_effects.yaml
availability_topic: "milight/client_status"
payload_available: "connected"
payload_not_available: "disconnected"

And here a path for group 0, When group 0 goes on or off then also the groups 1,2 and 3 get same state.

###########################################################################################
#
# PATCH WHEN GROUP 0 IS ON OR OFF THE CORRECT STATUS WILL SEND TO MILIGHT HUB FOR ALL LIGHTS INSIDE GROUP
#
###########################################################################################

- alias: "Patches - MiLight Dressoir Group 0"
  initial_state: "true"
  trigger:

    platform: mqtt
    topic: "milight/states/0xAAAF/rgb_cct/0"

  action:
    - service: mqtt.publish
      data_template:
        topic: "milight/states/0xAAAF/rgb_cct/1"
        payload_template: "{{ trigger.payload }}"

    - service: mqtt.publish
      data_template:
        topic: "milight/states/0xAAAF/rgb_cct/2"
        payload_template: "{{ trigger.payload }}"

    - service: mqtt.publish
      data_template:
        topic: "milight/states/0xAAAF/rgb_cct/3"
        payload_template: "{{ trigger.payload }}"

I never had this problem before. It worked for months till last week.

This:

schema: json #<---- Payload will be in JSON format
state_topic: "milight/states/0xAAAF/rgb_cct/3"  #<---- Topic supplying the payload

Look at the documentation for MQTT Light - JSON schema. It expects the received JSON data to contain a key called “state”. Here’s the example from the documentation:

{
  "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", #<---- This 'state' key is needed
  "transition": 2,
  "white_value": 150
}

Now look at the payload that was received:

{
	"brightness": 0,
	"level": 0,
	"color": {
		"r": 255,
		"g": 255,
		"b": 255
	},
	"device_id": 43695,
	"group_id": 0,
	"device_type": "rgb_cct"
}

There’s no state key in it and that’s what caused the error.

Thank you for the explanation. Ill go check this!

I deleted mosquito.db and now everything is working correct.

Those are likely to be just temporary solutions. The problem is that a malformed payload (without a state key) was received in the past and there remains a chance it will re-appear in the future.

The permanent solution is to determine why the malformed payload was generated and to correct it.

Thank you. I really don’t know how to check this.
This the playload when I send command turn on light via HA:

{
	"state": "ON",
	"status": "ON",
	"brightness": 26,
	"level": 10,
	"kelvin": 100,
	"color_temp": 370,
	"bulb_mode": "white",
	"color": {
		"r": 255,
		"g": 255,
		"b": 255
	},
	"effect": "white_mode"
}

And this is when I turn off the light via the Milight Hub:

{
	"state": "OFF",
	"status": "OFF",
	"brightness": 255,
	"level": 100,
	"kelvin": 69,
	"color_temp": 303,
	"bulb_mode": "white",
	"color": {
		"r": 255,
		"g": 255,
		"b": 255
	},
	"effect": "white_mode"
}

Thats why I don’t now where to search… Sorry for all the questions…

It’s the Milight Hub. Normally it sends what you posted above (contains a state key):

{
	"state": "OFF",  <---- Here is the state key.
	"status": "OFF",
	"brightness": 255,
	"level": 100,
	"kelvin": 69,
	"color_temp": 303,
	"bulb_mode": "white",
	"color": {
		"r": 255,
		"g": 255,
		"b": 255
	},
	"effect": "white_mode"
}

But sometimes it sends something like this that doesn’t contain a state key:

{
	"brightness": 0,
	"level": 0,
	"color": {
		"r": 255,
		"g": 255,
		"b": 255
	},
	"device_id": 43695,
	"group_id": 0,
	"device_type": "rgb_cct"
}

Whenever it sends a payload without a state key, that’s when Home Assistant reports the error.

I don’t know anything about the Milight Hub but that’s the source of the issue.

Thanks,

I will contact the developer of the Source code who build the software to control those lights.

Cheers

1 Like

Good idea.

Please mark my post (above) with the Solution tag so that other Milight Hub users (who may be experiencing the same problem) will have an easier time finding it.

1 Like