LG Hom Bot integration

I recently switched from Domoticz to HASS and one thing I really missed was the integration with my LG Hom Bot robot vacuum so that I can control it with presence detection, automation etc. I adapted the original script found here and got it to work with hass with minimum effort. Here’s how I did it. I’m pretty sure there’s a more elegant way to do it but I’m a total noob when it come to writing code so I did all of it with a command line switch and sensor.

  • First of all you’ll need to make your HomBot wifi-capable. There’s an excellent tutorial online written by Rob Van Hamersveld which you guys can find right here. Also make sure your vacuum has a static IP adres.

  • Then, create a shell script called hombot.sh and save it in your home dir. Don’t forget to make it executable with sudo +x

    #!/bin/bash
    curl http://$HOMBOTIP:$HOMBOT_PORT/status.txt > Hombot.txt

This will save a txt file containing all the hombot data. We’ll use this later with a command line sensor to get the data into HASS

  • add the script to your crontab so it can run every minute.

crontab -e

then add the location of hombot.sh

*/1 * * * * /home/pi/hombot.sh

  • Finally, add the following to your configuration.yaml:
switch:
  platform: command_line
  switches:
      HomBot:
        oncmd: "curl GET http://HOMBOTIP:HOMBOTPORT/json.cgi?%7b%22COMMAND%22:%22CLEAN_START%22%7d"
        offcmd: "curl GET http://HOMBOTIP:HOMBOTPORT/json.cgi?%7b%22COMMAND%22:%22HOMING%22%7d"
        statecmd: grep -oP '(?<=JSON_ROBOT_STATE=").*(?<=")' /home/pi/Hombot.txt | sed -e 's/^"//'  -e 's/"$//'
        value_template: '{{ value == "Working" }}'
sensor
  - platform: command_line
    name: Battery
    command: grep -oP '(?<=JSON_BATTPERC=").*' /home/pi/Hombot.txt | sed -e 's/^"//'  -e 's/"$//'
    unit_of_measurement: "%"
  - platform: command_line
    name: Status
    command: grep -oP '(?<=JSON_ROBOT_STATE=").*(?<=")' /home/pi/Hombot.txt | sed -e 's/^"//'  -e 's/"$//'

And that’s basically it. Only thing that’s kind off a bummer is that it takes a maximum of 60 seconds for the status to update when you engage the hombot but I can live with that :).

2 Likes

Looks pretty elegant to me, I have to say.

I took a slightly more complicated approach with a Python script that downloads the status.txt file and that will change the status of the Hom Bot in my HASS UI when the Hom Bot is offline, or when lg.srv has crashed. I also have a second Python script that downloads the statistic.html page and scrapes it for the number of finished runs, so HASS can send me a notification whenever my Hom Bot actually finishes a run.

Oh, and based on the modification timestamp of the local copies of status.txt and statistic.html, my sensors only seem to contact my Hom Bot every 90 seconds…not sure why. I wish I could have that happen more frequently, especially to determine the status of the Hom Bot.

Thanks for sharing this!
I have tried to get this working here… a few changes were needed to get the commands to working but the status update doesn’t work. I changed the switch definition like this:

switch:
  platform: command_line
  switches:
      hombot:
        command_on: "curl GET http://1HOMBOTIP:HOMBOTPORT/json.cgi?%7b%22COMMAND%22:%22CLEAN_START%22%7d"
        command_off: "curl GET http://HOMBOTIP:HOMBOTPORT/json.cgi?%7b%22COMMAND%22:%22HOMING%22%7d"
        command_state: grep -oP '(?<=JSON_ROBOT_STATE=").*(?<=")' /home/pi/Hombot.txt | sed -e 's/^"//'  -e 's/"$//'
        value_template: '{{ return_value == "WORKING" }}'
        friendly_name: LG HomBot

So using command_on and command_off, value_template and “WORKING” as my Hombot returns. I can see the status “WORKING” as a sensor value and also when executing the grep command manually, but the switch never shows “on”, and so switching off is not possible. Any idea what’s wrong?

Care to share these Scripts? I’m moving from Domoticz to HASS, but the delay in the status is kind of annoying…

I could share my scripts, but have you included a scan_interval for your command line sensor(s)? I have mine set to 10, but 5 might work too.

Thanks for the tip of changing the scan_interval, but the Sensor is based on a Txt File that is generated with a Cronjob. And unfortunately this Cronjobs runs once per minute. So If you could share the scripts that would be awesome :slight_smile:

Okay, so here are my Python scripts:

hombot_retrieve_status.py:

#!/usr/bin/python3

import sys
import os
import urllib.request
import time
import stat


##### BEGIN FUNCTIONS #####

def retrieve_value(file, entry):
    with open(file, 'r') as text_file:
        for line in text_file:
            if entry in line:
                return_value = line[line.find("\"")+1:len(line)-2]
                break
    return return_value

def calculate_file_age(file):
    return time.time() - os.stat(file)[stat.ST_MTIME]

###### END FUNCTIONS ######


local_url = "/home/hass/.homeassistant/python/hombot_status.txt"
keyword = sys.argv[1]

if keyword == "JSON_ROBOT_STATE":
    ip_address = "xxx.xxx.xxx.xx"
    remote_url = "http://" + ip_address + ":6260/status.txt"

    ping_result = os.system("ping -c 1 -w2 " + ip_address + " > /dev/null 2>&1")
    if ping_result == 0:
        try:
            urllib.request.urlretrieve(remote_url, local_url)
        except:
            print("lg.srv crashed")
        else:
            print(retrieve_value(local_url, keyword))
    else:
        print("OFFLINE")
else:
    if calculate_file_age(local_url) > 180:
        print("n/a")
    else:
        print(retrieve_value(local_url, keyword))

hombot_retrieve_statistic.py:

#!/usr/bin/python3

import os
import urllib.request
import sys
import time


local_url = "/home/hass/.homeassistant/python/hombot_statistic.html"
keyword = sys.argv[1]

if keyword == "NUM FINISH SB":
    time.sleep(3)
    
    ip_address = "xxx.xxx.xxx.xx"
    remote_url = "http://" + ip_address + ":6260/sites/statistic.html"

    ping_result = os.system("ping -c 1 -w2 " + ip_address + " > /dev/null 2>&1")
    if ping_result == 0:

        try:
            urllib.request.urlretrieve(remote_url, local_url)
        except:
            pass

command = "curl -s file://" + local_url + " | grep \"" + keyword + "\" | awk -F'>' '{print $4}' | awk -F'<' '{print $1}'"
os.system(command)

These scripts are used by the following sensors:

- platform: command_line
  name: "Hombot Status"
  command: "python3 /home/hass/.homeassistant/python/hombot_retrieve_status.py 'JSON_ROBOT_STATE'"
  value_template: "{{ value | capitalize() }}"
  scan_interval: 10

- platform: command_line
  name: "Hombot Battery"
  command: "python3 /home/hass/.homeassistant/python/hombot_retrieve_status.py 'JSON_BATTPERC'"
  unit_of_measurement: '%'
  scan_interval: 60

- platform: command_line
  name: "Hombot Last Clean"
  command: "python3 /home/hass/.homeassistant/python/hombot_retrieve_status.py 'CLREC_LAST_CLEAN'"
  value_template: >-
    {%- if value == "n/a" -%}
      n/a
    {%- else -%}
      {{ value.split('/')[2] }}/{{ value.split('/')[1] }}/{{ value.split('/')[0] }} {{ value.split('/')[3] }}:{{ value.split('/')[4] }}
    {%- endif -%}
  scan_interval: 60

- platform: command_line
  name: "Hombot finished ZZ"
  command: "python3 /home/hass/.homeassistant/python/hombot_retrieve_statistic.py 'NUM FINISH ZZ'"
  scan_interval: 60

- platform: command_line
  name: "Hombot finished SB"
  command: "python3 /home/hass/.homeassistant/python/hombot_retrieve_statistic.py 'NUM FINISH SB'"
  scan_interval: 60

- platform: command_line
  name: "Hombot finished SPOT"
  command: "python3 /home/hass/.homeassistant/python/hombot_retrieve_statistic.py 'NUM FINISH SPOT'"
  scan_interval: 60

The above took alot of Googling, as well as trial and error, as I’m not familiar with Python. In other words: the scripts may not be as elegant or efficient as they could be. They also took some tweaking as the webserver (lg.srv) would originally crash within less than 24 hours, probably due to simultaneous requests.

I have been running these scripts for months now without issues. The webserver lg.srv has been super stable. However, I do occasionally get an error in the HASS log stating that running the command from one of the sensors failed, but I’m putting those down to connection drops.

1 Like

Thanks for the Scripts!