Mqtt value template

The results you’ve provided indicate the messaging is not as simple as 0=off and 99=on. If it was, then the configuration you tested would work, yet it does not.

The “test light” switch defaults to on with the light off.

This is a problem. Because it is on it means:

  • It received a payload from zwave2mqtt/2/38/1/0
  • The payload was processed by state_value_template
  • The template determined the payload’s value is non-zero so it reported 99 which corresponds to payload_on.

Why does zwave2mqtt/2/38/1/0 contain a non-zero value when the actual light is `off``?
You need to use an MQTT client to subscribe to this topic and confirm what value it contains.

Switching “test light” off, the switch goes back to the on position by it self, and the light stays off.

  • Immediately returning to its previous state means “I sent the command to turn off but did not receive the expected response from the device so I am putting myself back to my previous state”.
  • It means it sent 0 but never received a reply containing 0.

For the next two steps, where you had some success turning the actual light on/off, implies “test light” was sending payloads that were understood by the actual light. However, the test light always had trouble with displaying the actual light’s status. This corresponds to the very first problem where the test light defaults to on, namely it has trouble getting the light’s correct status.

Here is what I would do:

  • Remove (or comment out) the ‘Test Light’ configuration from Home Assistant. Restart Home Assistant.
  • Turn off the light.
  • Connect MQTT Explorer to the MQTT broker.
  • Look at the payload in zwave2mqtt/2/38/1/0. It should be 0.
  • Turn on the light.
  • zwave2mqtt/2/38/1/0 should now contain 99.
  • Use MQTT Explorer to publish 0 to zwave2mqtt/2/38/1/0/set.
  • The light should turn off.
  • Look at the payload in zwave2mqtt/2/38/1/0. It should be 0.
  • Use MQTT Explorer to publish 99 to zwave2mqtt/2/38/1/0/set.
  • The light should turn on.
  • zwave2mqtt/2/38/1/0 should now contain 99.

If your test results don’t match what I’ve listed above, then the light’s behavior is not the way it was initially described.

Followed all test steps:

  • Commented out all lights for the node/light and restarted Home Assistant.
  • Connect MQTT Lens to MQTT broker (I’m using a chromebook so I can’t use MQTT Explorer)
  • Light off and the payload is 0.
  • Light on and the payload is 99.
  • Publish 0 or “0” to zwave2mqtt/2/38/1/0/set and the light turns off.
  • Publish 99 or “99” to zwave2mqtt/2/38/1/0/set and the light turns on.
1 Like

Good, that helps to confirm its operation. Let’s forget everything that’s been attempted so far and return to the basics with a simple MQTT Switch (not an MQTT Light).

Create this MQTT Switch:

  - platform: mqtt
    name: 'Test Switch'
    state_topic: 'zwave2mqtt/2/38/1/0'
    command_topic: 'zwave2mqtt/2/38/1/0/set'
    payload_on: 99
    payload_off: 0

It should work perfectly because it is simplicity itself: 0=off and 99=on. By default, MQTT Switch assumes the reported states are equivalent to payload_on and payload_off so there’s no need to specify state_on and state_off.

If it fails to work (you may try ‘99’ and ‘0’ … but you said both string and integer work), then I suspect there are deeper problems in your system.

Works perfectly.

On and off.
State updates to on and off correctly.
The state updates in the UI when the physical switch is used, both for on and off.

That’s encouraging news.

What happens if you turn off the zwave dimmer and then turn it on but only to 50 percent? Theoretically, the MQTT Switch should fail to indicate the light is on because it will receive 50 instead of 99.

Correct, the mqtt switch only indicates on if that switch is set to max brightness, 99.

OK, so let’s progress to this configuration:

  - platform: mqtt
    name: 'Test Switch'
    state_topic: 'zwave2mqtt/2/38/1/0'
    command_topic: 'zwave2mqtt/2/38/1/0/set'
    value_template: "{{ 0 if value == 0 else 99 }}"
    payload_on: 99
    payload_off: 0

If the MQTT Switch receives a value greater than 0 the template will covert it to 99. This permits the MQTT Switch to report its status as on for any value > 0.

I’m very interested to learn if this template works like I just explained or will cause the switch to indicate on by default.

Its back to previous behavior of defaulting to on, switching back to on after trying to the mqtt switch off. If I switch the mqtt switch to off and back on quickly, only then does the light turn on.

The template appears to behave like value is never equal to 0 so the template always reports 99.

This is a long-shot but try this template:

    value_template: "{{ 0 if value | int == 0 else 99 }}"

That works. The mqtt behaves and updates correctly on/off and triggers on when the switch is at 50% brightness.

That means value in the template is handled as a string. Therefore this:

{{ 0 if value  == 0 else 99 }}

always failed when the received value was zero (it was comparing a string to an integer).

Just to confirm it, try this version which handles everything as strings:

  - platform: mqtt
    name: 'Test Switch'
    state_topic: 'zwave2mqtt/2/38/1/0'
    command_topic: 'zwave2mqtt/2/38/1/0/set'
    value_template: "{{ '0' if value == '0' else '99' }}"
    payload_on: '99'
    payload_off: '0'

If it works, move on to the next step and create this MQTT Light:

  - platform: mqtt
    name: 'Test Light'
    state_topic: 'zwave2mqtt/2/38/1/0'
    state_value_template: "{{ '0' if value == '0' else '99' }}"
    payload_on: '99' 
    payload_off: '0'
    brightness_scale: 99
    brightness_state_topic: 'zwave2mqtt/2/38/1/0'
    brightness_command_topic: 'zwave2mqtt/2/38/1/0/set'
    on_command_type: 'brightness'

It’s exactly the same as what I proposed earlier but with one very important correction to the template. The previous version’s template was incorrect because it compared a string to an integer.

Test switch works.

Test light gives me this error:

Invalid config for [light.mqtt]: required key not provided @ data[‘command_topic’]. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/light.mqtt/

That’s good news.

Ouch! I forgot to include the command_topic (again).

Here it is with the command_topic:

 - platform: mqtt
   name: 'Test Light'
   state_topic: 'zwave2mqtt/2/38/1/0'
   state_value_template: "{{ '0' if value == '0' else '99' }}"
   command_topic: 'zwave2mqtt/2/38/1/0/set'
   payload_on: '99' 
   payload_off: '0'
   brightness_scale: 99
   brightness_state_topic: 'zwave2mqtt/2/38/1/0'
   brightness_command_topic: 'zwave2mqtt/2/38/1/0/set'
   on_command_type: 'brightness'
5 Likes

Works!

I’ve got an on/off switch.
Dimmer slider.
The UI updates to the changes made at the physical switch, on/off, and dim levels.

That’s great news!

Now I would appreciate it if you could mark my post as ‘Solution’ to make it easier for other users to find it (this thread has grown to over 32 posts in length). Only you, the author, can mark a post as ‘Solution’. It will automatically place a link to the Solution under your first post in this thread.

Thank you for the help.

I understand everything except the state_value_template, especially why the first ‘0’ before the “if” is needed.

This:

{{ '0' if value == '0' else '99' }}

means:
If the value is zero then report zero. Otherwise report ninety-nine.

It’s just a more compact way of performing an if-else test. For example, this:

{% if x == 100 %}
  It is 100.
{% else %}
 It is not 100.
{% endif %}

is the same as this:

{{ 'It is 100.' if x == 100 else 'It is not 100.' }}

Got it. Thanks.

1 Like

Thanks, was struggling with this exact same issue (Fibaro Dimmer 2 modules with Zwave2MQTT)
Thank you for your hard work, making my life a lot easier :smiley:

1 Like

Great post! I had exactly the same issue and was able to identify where I was going wrong.
What in the end was wrong in my configuration file is that I had the string values that were send for the state changes wrong. I used ‘0’ and ’ 100’, while I should have used ’ 0.00’ and ‘100.00’.
I have been looking for a few weeks to find a good step-by-step guide on how to identify errors. This post helped me a great deal understanding how mqtt/home assistant works…
Thanks a lot!

1 Like