Mqtt value template

I have a zwave dimmer that connects to HA via mqtt and I want make a switch that follows the brightness topic to switch to on at any brightness and to off at 0 brightness.

I have this so far:

  - platform: mqtt
    name: "test Switch"
    state_topic: "zwave2mqtt/2/38/1/0"
    command_topic: "zwave2mqtt/2/38/1/0/set"
    #availability_topic: "home/bedroom/switch1/available"
    value_template: “{% if value == 0 %}off{% else %}on{% endif %}”
    payload_on: "99"
    payload_off: "0"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

This works to turn the light on and off, but I am not getting the second desired effect. If turn the light on at the physical switch to a brightness greater than 0, I want the mqtt switch to change to ON.

Is this an MQTT Light or an MQTT Switch?

The light supports brightness, not the switch.

This is a mqtt switch…when I set this up as a light, I don’t get the on/off switch part because its a dimmer.

MQTT Switch doesn’t support a brightness topic.

When I set the light up as a mqtt light I don’t get and on off switch so that is what I’m trying to achieve.

I’m trying to use the brightness value to get HA to know if the switch is on or off.

Is there a way to do it as a mqtt light?

I don’t understand what you mean because all of my MQTT Lights appear in the Lovelace UI as toggle switches (on/off).

Screenshot%20from%202019-07-12%2012-16-51

Here is an example of the hallway light’s configuration (it’s an MQTT Light):

  - platform: mqtt
    name: "Hallway"
    state_topic: "premise/hallwaylight/powerstate"
    command_topic: "premise/command/hallwaylight/powerstate"
    payload_on: "1" 
    payload_off: "0"
    brightness_scale: 100
    brightness_state_topic: "premise/hallwaylight/brightness"
    brightness_command_topic: "premise/command/hallwaylight/brightness"
    on_command_type: 'brightness'

My switch doesn’t expose a power state topic like yours.

I am using zwave2mqtt and its a ge dimmer switch

What are the payloads sent via this topic:

zwave2mqtt/2/38/1/0

Are they simply numbers from 0 to 99? Where off=0 and on=99?

What kind of payloads does the GE switch expect to receive via this topic:

zwave2mqtt/2/38/1/0/set

Simply numbers from 0 to 99?

Yes
and
Yes

Try this configuration for an 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'

Its giving 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/

Add this to it:

    command_topic: 'zwave2mqtt/2/38/1/0/set'

No errors, but I’m not home. I cant see whats going on, I’ll report back later.

Seriously, thanks for the help.

Doesn’t work and isn’t able to turn the light on or off when “Test Light” switch is toggled and its default state is on.

Interesting enough, on the states page for your light, it shows a brightness attribute that follows the changes when I turn the light on/off from the physical switch.

Default state is on? That means it received a payload with a value other than 0 via the state_topic.

If it fails to control the light, that means the values it publishes to brightness_command_topic aren’t valid. The following line:

on_command_type: 'brightness'

means it should only use the brightness_command_topic for controlling the light.

Let’s try an experiment. We will reduce the configuration to the bare minimum. This should only be able to turn the light on and off.

  - 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'

I recommend you use an MQTT client, such as MQTT Explorer, to observe what Home Assistant publishes to zwave2mqtt/2/38/1/0/set when you turn the MQTT Light on and off.This information is needed otherwise it becomes very difficult for me to diagnose this further

That does not work to turn the light on or off, and the default state for the switch is ON.

The message published to zwave2mqtt/2/38/1/0 is 0 when the switch is toggled.

This is what I have that has a working UI switch, and dimmer slider. It just doesn’t get updates when the physical switch is used:

  - platform: mqtt
    name: "Kitchen Island Chandlier"
    state_topic: 'zwave2mqtt/2/38/1/0'
    command_topic: "zwave2mqtt/2/38/1/0/set"    
    brightness_command_topic: "zwave2mqtt/2/38/1/0/set"
    brightness_scale: 99
    brightness_state_topic: "zwave2mqtt/2/38/1/0"
    on_command_type: "brightness"
    payload_off: 0
    payload_on: 99

When providing someone with assistance, you have to assume everything they tell you and everything they share with you is a fact. For example, in the first example you provided, you used string values for payload_on and payload_off:

    payload_on: "99"
    payload_off: "0"

So all of my examples use string values. However, in your latest example, the one you claim that works, (which is almost identical to the one that I proposed) it uses integer values:

    payload_off: 0
    payload_on: 99

I’m no longer certain what is fact and what is fiction. For example, you stated this:

The message published to zwave2mqtt/2/38/1/0 is 0 when the switch is toggled.

What do you mean when you say ‘switch is toggled’? Did you flip the switch off/on/off? Did you simply turn it off? Saying it is 0 when ‘toggled’ isn’t at all clear to me. Please elaborate.

If the values are integers, and 0 is off and 99 is on, then this should minimally be able to turn the light on and off and display the light’s state:

  - 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

If it doesn’t then some detail you’ve been sharing isn’t a fact.

Sorry for the confusion, I just tested the payload_on and payload_off as an integer and string in the last example I posted that worked and both ways it functions the same.

Using your example:

  - 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

The “test light” switch defaults to on with the light off.
Switching “test light” off, the switch goes back to the on position by it self, and the light stays off.
If I switch “test light” off and switch back on before it does it self, the light turns on, and the switch says in the on position.
With the light on and the switch in the on position, switching “test light” off, the switch goes back to the on position by it self, and the light stays off.

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