Problem with MQTT dimmer light switch co-ordination between on, off, and brightness

Ok again I need some help. This time it is with a in-wall, mains powered, zwave dimmer switch that is a device in Homeseer and I am trying to control via MQTT.

Pertinent information is as follows:
HASSOS: 4.12
Home Assistant Core: 0.114.0
Platform: Raspberry Pi 3 B+
Mosquitto MQTT add-on: 5.1
Node-Red add-on: 7.0.0

configuration.yaml contains the following line
light: !include lights.yaml

In my lights.yaml file I have the following two tries at this:

# Dining Room Light Switch
# Homeseer Node: 13
# Ref ID: 28 Name: Dining Room Light

  - platform: mqtt
    schema: template
    name: "Dining Room Light"
    unique_id: "DiningRoomLight"
    state_topic: "CRUD-06/mcsMQTT/Dining_Room/Lighting/Dining_Room_Light"
    command_topic: "CRUD-06/mcsMQTT/Dining_Room/Lighting/Dining_Room_Light/Control"
    command_on_template: '{{ "99" | replace("99","on") }}'
    command_off_template: '{{ "0" | replace("0","off") }}'
    brightness_template: '{{ range(1,98) }}'
    qos: 0
    optimistic: false

# Kitchen Light 1
# Homeseer Node: 37
# Ref ID: 1086 Name: Kitchen Light 1

  - platform: mqtt
    name: "Kitchen Light 1"
    payload_on: "99"
    payload_off: "0"
    unique_id: "KitchenLight1"
    brightness_scale: "99"
    on_command_type: brightness
    brightness_state_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1"
    brightness_command_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1/Control"
    state_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1"
    command_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1/Control"
    optimistic: false

In the 2nd instance of the above for “Kitchen Light 1”, the switch in the UI that is displayed so you can control the light for on and off works. If I use the slider provided in the pop-up it is not co-ordinated. I can control brightness but the switch state is not co-ordinated. Therefore if I set the slider to say 50% brightness it physically sets the light to 50% the light bulb icon shows a “dimmed” version, but the switch in the UI shows off. If I slide the slider all the way down it never goes to “0” which would be off and if I slide it all the way up to “99” then the UI switch state changes to on.

So the 1st instance above for “Dining Room Light” was my attempt to try and get a co-ordination between the brightness and on/off states in the UI. I get a different icon for the on/off states. I get a lightning bolt instead of the simple slider switch. I can turn the light on and off but I cannot set a “dim” or “brightness” setting. The slider in the popup is not co-ordinated with the switch state. In testing from a light off state (both physically and in the UI) I set the slider to 50. The light comes on to full brightness (99). If I move the slider to “0” nothing happens. If I use the icons to turn the light off it turns off.

Homeseer publishes the following:
“0” for both control and status = off
values of 1 to 98 for both control and status = dim or brightness
“99” for both control and status = on

there is one other control status pair it is:
“255” which equals “Last” which is the last setting the switch was at. So if the brightness had been set to 50% and then you turned the switch off then subsequently turned it back on by sending “255” it would turn the light on to the last brightness setting of 50%.

I am not worried about this last state (“255”) if I could get the on/off/brightness co-ordinated.

Thanks in advance for any help or advice that might be coming my way.

Chuck

The situation is complicated by the fact brightness_state_topic and state_topic are subscribed to the same topic.

state_topic expects to see one of two values: something that represents on and something that represents off. You specified 0 for off and 99 for on. That’s fine except when you adjust the brightness to 50, that value is used for brightness and state. 50 as a state is neither 0 or 99 so the light’s state defaults to off (the toggle button slides to the off position).

Try the following version. It uses a state_value_template to convert the received brightness values (0-99) into either on (if value is greater than zero) or off (if value is zero).

  - platform: mqtt
    name: "Kitchen Light 1"
    unique_id: "KitchenLight1"
    brightness_scale: 99
    on_command_type: brightness
    payload_on: 'on'
    payload_off: 'off'
    brightness_state_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1"
    brightness_command_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1/Control"
    state_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1"
    state_value_template: "{{ 'on' if value|int > 0 else 'off' }}"
    command_topic: "CRUD-06/mcsMQTT/Kitchen/Lighting/Kitchen_Light_1/Control"

@123 you are fast becoming my hero :laughing:. I can’t thank you enough for your help and advice with this. Just so you know I have been working on this for over 3 days off and on. I am not a programmer, just a retired hobbyist. I have been reading posts, jinja2 documents, etc etc trying to get this right on my own.

I have marked your post as the solution. Maybe someone will find it as useful for them as it is for me.

Again many thanks

Chuck

1 Like