MQTT Dimmer using state_value_template works in the template tester but not in config

Any one have any ideas on getting a dimmer to work states? I tried using single quotes around numbers and not but doesn’t change anything. I have debug logging turned on but nothing appears to be an issue.

Export of the json used in homeassistant/light/dimmer-3f91181/config

{
    "name": "Master Bed Light Switch",
    "command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_value_template": "{ '0' if value == '0' else '100' }",
    "on_command_type": "brightness",
    "brightness_scale": 100,
    "payload_on": 100,
    "payload_off": 0
}
1 Like

Hey, I just helped someone else with this. Don’t put a dash in the state names. Or if you do (can’t change it)…ummm, there’s probably something else you have to do, but I’m not sure.

Luckily this is probably wrong!

That would be highly unfortunate if dashes aren’t supported. I’m using the homie library which uses dashes in the names. Do you know what the error or issue that started this?

I checked the logs and HASS is able to see the changes though!?
2020-02-19 20:25:41 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homie/dimmer-23b4db1/dimmer/dimmer: b’74’
2020-02-19 20:25:41 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homie/dimmer-23b4db1/dimmer/power: b’ON’

Oh, ok. Not sure why that fixed it for someone else lol.

The state template needs double {{ and }}. Looks like you only have single.

“state_value_template”: “{{ ‘0’ if value == ‘0’ else ‘100’ }}”,

Try this:

  - platform: mqtt
    name: Master Bed Light Switch
    state_topic: "homie/dimmer-3f91181/dimmer/dimmer"
    value_template: >
      {% if value_json.value|float == 0 %}
        OFF
      {% else %}
        ON
      {% endif %} 
    command_topic: "homie/dimmer-3f91181/dimmer/dimmer/set"
    payload_on: '99' 
    payload_off: '0'
    brightness_scale: 99
    brightness_state_topic: "homie/dimmer-3f91181/dimmer/dimmer"
    brightness_command_topic: "homie/dimmer-3f91181/dimmer/dimmer/set"
    on_command_type: 'brightness'
    optimistic: true
    retain: true

I tried this one

{
    "name": "Master Bed Light Switch",
    "command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_value_template": "{{ 0 if value == 0 else 100 }}",
    "on_command_type": "brightness",
    "brightness_scale": 100,
    "payload_on": 100,
    "payload_off": 0
}

and this one

{
    "name": "Master Bed Light Switch",
    "command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_value_template": "{{ 0 if value == 0 else 100 }}",
    "on_command_type": "brightness",
    "brightness_scale": 100,
    "payload_on": "100",
    "payload_off": "0"
}

So the system I’m using generates JSON configs that are consumed by HASS from MQTT. I won’t be using YAML configs in the end since my new devices come from MQTT. For testing I’ll look to add exclusion so the JSON so this device doesn’t generate JSON configs.

Two notes I had on this YAML config was in the state template you use “value__json” I would assume I would need to change that to value? since it would be valid json see log message below.

Other one is related to 99 brightness being the highest number since i’m dividing to get a percent it would be possible for 100 might cause an issue?

2020-02-19 20:25:41 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homie/dimmer-23b4db1/dimmer/dimmer: b’74’

the yaml I sent is for a zwave dimmer that I access through zwave2mqtt. The value reference in mine is from this (0 being off):

{"value_id":"4-38-1-0","node_id":4,"class_id":38,"type":"byte","genre":"user","instance":1,"index":0,"label":"Level","units":"","help":"The Current Level of the Device","read_only":false,"write_only":false,"min":0,"max":255,"is_polled":false,"value":0}

That could be. I don’t recall where, but the yaml I sent you was buried in a separate thread. I was having a hard time getting my dimmer set up using the json from zwave2mqtt. I found that value template and the 99 reference elsewhere and it worked for me. Here is the json for that dimmer that I couldn’t get to work until I used the yaml structure I sent you. Not sure if this will help clarify an issue or not.

{
  "type": "light",
  "object_id": "dimmer",
  "discovery_payload": {
    "schema": "template",
    "brightness_template": "{{ (value_json.value / 99 * 255) | round(0) }}",
    "state_topic": "zwave2mqtt/nodeID_4/38/1/0",
    "state_template": "{{ \"off\" if value_json.value == 0 else \"on\" }}",
    "command_topic": "zwave2mqtt/nodeID_4/38/1/0/set",
    "command_on_template": "{{ ((brightness / 255 * 99) | round(0)) if brightness is defined else 255 }}",
    "command_off_template": "0",
    "device": {
      "identifiers": [
        "zwave2mqtt_nodeID_4"
      ],
      "manufacturer": "Leviton",
      "model": "0x0001",
      "name": "DZPD3-2BW Decora 300W Plug-In Smart Dimmer",
      "sw_version": "2.1.1"
    },
    "name": "nodeID_4_dimmer",
    "unique_id": "zwave2mqtt_nodeID_4_4-38-1-0"
  },
  "discoveryTopic": "light/nodeID_4/dimmer/config",
  "values": [
    "38-1-0"
  ],
  "persistent": false,
  "id": "light_dimmer"
}

Nice, doing the brightness math in the template. So the one thing I notice is your payloads are defaults. When I did some testing that worked for all the states (ON/OFF and brightness) but then I ran into issues sending commands since my devices don’t support ON of OFF) they only support brightness as payloads. There isn’t a supported payload template for MQTT Light using JSON.

EDIT not defaults, using command templates :frowning:

command_off_template and command_on_template

which isnt supported in JSON

That is why I had to do the state template to try and match to the the payload on and off so the states would be detected correct. That fixes the sending the right payload but states stop working. It drives me nuts it works in the template tester in the dev tools

"state_value_template": "{{ 0 if value == 0 else 100 }}",
    "payload_on": 100,
    "payload_off": 0

I’m not too familiar with that type of set up; sorry. Looking at the docs though, do you need brightness: true set as attribute? Says the default is off. It also appears brightness is scaled to 255 not 100. No idea if any of that matters though

If i remember reading correctly brightness was retired. I tested and got this

voluptuous.error.MultipleInvalid: extra keys not allowed @ data[‘brightness’]

I’ll try the YAML and test it with state template and switch tousing the command templates see if one or both work. I can’t image this is a bug since i’ve see others on the fourms with the same json config

Now i’m just straight confused. I’m getting an error command on template doesn’t exist error using YAML?

Invalid config for [light.mqtt]: [command_on_template] is an invalid option for [light.mqtt]. Check: light.mqtt->command_on_template

This is an example from the Light MQTT link above

# Example configuration.yaml entry
light:
  - platform: mqtt
    schema: template
    command_topic: "home/rgb1/set"
    command_on_template: "on"
    command_off_template: "off"

CORRECTION
its required to have

schema: template

Ok that took longer then it should have. I have a working POC of template schema in YAML. Now I need to see how I translate that to config json. I’m curious has anyone working with config json schema: template?

Also note my pain that I guess the MQTT server is converting my float to a string so I had to convert to int to get this to work.

light:
  - platform: mqtt
    name: Master Bed Light Switch
    schema: template
    state_topic: "homie/dimmer-3f91181/dimmer/dimmer"
    command_topic: "homie/dimmer-3f91181/dimmer/dimmer/set"
    state_template: "{{ 'off' if value == '0'  else 'on' }}"
    command_on_template: "{{ ((value | int / 255 * 100) | round(0)) if value is defined else 255 }}"
    command_off_template: "0"
    brightness_template: "{{ ((value | int) / 100 * 255) | round(0) }}"
    retain: true

So using MQTT discovery doesn’t support template schema and looks like basic is what its called in the source code. So in trying to get this back in the format of “basic” the top part of the MQTT Light doc flags [Called default in documentation]

I was able to fix the brightness issue by converting it to an int

I have to assume i’m doing something wrong or this would be a bug. The light is always on as the state is concerned but you can double tap and see the commands send 0 and 100 for on so the payload works but always snaps back to ON. So for state_template I tried to match the payload_off with ‘off’, 0, ‘0’ anything to get the state to stay in the off state to debug this. Is there a better explanation on how the state_template works for setting for if its off or on? I assumed its off if it matches payload_off and on if it matches payload_on.

The end goal is use this "{{ 0 if (value | int) == 0 else 100 }}"

{
    "name": "Master Bed Light Switch",
    "command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_value_template": "{{ 'off' }}", #tried 0 or '0'
    "brightness_scale": 100,
    "on_command_type": "brightness",
    "brightness_value_template": "{{ value | int }}",
    "payload_on": 100,
    "payload_off": 0
}

Note: I tried the state_template with or without {{ }} to see if there was an issue with those

Looking at the code something might be off with state_value_template vs value_template based on the const values?


I’m late to the party here (and have only skimmed the thread) but the first post’s example caught my attention. It mixes strings and numbers and fails to use double-braces to define the template.

Try this and see if it helps:

{
    "name": "Master Bed Light Switch",
    "command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_value_template": "{{ '0' if value == '0' else '100' }}",
    "on_command_type": "brightness",
    "brightness_scale": 100,
    "payload_on": "100",
    "payload_off": "0"
}

This abbreviated version may work as well:

{
    "name": "Master Bed Light Switch",
    "command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_command_topic": "homie/dimmer-3f91181/dimmer/dimmer/set",
    "brightness_state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_topic": "homie/dimmer-3f91181/dimmer/dimmer",
    "state_value_template": "{{ 'OFF' if value == '0' else 'ON'}}",
    "on_command_type": "brightness",
    "brightness_scale": 100
}

The solution was to publish to the ‘homeassistant/light/dimmer-3f91181/config’ with payload of ‘’ which is just blank. This caused HASS to do clean up and remove the objects instead of updating them. Then publish the same template to that topic and it worked. I would hope the update function to the config json loader would take care of that. I’ll look at adding a adding that to this project on every startup to clear these out.

{
  "name": "{self.name}",
  "command_topic": "homie/{self.device_id}/dimmer/dimmer/set",
  "brightness_command_topic": "homie/{self.device_id}/dimmer/dimmer/set",
  "brightness_state_topic": "homie/{self.device_id}/dimmer/dimmer",
  "state_topic": "homie/{self.device_id}/dimmer/dimmer",
  "state_value_template": "{{{{ 0 if (value | int) == 0 else 100 }}}}",
  "brightness_scale": 100,
  "on_command_type": "brightness",
  "brightness_value_template": "{{{{ value | int }}}}",
  "payload_on": 100,
  "payload_off": 0
}

BTW, publishing a blank payload to any MQTT topic is the standard way to purge a retained message. As you’ve seen, purging the homeassistant/../config topic’s message will cause MQTT Discovery to remove the associated entity (this is a known behavior).

In fact, if you do not publish a retained message to the config topic, when Home Assistant restarts it will automatically delete the associated entity (because on startup, the config topic will not have a message).

Which template are you referring to? The first one you posted had errors (identified in my previous post) and subsequent ones had questionable templates.

Edit: Moved Template to Answer

You should probably paste that template into the post you marked as being the Solution. This was the point I made 19 days ago, your Solution post refers to a template that it doesn’t reveal.

FWIW, it’s not obligatory to delete an entity prior to updating its configuration via MQTT Discovery.