[SOLVED] So why state is not registered?

On UI Light-switch does not correspond to the state of the light - so HA does not “see” the right status. Why is that?

Config:

light:
  - platform: mqtt
    name: "light_front"
    command_topic: "cmnd/sonoff_front/POWER"
    state_topic: "stat/sonoff_front/RESULT"
    brightness_command_topic:   "cmnd/sonoff_front/Dimmer"
    brightness_state_topic:     "stat/sonoff_front/RESULT"
    on_command_type:            "brightness"
    brightness_value_template:  "{{ value_json.Dimmer }}"
    state_template:             "{{ value_json.POWER }}"
    payload_on:                 "ON"
    payload_off:                "OFF"
    brightness_scale:           100
    qos:                        1
    retain:                     true

MQTT traffic:

cmnd/sonoff_front/Dimmer 100
stat/sonoff_front/RESULT {"POWER":"ON","Dimmer":100}
cmnd/sonoff_front/POWER OFF
stat/sonoff_front/RESULT {"POWER":"OFF"}
stat/sonoff_front/POWER OFF

For some reason, for this component the state_template is actually state_value_template

Try

light:
  - platform: mqtt
    name: "light_front"
    command_topic: "cmnd/sonoff_front/POWER"
    state_topic: "stat/sonoff_front/RESULT"
    brightness_command_topic:   "cmnd/sonoff_front/Dimmer"
    brightness_state_topic:     "stat/sonoff_front/RESULT"
    on_command_type:            "brightness"
    brightness_value_template:  "{{ value_json.Dimmer }}"
    state_value_template:             "{{ value_json.POWER }}"
    payload_on:                 "ON"
    payload_off:                "OFF"
    brightness_scale:           100

Cool! Works. But one more issue… When I just set power on in the UI, in the log I get:

Jan 30 19:24:03 hassbian hass[9864]: 2018-01-30 19:24:03 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_subscribe.<locals>.async_mqtt_topic_subscriber('stat/sonoff_front/RESULT', b'{"POWER":"OFF"}', 0) at /srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/mqtt/__init__.py:213
Jan 30 19:24:03 hassbian hass[9864]: Traceback (most recent call last):
Jan 30 19:24:03 hassbian hass[9864]:   File "/usr/lib/python3.5/asyncio/events.py", line 126, in _run
Jan 30 19:24:03 hassbian hass[9864]:     self._callback(*self._args)
Jan 30 19:24:03 hassbian hass[9864]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/mqtt/__init__.py", line 232, in async_mqtt_topic_subscriber
Jan 30 19:24:03 hassbian hass[9864]:     hass.async_run_job(msg_callback, dp_topic, payload, dp_qos)
Jan 30 19:24:03 hassbian hass[9864]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 253, in async_run_job
Jan 30 19:24:03 hassbian hass[9864]:     target(*args)
Jan 30 19:24:03 hassbian hass[9864]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/mqtt.py", line 241, in brightness_received
Jan 30 19:24:03 hassbian hass[9864]:     device_value = float(templates[CONF_BRIGHTNESS](payload))
Jan 30 19:24:03 hassbian hass[9864]: ValueError: could not convert string to float:

Seems like I need some kind of template where Dimmer-value can be undefined… But I don’t know how to do that.

1 Like

seems like its refering to the payload on/off. maybe they should be integers ranging from 0 to 255.

This does the trick

light:
  - platform:                   mqtt
    name:                       "front"
    command_topic:              "cmnd/sonoff_front/POWER"
    state_topic:                "stat/sonoff_front/RESULT"
    brightness_command_topic:   "cmnd/sonoff_front/Dimmer"
    brightness_state_topic:     "stat/sonoff_front/RESULT"
    on_command_type:            "brightness"
    brightness:                 1
    brightness_value_template:  "{% if value_json.Dimmer%}{{ value_json.Dimmer }}{% else %} 0 {% endif %}"
    state_value_template:       "{{ value_json.POWER }}"
    brightness_scale:           100
    qos:                        1
    retain:                     false
1 Like