Mqtt value template

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