I think it is intuitive, did you read the docs?
So if your payload_on represents what you need, there is no need to use[value_template](https://www.home-assistant.io/integrations/switch.mqtt/#value_template)
I believe that if you define it like
And if you define it like this yaml</s> <s>value_template: ></s> <s> {{ 'ON' if value_json.value == 1 else 'OFF' }}</s> <s> there will be no need to have payload_xxx if I remember it right
EDIT: well, it will work only for displaying state of your switch in HA but it wonāt let you control your relay as ON/OFF (default values) wonāt be recognised (see this post for more details).
Itās not intuitive only because you have instructed Home Assistant to match the literal JSON string in the payload. Normally, when the payload contains a JSON string, you would refer to it using value_json in the value_template.
Youāre not doing that and have chosen to match the string verbatim. You may believe itās a Solution but what youāve done is work around the reason it originally failed.
The problem is that the JSON stringās key is called value. That word has special meaning for Home Assistant (just like value_json). If the value_template looks like this:
{{ value_json.value }}
it is using what is called ādot notationā to refer to value key in {value: 1} and will fail because the meaning of value is now ambiguous (i.e. do you want the value key or the special meaning of value?).
If you use ābracket notationā then the meaning of value becomes clear to Home Assistant. You donāt want the āspecial meaningā you simply want it to refer to the valuekey in the payload.
thanks for stepping in. I feel the need to appologize for using the phrase āunintuitiveā. Better I would have used the phrase āI dont understandā.
If you specify payload_on and payload_off, they define both the received and transmitted states. If you want the received state to be different from the transmitted state, you must also use state_on and state_off.
However, even though the example Iāve just presented is technically correct, it is needlessly complicated.
The fact is the switch you are using presents its data in JSON format but thereās no benefit to handling it in JSON format! The data is so simple that the best way to handle it is the way you did it. Just ignore the JSON format and interpret the received payload verbatim.
I then published '{"value": 1}' and '{"value": 0}' messages to test/State topic and I saw no difference in my _dot and _ bracket versionsā states.
I expected sensor.test_dot to be different and even published '{"value": 1, "extra": 2}' but itās still 1 for both sensors.
Therefore it seems to me that itās ok to have value key in JSON and we can use it safely in both dot and bracket notations as any other key.
The reason TSās original config didnāt work was the result of value_template (they most likely mixed MQTT switch with MQTT Binary Sensor) and to fix it they can either make it return a value that agrees with payload_on/payload_off or just delete it completely - as described in my previous reply.
And as it was pointed out, because we have a switch, payload_xxx is used to determine its new state (when HA receives a new message) and to control the device (as HA sends payload_xxx depending on the switchās current state to toggle the deviceās state) so we should not change it or fall back to the default values (ON/OFF).
well, they broke it and they fixed it (by trial and error I presume) - I donāt mind, really
just wanted to make clear that āspecial meaningā situation
Looking at this topic and the MQTT Switch documentation (pretty sure other MQTT integrations are the same) I have to say that docs would do with improvements as there is a complex relationship between some of the configuration variables (payload_xxx, state_xxx and value_template in this case) but itās not reflected there at all or well enough to understand how to use them.
Might make a PR when I have time.