MQTT Switch - Unknown State

Hi!

I am trying to make a MQTT switch but I think I don’t know how to do it. This is what I have done till now:

- name: JBL Speaker Switch
    state_topic: "tele/ZigBee_Bridge/29C3/SENSOR"
    command_topic: "cmnd/ZigBee_Bridge/ZbSend"
    value_template: |
      {% if state_attr("sensor.variable_home_tablet_media_volume","bluetooth") == "connected" %}
      on
      {% else %}
      off
      {% endif %}
    payload_on: '{"device": "JBL_Speaker_Switch","send":{"Power":1}}'
    qos: 2
    retain: true

The switch it is working as turning it on, but the state is “unknown”… Any idea?

Thank you in advance

First of all, you have to wait until

sensor.variable_home_tablet_media_volume

updates to get the result

Second : I would remove the retain flag, as this retains the command, and not the state.

Also you can change this:

    value_template: |
      {% if state_attr("sensor.variable_home_tablet_media_volume","bluetooth") == "connected" %}
      on
      {% else %}
      off
      {% endif %}

To this:

    value_template: "{{ is_state_attr('sensor.variable_home_tablet_media_volume', 'bluetooth', 'connected') }}"
- name: JBL Speaker Switch
    state_topic: "tele/ZigBee_Bridge/29C3/SENSOR"
    command_topic: "cmnd/ZigBee_Bridge/ZbSend"
    value_template: "{{ is_state_attr('sensor.variable_home_tablet_media_volume', 'bluetooth', 'connected') }}"
    payload_on: '{"device": "JBL_Speaker_Switch","send":{"Power":1}}'
    qos: 2

Still unknown state

What message is sent in your state topic?

tele/ZigBee_Bridge/29C3/SENSOR

Message 0 received on tele/ZigBee_Bridge/29C3/SENSOR at 15:03:

{"ZbReceived":{"JBL_Speaker_Switch":{"Device":"0x29C3","Name":"JBL_Speaker_Switch","Power":1,"Endpoint":1,"LinkQuality":113}}}

QoS: 0 - Retain: false 

When I call it with {"device": "JBL_Speaker_Switch","send":{"Power":1}}

This device only have “Power”:1, it is a zigbee fingerbot, one way switch

Ah, ok. That’s not going to work then.

What does this return in the template editor:

{{ state_attr('sensor.variable_home_tablet_media_volume', 'bluetooth') }}

“connected” or “disconnected”

Try this:

value_template: "{{ 'ON' if is_state_attr('sensor.variable_home_tablet_media_volume', 'bluetooth', 'connected') else 'OFF' }}"

Make sure you copy it exactly. The upper case states are important.

The state of switch it is always “off”

It started with “unknown”, till I changed the bluetooth value, then it was “off”, even when I changed the value to connected or disconnected…

Strange, if I do this into the template editor, all good… but the switch, it is always off

I missed this:

As well as the payload that is sent it is also the payload that is being looked for by the value template for the on state. So try this:

- name: JBL Speaker Switch
    state_topic: "tele/ZigBee_Bridge/29C3/SENSOR"
    command_topic: "cmnd/ZigBee_Bridge/ZbSend"
    value_template: >
      {% if is_state_attr('sensor.variable_home_tablet_media_volume', 'bluetooth', 'connected') %}
        {"device": "JBL_Speaker_Switch","send":{"Power":1}}
      {% else %}
        OFF
      {% endif %}
    payload_on: '{"device": "JBL_Speaker_Switch","send":{"Power":1}}'
    qos: 2
    retain: false

It is acting in reverse :smiley: It is ON when bluetooth it is disconnected and OFF when it is connected

I don’t understand how that is possible but it’s an easy fix:

    value_template: >
      {% if is_state_attr('sensor.variable_home_tablet_media_volume', 'bluetooth', 'connected') %}
        OFF
      {% else %}
        {"device": "JBL_Speaker_Switch","send":{"Power":1}}
      {% endif %}

Maybe because the bluetooth it is connecting after 5 seconds or something… It is not instantly.

That would cause the switch state to “bounce” (if turning on it would go, on then off then on) not reverse the logic.

What’s in your state topic when you send the turn on command?
What’s in your state topic when you send the turn off command?

Right now, I think there’s a mixup going on. You may have to define payload_on and state_on if the state_topic does not match the payload_on after the payload on is sent to the command topic.

I can only send one command, the device is an zigbee fingerbot with only one command {“power”:1}

The switch state is unknown after HA restart

Yes, but your configuration shows a separate topic for the state. So, does that separate topic update when you send to the command_topic?

Your state topic determines the state of the switch
Your command topic is a topic that your switch sends commands to when you run turn_on/turn_off/toggle services.

It is about that Power with value 1, wich is always 1, if I send the command topic, with the command Power 1 to the device, which is already 1, it will make the device to react and the state topic will receive power 1 as well

Can you send an on and off command? I suggest you make a proper mqtt switch then make a template switch after the fact.