Alexa, Roomba, and presence detection

I wanted to create an automation that runs Roomba once a week, but only when no-one is home. I am having a devil of a time getting reliable presence detection with IOS, so I decided to leave one manual step in the process. Now, when I leave, I say “Alexa tell Home Assistant we’re leaving” and, if it has been 7 days or more since the last time Roomba ran, then, Home Assistant will start Roomba. This intent also turns off all the lights. Special thanks to koalazak and the dorita980 project for getting the roomba stuff up and running.

Here’s the code:
First, I have a bash script that writes the current date to a text file, then it starts Roomba. Saving the date to a text file allows the date to persist through any kind of reboot.

/bin/date "+echo %b %d %Y" > /users/treno/.homeassistant/scripts/roombadate.sh
cd /Users/treno/projects/Roomba
node roombastart.js

NOTE: this failed time after time until I finally realized that the Home Assistant process doesn’t have any PATH. So I have to hard-code the path to the date command as /bin/date.

Then I have a command line sensor to read that text file

 - platform: command_line
    name : roombaRunDate
    command: /users/treno/ha/scripts/roombadate.sh
    scan_interval : 1

Now I have everything I need in Home Assistant to set up the SCRIPT for Alexa to run

goodbye:
  sequence:
    - service: homeassistant.turn_off
      entity_id: group.livingroom
    - service: homeassistant.turn_off
      entity_id : group.office
    - service: homeassistant.turn_off
      entity_id : switch.garage
    - service: homeassistant.turn_off
      entity_id : group.bedroom
    - condition: template
      value_template: '{{(((as_timestamp(now())-as_timestamp(strptime(states.sensor.roombarundate.state,"%b %d %Y")))/86400)| int)>6}}'
    - service: shell_command.rroomba

The template here does the date math to determine if it has been 7 days.

And Finally, the ALEXA intent code:

  GoodbyeIntent:
    action: 
      service: script.turn_on
      entity_id: script.goodbye
    speech: 
      type: plaintext
      text: >
        "The house is in away mode."
        {% set elapsed = ((as_timestamp(now())-as_timestamp(strptime(states.sensor.roombarundate.state,"%b %d %Y")))/86400)| int%}
        {%if elapsed < 6 %}
        "Roomba will run in " {{7- elapsed}} "days"
        {% elif elapsed < 7 %}
        "Roomba will run tomorrow"
        {% else %}
        "Roomba will clean up while you are gone"
        {% endif %}

It was a fun couple of hours figuring out how to get all this stuff to work together.
I hope it helps someone out there with their own project.

1 Like

Nice share. For ios i found bluetooth to be most accurate. Unsure how that works with a bigger house and more thick walls, but for me works perfectly. You can combine with nmap or router integration for extra precision, but I found that for me worked great even without.

Yea, I tried bluetooth tracker, but my HA server is too far away. I plan to try beacons at some point in the future.

Bluetooth or Bluetooth LE? I found for me only LE one gets it slow

This is awesome. I exposed an input Boolean to HomeKit and use a homekit automation to flip that switch for presence and that has been 100% so far. I struggled with owntracks and iOS app before that

1 Like

Does anyone know if this will work with Hass.io?