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’
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.
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
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
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.
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.
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.
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.
ya I would hope it doesn’t require the deletes, there might have been issues with my Home Assistant too.But It dawned on me after working with some nice people on Home Assistant Discord channel were able to get it working on a new object with the same template pointing to the same MQTT topics but different named device in HA. At that point I knew I had to delete that object and apply the template again. Can’t really explain why
Thanks for your help your template you posted above would have worked!
Thanks for this, been struggling to get my dimmer lights showing correctly in HA.
Not sure what the final issue was of yours, but I think Zwave2MQTT was sending the value to topic as a string not as a number. Adding the INT seems to have done the trick!