MQTT switch setup - assistance needed

Hi everyone
Need assistance to create a MQTT switch to turn enable/disable bluetooth pairing. Currently I am using just 2 buttons with MQTT publish commands. But always learn =)

Got lost in all those options…

There are 2 separate MQTT topics - one for status and one for commands.
Device sends its status on

iot-2/evt/generic/fmt/json

Output is similar to this:

{
"device_id": "321341100000400d",
"message": "DEVICE_INFO",
"payload": {
  "battery_level": 50,
  "free_storage": 98,
  "interaction_lock": false,
  "voice_lock": false,
  "parental_lock": false,
  "is_playing_content": true,
  "bluetooth_pairing_enabled": false,
  "light_status": true,
  "max_volume": 100,
  "firmware_version": "1.1.4.0"
}
}

Commands on
iot-2/cmd/generic/fmt/json

To send the command, is like this:
{"command":"BLUETOOTH_PAIRING","payload":{"enabled":true}}

What do I need to use where?

mqtt:
  switch:
    - unique_id: codi_bt_pairing_switch
      name: "codi_bt_pairing_switch"
      state_topic: "iot-2/evt/generic/fmt/json/BLUETOOTH_PAIRING"
      command_topic: "iot-2/cmd/generic/fmt/json/BLUETOOTH_PAIRING"
      availability:
        - topic: "iot-2/cmd/generic/fmt/json"
      payload_on: "'enabled':true"
      payload_off: "'enabled':false"
      state_on: "enabled"
      state_off: "disabled"
      optimistic: false
      qos: 0
      retain: true

more something like

mqtt:
  switch:
    - unique_id: codi_bt_pairing_switch
      name: "codi_bt_pairing_switch"
      state_topic: "iot-2/evt/generic/fmt/json"
      command_topic: "iot-2/cmd/generic/fmt/json/BLUETOOTH_PAIRING"
      availability:
        - topic: "iot-2/cmd/generic/fmt/json"
      payload_on: '{"enabled":true}'
      payload_off: '{"enabled":false}'
      value_template: >
         {% if (value_json.payload.bluetooth_pairing_enabled) %}
           {{'ON'}}
         {% else %}
           {{'OFF'}}
         {% endif %}

      optimistic: false
      qos: 0
      retain: true

Keep getting Unavailable =(

Try removing the availability, that is not correct anyway.

Yeah, tried that. Still no good

try this

mqtt:
  switch:
    - unique_id: codi_bt_pairing_switch
      name: "codi_bt_pairing_switch"
      state_topic: "iot-2/evt/generic/fmt/json"
      command_topic: "iot-2/cmd/generic/fmt/json"
      payload_on: '{"enabled":true}'
      payload_off: '{"enabled":false}'
      value_template: >
         {% if (value_json.payload.bluetooth_pairing_enabled) %}
           {{'ON'}}
         {% else %}
           {{'OFF'}}
         {% endif %}

      optimistic: false
      qos: 0

same…

I even tried to get the state using the same method that I get for the sensor:

    - unique_id: codi_bt_pairing_switch_2
      name: "codi_bt_pairing_switch_2"
      state_topic: "iot-2/evt/generic/fmt/json"
      #value_template: "{{ value_json.payload.bluetooth_pairing_enabled }}"
      value_template: "{{ value_json.device_id }}"
      #{{ state_attr("sensor.codi_robot_device", "payload").bluetooth_pairing_enabled  }}
      json_attributes_topic: "iot-2/evt/generic/fmt/json"
      json_attributes_template: "{{ value_json.payload | tojson }}"
      command_topic: "iot-2/cmd/generic/fmt/json/BLUETOOTH_PAIRING"
      #      availability:
      #        - topic: "iot-2/cmd/generic/fmt/json"
      #availability_mode: latest
      payload_on: '{"enabled":true}'
      payload_off: '{"enabled":false}'
      #optimistic: false
      #qos: 0
      retain: false

still unknown… And I tried that commented out
{{ state_attr("sensor.codi_robot_device", "payload").bluetooth_pairing_enabled }}

From the template testig it shows me “false” or “true” (thats the MQTT sensor), but I still get unknown in the sate =(

State attributes that I get through json_attributes_topic/json_attributes_template show up ok

Funny that when MQTT changes the message (new track is played, so bluetooth_pairing_enabled attribute is not sent), I can see warning in the logs to say that bluetooth_pairing_enabled is not found

  • Template variable warning: ‘dict object’ has no attribute ‘bluetooth_pairing_enabled’ when rendering ‘{% if (value_json.payload.bluetooth_pairing_enabled) %} {{‘ON’}} {% else %} {{‘OFF’}} {% endif %}’

So it knows its there or not, but why its not showing its state…

Any other things to try?

So it is not always present in the json ?

  switch:
    - unique_id: codi_bt_pairing_switch
      name: "codi_bt_pairing_switch"
      state_topic: "iot-2/evt/generic/fmt/json"
      command_topic: "iot-2/cmd/generic/fmt/json"
      payload_on: '{"enabled":true}'
      payload_off: '{"enabled":false}'
      value_template: >
        {% if (value_json.payload.bluetooth_pairing_enabled) %}
           {{'ON'}}
         {% else %}
           {{'OFF'}}
         {% endif %}
      state_on: 'ON'
      state_off: 'OFF'
      optimistic: false
      qos: 0

Far out =) It worked! Need to remember this…
Had to pass the full command in the payload, but it works!

  payload_on: '{"command":"BLUETOOTH_PAIRING","payload":{"enabled":true}}'
  payload_off: '{"command":"BLUETOOTH_PAIRING","payload":{"enabled":false}}'