Let your plant twitter if it needs to be watered

You often forget to water your plants? We already created a guide how to make sure you will be informed to water your plant when you’re coming home. But let’s try something that is a bit crazier! Wouldn’t it be cool if your plant would have it’s own Twitter account and tell you what to do?

What you need

  • one or more Mi Flora Plant sensors
  • a Raspberry Pi or PC running Home Assistant
  • a Twitter account

Installing the software

The base configuration of the Mi Plant sensor has been documented in our first flower sensor article.

Configuring the Twitter integration

If you have already a twitter account, go to apps.twitter.com and create a new App. You need to input some data and then you will get the consumer key/secret and you can create an access token.

Now, configure a notifier in your configuration.yaml file as follows:

notify:
  name: pflanzentwitter
  platform: twitter
  consumer_key: xxxxxxxxx
  consumer_secret: xxxxxxxx
  access_token: xxxxxxxx
  access_token_secret: xxxxxxx

Just copy the tokens from the developer web site into the configuration.

Create some twitter rules

Now, it your turn to find some cool post for your plant. Here is an example that I use. I’ve create a seperate automations.yaml file, that will be included in the main configuration:

- alias: Basil - sunny
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_light_intensity
    above: 9800
  action:
    service: notify.pflanzentwitter
    data:
      message: "Basil: Nice sunny day today \U0001f60e"

- alias: Basil - dark
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_light_intensity
    below: 100
  action:
    service: notify.pflanzentwitter
    data:
      message: "Basil: It's getting dark outside, good night! \U0001f303"

- alias: Basil - first light
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_light_intensity
    above: 100
  action:
    service: notify.pflanzentwitter
    data:
      message: "Basil: Good morning! \U0001f305"

- alias: Basil - above 30 degree
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_temperature
    above: 30
  action:
    service: notify.pflanzentwitter
    data:
      message: "Basil: Hot day today, it's over 30 degree celsius now. \U0001f321"

- alias: Basil - water warning 2
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_moisture
    below: 25
  action:
    service: notify.pflanzentwitter
    data:
      # Cactus emoticon
      message: "Basil: What about some water for me? \U0001f335"

- alias: Basil - water warning 3
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_moisture
    below: 20
  action:
    service: notify.pflanzentwitter
    data:
      # Skull emoticon
      message: "Basil: Water me or I will die of thirst. \U0001f480"

- alias: Basil - water warning 4
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_moisture
    below: 10
  action:
    service: notify.pflanzentwitter
    data:
      # Skull emoticon
      message: "Basil: Help! Water! \U0001f480\U0001f480\U0001f480"

- alias: Basil - enough water
  trigger:
    platform: numeric_state
    entity_id: sensor.basilikum_moisture
    above: 50
  action:
    service: notify.pflanzentwitter
    data:
      message: "Basil: I'm feeling better, thank you for the water. \U0001f3a0"

You might ask yourself, what those crazy \U000xxxx numbers are? These are just emoticons. Have a look here for their unicode character codes.

Monitor your plants with Home Assistant

I love to have a lot of plants in my apartment, but unfortunately I sometimes forget to water them. So why not using modern technology to make sure, I don’t ever forget it again.

What you need

  • A Raspberry Pi or a Linux-PC with a supported Bluetooth LE interface.
  • Bluez
  • Mi Plant flower sensor for every plant
  • Home assistant

Setup

Home assistant

First you should install Home Assistant and make sure everything works well. Home assistant provides good documentation how to setup the platform.

Bluetooth software

The Linux bluetooth software might not yet be installed on your system.

Check communication with the plant sensor

The easiest way to make sure your PC can read data from the sensor is using the hcitool command line tool.

hcitool lescan
LE Scan ...
XX:XX:XX:XX:XX:XX (unknown)
XX:XX:XX:XX:XX:XX Flower mate

It might list a lot of other devices. Just make sure, it can also see the “Flower mate” device. If you can’t see this device, the distance between the flower sensor and your PC might be too large. Try moving the plant near your PC and check if it works then.

Integrate polling into Home Assistant

First, you need to poll the data from the sensor in Home Assistant:

sensor
  platform: miflora
  mac: xx:xx:xx:xx:xx:xx
  name: Flower 1
  force_update: false
  monitored_conditions:
   - moisture
   - light
   - temperature
   - conductivity

As the MAC address, you need to use the address that is shown in the hcitool lescan output.

Notify yourself when you come home

It doesn’t make much sense to send you a notification during the day that you should water the flowers. You might have forgotten it already when you come home. So let’s just send a notification when you come home.

Setup presence detection

Home Assistant documentation how to do this. There are a lot of different device trackers. Have a look and decide what works best for you.

Setup notifications

The next step is sending a notification to you. For this guide I use Pushetta, but Home Assistant provides a large number of notification mechanisms. Have a look at them and select the one that fits best for you.

An example notifier configuration might look like this:

notify
  name: pushetta
  platform: pushetta
  channel_name: mychannel
  api_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Define the automation

Now comes the new rule:

automation:
  alias: Alarm me to water plants
  trigger:
    platform: state
    entity_id: device_tracker.myname
    from: 'not_home'
    to: 'home'
  condition:
    condition: numeric_state
    entity_id: sensor.flower1_moisture
    below: 20
  action:
    service: notify.pushetta
    data:
      message: 'Water Flower 1!'

What does this do? First it checks it check, if you came from (device tracker changes from not_home to home) and check then if the moisture is below a well-defined threshold. The value might be a bit different for your plant, you need to adjust this. If the moisture is too low, it sends a notification via Pushetta.

You can combine multiple plants in the condition statement or create an automation for every single plant. I personally prefer the latter as I then know exactly which plant I should water.

Long-term monitoring

It often helps to have long-term data available to optimize not only watering, but also fertilize in the right intervals. While you can store the whole history within Home assistant, it isn’t really optimized for this use case. Therefore I recommend to use InfluxDB for this. This is a time-series database specifically designed to store sensor data over a long period. To visualize the data, I use Grafana. With this, you can create nice reports like this:
TODO

Monitoring your laundry

You might know the problem that you put laundry in your washing machine or the dryer and forget about it.  While the dryer is usually less critical, it’s no fun to leave the laundry in the washing machine for hours.

Nolan Gilley wrote a nice article how to use accelerator sensors to monitor your laundry with Home Assistant. Check it out.

protoboard

I will further look into this. It would be very cool to use BLE for this and use energy harvesting from the vibrations to power the circuit. But unfortunately piezo elements for energy harvesting are quite expensive.

Home Assistant: Logging to a central server

As the Raspberry Pi is relatively cheap, you might think: Why not having multiple Home Assistant installations running in my house? This gives you some advantages:

  • You might monitor temperature and humidity in different rooms.
  • If one instance fails, the other can still do the same job.

However, even with multiple HA instances, it might make sense to have the sensor data logged to a central instance. This makes data analytics much easier.

I use InfluxDB to store the sensor data. With our latest patch that will be included in Home Assistant 0.25, it is now possible to distinguish between the data from the different HA instances.

The logging configuration on the two HA instances would then look like this:

influxdb:
  host: 192.168.1.199
  tags:
    instance: living_room
influxdb:
  host: 192.168.1.199
  tags:
    instance: balcony

Accessing your Home assistant installation outside your house

Having a home automation solution that is only available inside your house might work for some people, but most people want to control their home also form the outside.

There are different options to achieve this in a secure way.

VPN

IC196810Using a VPN is usually the most secure solution. Even if there is a major security bug in your Home Assistant installation, attackers still can’t compromise your system as it isn’t publicly available on the internet. Many ISP’s today already provide a VPN service. You need to check with your ISP if it is available and how to configure it.

The disadvantage of a VPN is that you have to connect to the VPN first. This becomes an even bigger issue, if you use 3rd party applications (e.g. device trackers) that want to connect to your Home Assistant server. If the VPN isn’t connected, they will fail and some status informations might not be updated correctly.

HTTPS

Using a public HTTPS server is not the most secure solution, but still ok in general. Just note that you have to always check for security problems and update your Home Assistant installation regularly.

letsencryptFor HTTPS you will need certificates. A simple and free option today is Let’s encrypt. There is already a guide on the home assistant web site that shows how to use Home Assistant with Let’s Encrypt.

An additional security improvement can be implemented by using a web application firewall in front of the web server. While you can do this by yourself, it it quite complicated. Another option is using Cloudflare.  While you can’t use your own rules in the free version, it will still provide some basic security and protection against general attack patterns.

Comparison of both variants

VPN HTTPS
Security Highest, as your system is protected even in case of security flaws in Home Assistant Ok, but you need to make sure that you regularly patch Home Assistant
Ease of setup Relatively easy if your ISP already offers it, otherwise complicated Medium, guides are available
Ease of use  Medium, you always have to connect the VPN first Very easy

Blocking sensors in Home Assistant

I really like the open source software Home Assistant. However, today I noticed that the whole system wasn’t working anymore. Ok, sometimes a Reboot helps, but not this time. Looking further into this I found the cause of this problem. One of the WiFi enabled switches wasn’t responding anymore. And Home Assistant completely blocked.

This is a major issue in a home automation software as you can’t be sure that in a complex environment every component will work as expected. An issue has been opened for this problem. I hope that there is a easy way to fix this kind of problem.

If your Home Assistant isn’t working anymore, start it from command line and check the output. You might find a subsystem that blocks the startup.

Bluetooth problems on the Raspberry Pi 3

The Bluetooth tracker in Home Assistant is a cool device as it allows you to track if people are home based on their mobile device. With the Raspberry Pi 3’s onboard bluetooth chip, this is a great features as it does not even require additional hardware. However, I’ve had major problems getting it running. Bluetooth was enabled on the mobile phone, but Home Assistant just did not find any device.

Ok, checking from command line:

[email protected]:~# hcitool scan
Scanning ...

No bluetooth devices, even if there are bluetooth enabled devices available. Let’s try with “blutoothctl”:

[email protected]:~# sudo bluetoothctl
[NEW] Controller B8:27:EB:76:70:7C raspberrypi [default]
[bluetooth]# devices
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:76:70:7C Discovering: yes
[NEW] Device 04:69:F8:xx:xx:xx 04-69-F8-xx-xx-xx
[NEW] Device 88:C6:26:xx:xx:xx 88-C6-26-xx-xx-xx
[bluetooth]# quit
[DEL] Controller B8:27:EB:76:70:7C raspberrypi [default]

Clearly, blutoothctl sees other devices. Maybe a daemon isn’t running?

[email protected]:~# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Mon 2016-06-13 15:53:36 UTC; 1h 54min ago
     Docs: man:bluetoothd(8)
 Main PID: 729 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─729 /usr/lib/bluetooth/bluetoothd

Jun 13 15:53:36 raspberrypi systemd[1]: Starting Bluetooth service...
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Bluetooth daemon 5.23
Jun 13 15:53:36 raspberrypi systemd[1]: Started Bluetooth service.
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Starting SDP server
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Bluetooth management interface 1.10 initialized
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Sap driver initialization failed.
Jun 13 15:53:36 raspberrypi bluetoothd[729]: sap-server: Operation not permitted (1)

Ok, this looks strange. While bluetoothd is running, there is an error message from sap-server. Looking for the error message, I found a long thread on raspberypi.org. The solution: add a command line option to the Bluetooth daemon

Edit the file /etc/systemd/system/bluetooth.target.wants/bluetooth.service and add the option “–noplugin=sap” to the ExecStart line:

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

Ok, let’s check the daemon again:

[email protected]:~# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Mon 2016-06-13 17:56:35 UTC; 9s ago
     Docs: man:bluetoothd(8)
 Main PID: 19106 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─19106 /usr/lib/bluetooth/bluetoothd --noplugin=sap

Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Bluetooth daemon 5.23
Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Starting SDP server
Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Excluding (cli) sap
Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Bluetooth management interface 1.10 initialized
Jun 13 17:56:35 raspberrypi systemd[1]: Started Bluetooth service.

This looks better. However, even after a reboot,

hcitool scan

still does not show any Bluetooth device.

To be continued …

Using Home Assistant to control your home (and more)

If you’re looking for an home automation software, you will find a lot of projects. There is a huge interest in this area and a lot of developers working on different projects. Many focus on the visualisation. While it is important to have a good user interface, the main focus of an automation system should be it’s backend.
One project I really like is Home Assistant. It uses a very clean backend/frontend separation.You can easily add your own user interface by just using its web API. It is written in Python. That makes it easier for me to add missing functionalities.

An architecture based on Home assistant could look like this:

ha-architecture

KNX isn’t supported out-of-the box in the current Home Assistant release (June 2016). However, I already did some programming and will work on integrating it into the official Home Assistant release.

Control EDIMAX SP-1101 or SP-2101 from Home Assistant

The Edimax SP-1101W and SP2101W are supported in a standard home assistant installation. However, you need to configure a user name. During setup of the smart switch you can set a new password, but never a user name. However, it is easy: the username is “admin”.
To control an Edimax switch from Home assistant, just add the following block and set the correct host, password and name parameters for your installation.

switch:
  platform: edimax
  host: 
  username: admin
  password: 
  name: