fokcuk
(Serge)
April 28, 2024, 3:53am
1
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
francisp
(Francis)
April 28, 2024, 5:20am
2
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
fokcuk
(Serge)
April 28, 2024, 12:32pm
3
Keep getting Unavailable =(
francisp
(Francis)
April 28, 2024, 12:37pm
4
Try removing the availability, that is not correct anyway.
fokcuk
(Serge)
April 28, 2024, 12:46pm
5
Yeah, tried that. Still no good
francisp
(Francis)
April 28, 2024, 12:51pm
6
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
fokcuk
(Serge)
April 28, 2024, 1:24pm
7
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
fokcuk
(Serge)
April 28, 2024, 1:44pm
8
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…
francisp
(Francis)
May 1, 2024, 11:47am
10
So it is not always present in the json ?
francisp
(Francis)
May 1, 2024, 12:00pm
11
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
fokcuk
(Serge)
May 1, 2024, 2:19pm
12
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}}'