Kuba Wolanin
About

Integrating LG ThinQ Air Conditioner with openHAB

By Kuba Wolanin
Published in Home automation
June 07, 2021
2 min read

I bought LG Artcool Mirror air conditioning unit for my living room. It had a built-in WiFi connection and at the time I didn’t give it too much thought in terms of hooking it up to my smart home system.\ Soon after, I realized that it could not link with my Google Home (since it’s located in Poland and LG ThinQ Google Assistant integration works only in US and a few other markets…). What’s more, integrating it with my home automation system was not trivial as well.

After searching GitHub with working ThinQ projects, I found sampsyo/wideq repo, which potentially could resolve my issues with connecting to my AC.

Setting up Wideq

After cloning the repository to the home folder on my Ubuntu machine, I’ve run the example.py with Python3.

We’ll be working with openHAB Exec binding, so it’s wise to run all shell comands using openhab user

/usr/bin/python3 /home/kuba/wideq/example.py -c PL -l pl-PL

c parameter represents country l parameter represents language

Now the command line interface will ask you to log into your LG account from the browser. After logging in, copy the URL, save it somewhere and paste it to the CLI.

Successful authentication should result in listing all your devices along with their IDs:

kuba@hub:~$ /usr/bin/python3 /home/kuba/wideq/example.py -c PL -l pl-PL ls

47999999-1111-11e8-1111-044eaf865555: REFRIGERATOR (REFRIGERATOR 2RES1VE61NFWA)
47afeeee-2222-11e9-2222-020000001111: WASHER (WASHER XXXXXXX)
d27eeeee-3333-11d3-3333-2c2bf9389999: AIR CONDITIONER (AC RAC_111111_WW)

Awesome! Now let’s save the air conditioner’s ID somewhere (in example above it’s d27eeeee-3333-11d3-3333-2c2bf9389999) and proceed with modifying the example.py code so that it doesn’t ask us for the URL every time.

Open example.py in a text editor of your choice

sudo nano /home/kuba/wideq/example.py

Then modify the authenticate method like so:

def authenticate(gateway):
    """Interactively authenticate the user via a browser to get an OAuth
    session.
    """

    login_url = gateway.oauth_url()
    # print(login_url)
    # print("Then paste the URL where the browser is redirected:")
    callback_url = "YOUR_LG_URL"
    return wideq.Auth.from_url(gateway, callback_url)

Now replace YOUR_LG_URL with your callback URL you got in the browser.

This is an ugly hardcoding, I know. But it got the job done in my case and I didn’t want to bother with something more elegant ¯_(ツ)_

openHAB

First, make sure you have Exec binding installed. Then create the following files:

  • scripts/ac-turn.sh
#!/bin/bash

/usr/bin/python3 /home/kuba/wideq/example.py -c PL -l pl-PL turn d27eeeee-3333-11d3-3333-2c2bf9389999 $1
  • scripts/ac-set-temp.sh
#!/bin/bash

/usr/bin/python3 /home/kuba/wideq/example.py -c PL -l pl-PL set-temp d27eeeee-3333-11d3-3333-2c2bf9389999 $1

Remember to replace the example ID with your AC’s!

  • things/exec.things
Thing exec:command:thinqAcTurn [
    command="/etc/openhab2/scripts/ac-turn.sh %2$s",
    interval=0,
    timeout=10,
    autorun=true
]

Thing exec:command:thinqAcTemp [
    command="/etc/openhab2/scripts/ac-set-temp.sh %2$s",
    interval=0,
    timeout=10,
    autorun=true
]
  • Remember to add the files to misc/exec.whitelist
/etc/openhab2/scripts/ac-turn.sh %2$s
/etc/openhab2/scripts/ac-set-temp.sh %2$s

And lastly, make the scripts executable:

chmod +x /etc/openhab2/scripts/ac-turn.sh
chmod +x /etc/openhab2/scripts/ac-set-temp.sh

You can now test if the script turns on your air conditioner from command line:

sudo -u openhab /etc/openhab2/scripts/ac-turn.sh on
sudo -u openhab /etc/openhab2/scripts/ac-turn.sh off

Yay!

In the next step I needed to restart my openHAB instance (sudo systemctl restart openhab2.service) but restarting a Exec binding bundle should be enough:

sudo openhab-cli console
# type `habopen` as password
bundle:list |grep Exec
bundle:restart <ID>

Now let’s prepare some Items, rules and a proper Sitemap:

  • items/climate.items
Switch    LivingRoom_AC             "Air Conditioner [%s]"
Number    LivingRoom_AC_Temperature "Target Temperature [%.1f °C]"

String Thinq_ac_turn_Args "AC Turn on args [%s]" {channel="exec:command:thinqAcTurn:input"}
String Thinq_ac_turn_Out "AC Turn on out [%s]" {channel="exec:command:thinqAcTurn:output"}
Number Thinq_ac_turn_Exit "AC Turn on exit [%s]" {channel="exec:command:thinqAcTurn:exit"}


String Thinq_ac_temp_Args  "AC Temp args [%s]" {channel="exec:command:thinqAcTemp:input"}
String Thinq_ac_temp_Out "AC Temp out [%s]"  {channel="exec:command:thinqAcTemp:output"}
Number Thinq_ac_temp_Exit "AC Temp exit [%s]"  {channel="exec:command:thinqAcTemp:exit"}
  • rules/airconditioner.rules
rule "Turn the AC on or off"
when
   Item LivingRoom_AC received command
then
   if (LivingRoom_AC.state == ON) {
      Thinq_ac_turn_Args.sendCommand("on")
   }else if (LivingRoom_AC.state == OFF) {
      Thinq_ac_turn_Args.sendCommand("off")
   }

end

rule "Set the temperature"
when
    Item LivingRoom_AC_Temperature changed
then
    Thinq_ac_temp_Args.sendCommand(LivingRoom_AC_Temperature.state.toString())
end

What thee rules do:

  1. React on LivingRoom_AC item state change and run Executable with given parameters. Thinq_ac_turn_Args.sendCommand("on") is an equivalent of
$ sudo -u openhab /etc/openhab2/scripts/ac-turn.sh on
  1. React on LivingRoom_AC_Temperature item state (note it’s a Number item) and sets the temperature. Note: I had to modify wideq’s example.py again, so that it receives Celsius instead of Fahrenheit:
def set_temp(client, device_id, temp):
    """Set the configured temperature for an AC or refrigerator device."""

    device = client.get_device(device_id)

    if device.type == wideq.client.DeviceType.AC:
        ac = wideq.ACDevice(client, _force_device(client, device_id))
        ac.set_celsius(int(temp)) # <- changed from ac.set_fahrenheit(int(temp))

Now running ac-set-temp.sh should wake up your AC with the right target temperature:

$ sudo -u openhab /etc/openhab2/scripts/ac-set-temp.sh 22

Ok, last but not least - a sitemap!

  • sitemap/home.sitemap
...
Frame label="Air Condition" {
    Switch item=LivingRoom_AC icon="airconditioner" label="Air Conditioner []" mappings=[ON="Turn On", OFF="Turn Off"]
    Setpoint item=LivingRoom_AC_Temperature icon="thermometer" minValue=20 maxValue=24 step=1
}
...

Now you can use LivingRoom_AC and LivingRoom_AC_Temperature items in your automations!

Notes

  • You might want to set a DEBUG logging level for the Exec binding:
sudo openhab-cli console
# type `habopen` as password
openhab> log:set DEBUG org.openhab.binding.exec
  • Controlling your AC using a IR blaster (or a pre-made one like a Broadlink RM4C or other) can be much simpler.

  • At the time of writing there were no viable LG ThinQ AC integrations for Node-RED, nor for MQTT.

  • Wideq can control other appliances as well. Please refer to the docs for more information.


Previous Article
Secure pairing Danalock with openHAB by Z-Wave binding

Topics

Home automation
Misc Projects

Related Posts

Secure pairing Danalock with openHAB by Z-Wave binding
May 28, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

About

Social Media