Sofabaton X2 integration

I’ve recently received a Sofatbaton X2 and have now managed to get it working well with Openhab. I thought others might find my discoveries useful.

I wanted to trigger a number of power switches which are Tapo (TP-Link) Wifi devices. However, there is no specific integration with Tapo/TP-Link on the Remote. So I have found a way to do this via Openhab.

There’s no specific integration with Openhab, but I’ve used a combination of the Sofabaton X2 integration with Home Assistant and a few rules in Openhab.

The HA integration is simply using MQTT messages. So to get this to work, you first need to make sure you have installed the MQTT broker binding and have an MQTT broker up and running.

Here’ are the steps I took:

1) Setup the link to your MQTT broker in the Sofabaton App

From the Sofabaton App, choose ‘Me’ in the bottom right, then ‘Connect to Home Assistant’

Confirm you’ve installed the MQTT server, just hit next on the confirmation page

Enter the url and port of your MQTT server, username and password

You should get a confirmation screen saying it’s all setup.

2) Setup a device on your Sofabaton remote

Still in the app, go to the devices screen and hit the + sign to add a new device. Choose ‘Wi-Fi’ control method.

Then choose ‘Home Assistant Remote’

3) Add action buttons to your remote

You now need to add action buttons that will appear on your remote.

Note, that you can only modify the command name - ie, the name that appears on the button. Each button will be assigned a unique button number, there’s no way to change this - it’s allocated by the app.

You’ll see a ‘Topic’ and ‘Payload’ displayed under the command name. These are the important codes you need to copy and use in Openhab.

The topic will always be the same for your remote - this is it’s MAC address followed by /up

The payload is a combination of a device_id and key_id

The device is the device number in your list (ie, for me it was device number 8). The key number is the sequential number of the key. ie, the first button you add will be 1, then 2, 3, etc. As i say,there is no way to change this - you just have to remember which button number responds to your command name.

Hit next, you see the button commands you’ve setup. You can add as many as you like.

Once you’ve created you buttons, you’ll be asked to name the device and the app will download the settings to your remote.

You’ll now see a new device. Here, I created one for all my Tapo plugs called ‘Plugs’:

When i click it, I see the command buttons I’ve setup:

These are also displayed on the screen on the Sofabaton X2.

Now over to Openhab setup steps….

4) Create a Generic MQTT Thing to receive your Sofabaton messages

You need to create a Generic MQTT Thing that receives the MQTT message from the Sofabaton remote (I’m assuming here you’ve already setup your MQTT bridge in Openhab - if not, there’s a lot of posts on how to do that).

Create a new Thing by selecting the MQTT binding and then ‘Generic MQTT Thing’ as the Thing type.

Make sure you select your MQTT bridge when setting up your new Thing. Once you do this, your Thing should show online.

5) Create channels to receive data sent from Sofabaton

Create 2 channels: one for the Device_ID, one for the Key_ID. In both of these, add your ‘topic’ from the Sofabaton command into ‘State Topic’. Remember to add the ‘/up’. So your command should be:

[mac-address-of your remote]/up

You don’t need to enter the command topic. As Sofabaton currently only supports sending commands from the remote to Openhab - ie, it’s state read only.

Now here’s the fun part….

We are going to need to handle the format of the JSON payload coming in from the remote.

Click on ‘Show Advanced’ on your Channel. Enter the incoming Value Transformation as:

JSONPATH:$.device_id

or

JSONPATH:$:key_id

Note: you’ll need to make sure you already have the JSONPATH transformation binding for this to work (I assume you probably do if you are already using MQTT). If not, add it from the bindings add-on store).

6) Create an item for each channel

Create new items for the device_id and key_id. Make them both string types. (I tried point and that didn’t seem to work).

7) Test your commands are being received

You should now be able to press the command buttons you created on your remote, and see those device_id and key_id values changing in Openhab. If you do - well done! you are connected :clap: :clinking_glasses: (maybe not the device_id if you’ve only created 1 device!).

8) Create a rule to manage your Sofabaton commands

You’ll need to create a rule (or rules) to handle the receiving commands and perform your desired actions. You could create one rule for each button directly in the rules UI. To do this, make sure you choose the ‘Was Updated’ trigger:

This gets very messy once you have a few buttons. And if you end up with setting up multiple devices, it’ll be unmanageable.

The better way is to set a text file rule using Switch logic

To do this, you need to create a file in your rules folder with a .rules extension. eg, sofabaton.rules

Here’s the code I created to handle 4 command buttons:

rule "Sofabaton Device #7"
when
    Item Sofabaton_Device_ID received update "7"
then
    var buttonNumber = Sofabaton_Sofabaton_Key.state

    logInfo("Sofabaton","Button {}", Sofabaton_Sofabaton_Key.state)

    switch(buttonNumber) {
        case 1 : {
            AVPowerStrip_Outlet1_Switch.sendCommand(if(AVPowerStrip_Outlet1_Switch.state == ON) OFF else ON)
        }
        case 2 : {
            AVPowerStrip_Outlet2_Switch.sendCommand(if(AVPowerStrip_Outlet2_Switch.state == ON) OFF else ON)
        }
        case 3 : {
            CurveLight.sendCommand(if(CurveLight.state == ON) OFF else ON)
        }
        case 4 : {
            CoffeeLight.sendCommand(ON)
        }
    }
end 

The first three are ‘toggle’ buttons. So turn on if off, and off if on

The 4th just turns the light on all the time, you’ll probably need a separate light off command if you want to do this.

Conclusion

It’s a little fiddly, but once you get the first button done, it’s pretty easy to add new ones. The performance is excellent - instant i would say.

I’m not sure I’d use the sofabaton remote for a home control remote, but for those key on/off switches or light scene switching for movie nite it works really well.

Let me know how you get on!

6 Likes

Thanks for the guide, dont have this hardware but interested in checking it out…

Out of interest, what does the MQTT traffic look like? It is possible that openHAB can auto detect and setup the device for you but if it can you need to setup the following:

  1. MQTT binding
  2. Home Assistant - Bindings | openHAB
  3. Jinja Transformation
  4. JSON Transformation

Sadly the 3 and 4 need to manually be installed when IMHO it should be done automatically when you install 2.

The messages received are pretty basic, look like this:

{"device_id":7,"key_id":1}

{"device_id":7,"key_id":2}

{"device_id":7,"key_id":3}

I only have one device setup, so all my device_id’s will be 7

I’ve not tried adding the Home Assistant Binding, that might be interesting. Could save a couple of steps.

The binding / mqtt layer uses the TOPIC as well as the MESSAGE PAYLOAD to auto detect and setup things and channels inside openHAB. Worth trying as I use this method for a number of devices and they just show up in the INBOX and can be added via the UI with a few clicks.

ah, ok. Topic is the MAC address of the remote plus /up

Will give it a go

I don’t think it works. As i think it needs Sofabaton to publish it’s messages with homeassistant in the topic. And unfortunately it doesn’t do that ;-( Nice idea though