MQTT lights brightness error

Since latest 0.66 update I get the following error if I change my mqtt light status.
But lights are working…

Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 647, in _mqtt_handle_message
    msg.topic, payload, msg.qos)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/core.py", line 248, in async_run_job
    target(*args)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/light/mqtt.py", line 247, in brightness_received
    device_value = float(templates[CONF_BRIGHTNESS](payload))
ValueError: could not convert string to float: 

Tried to restart my mqtt module and my mosquitto service but no luck.

Anyone with the same problem ?

Having the same issue with a GE Wall Socket Dimmer. I’m using zwave2mqtt. I tried used the auto discovery within zwave2mqtt, but the dimmer wasn’t behaving correctly so I used the code below:

light:
  - platform: mqtt
    name: "Living Room Dimmer"
    command_topic: "zwave2mqtt/LivingRoom/DimmerPlug/38/1/0/set"
    brightness_command_topic: "zwave2mqtt/LivingRoom/DimmerPlug/38/1/0/set"
    brightness_scale: '99'
    brightness_state_topic: "zwave2mqtt/LivingRoom/DimmerPlug/38/1/0"
    on_command_type: "brightness"
    payload_off: '0'

Functionally, it’s working. But every time I hit on / off I get the following error:

2020-05-18 14:36:55 ERROR (MainThread) [homeassistant.util.logging] Exception in brightness_received when handling msg on 'zwave2mqtt/LivingRoom/DimmerPlug/38/1/0': '{"value_id":"4-38-1-0","node_id":4,"class_id":38,"type":"byte","genre":"user","instance":1,"index":0,"label":"Level","units":"","help":"The Current Level of the Device","read_only":false,"write_only":false,"min":0,"max":255,"is_polled":false,"value":23,"lastUpdate":1589826864575}'
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_basic.py", line 328, in brightness_received
    device_value = float(payload)
ValueError: could not convert string to float: '{"value_id":"4-38-1-0","node_id":4,"class_id":38,"type":"byte","genre":"user","instance":1,"index":0,"label":"Level","units":"","help":"The Current Level of the Device","read_only":false,"write_only":false,"min":0,"max":255,"is_polled":false,"value":23,"lastUpdate":1589826864575}'

If you have this:

brightness_state_topic: "zwave2mqtt/LivingRoom/DimmerPlug/38/1/0"

then Home Assistant expects to receive a payload containing an integer value between 0 and 255.

However, the payload it received contains a long JSON string:

{
	"value_id": "4-38-1-0",
	"node_id": 4,
	"class_id": 38,
	"type": "byte",
	"genre": "user",
	"instance": 1,
	"index": 0,
	"label": "Level",
	"units": "",
	"help": "The Current Level of the Device",
	"read_only": false,
	"write_only": false,
	"min": 0,
	"max": 255,
	"is_polled": false,
	"value": 23,
	"lastUpdate": 1589826864575
}

That’s why the error message indicates:

could not convert string to float

It could not convert that long string into a floating-point value.

Add a brightness_value_template to extract the brightness level from the received JSON string. From what I can see in the data, it is represented by the key named value.

    brightness_value_template: "{{value_json['value']}}"
1 Like

That fixes the error, thanks! I’m pretty new at this and finding it slightly more complicated than I was expecting, even with an IT background. I don’t understand why the predefined json in zwave2mqtt doesn’t work right when using auto discovery. The brightness value doesn’t seem to be retained when I turn the light off / on. Adding a state_topic to this configuration is what seems to make it not work as expected which is what was in the original json in zwave2mqtt.

I don’t think I can mark this as solved since it was someone else’s thread, but I appreciate the assistance.

1 Like

That’s why it’s preferable to create a new topic rather than append it to an existing one, especially not one that’s over 2 years old.

You’re welcome!

Noted… first time posting in this forum and I didn’t want to post the same problem that I found elsewhere. Thought that might be more frowned upon. Also, didn’t notice it was two years old until I clicked submit.

Sorry to resurect this old post but I just had to stop by and say thanks to you! I have been pulling my hair out for the last few hours trying to get my MQTT Light, via TASMOTA, to work nicely with Home assistant. There is just so much mixed information out here and YOU were the one that finally got me what I needed. I was able to get it working SORTA up to this point. I could turn the light on and control the brightness with HA but if I turned the light on without HA, like via the TASMOTA web interface, HA would not update the state. Well, rather, if I turned it off, but then adjusted the slider on the web interface, HA would not see it turn on. MANY posts would show the brightness_value_template as
brightness_value_template: "{{ value_json.Dimmer }}" but home-assistant.log was saying it was not formatted correctly. YOU were the only person I have seen to show it as "{{value_json['value']}}"
From that I was able to get both the dimmer value AND the power state of the light to show in HA correctly.

Heres what I ended up using. Hopefully this helps somebody else lol. THANKS AGAIN!

mqtt:
  light:
    - name: "Under Cabinet Light"
      command_topic: "cmnd/kitchenmulti/POWER2"
      availability_topic: "tele/kitchenmulti/LWT"
      on_command_type: "brightness"
      brightness_command_topic: "cmnd/kitchenmulti/Dimmer"
      brightness_scale: 100
      state_topic: "stat/kitchenmulti/RESULT"
      state_value_template: "{{ value_json['POWER2'] }}"
      brightness_state_topic: "stat/kitchenmulti/RESULT"
      brightness_value_template: "{{value_json['Dimmer']}}"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      payload_available: "Online"
      payload_not_available: "Offline"
      retain: false
1 Like

You’re welcome! Glad to hear it was still useful information.

Out of curiosity, is there a reason you’re not using Tasmota’s MQTT Discovery method with Home Assistant’s Tasmota integration? It spares you the work of configuring the light entity in YAML.