Site updated at 2016-08-31 05:05:07 UTC
This commit is contained in:
parent
e9165c0f8b
commit
1fb5b5b9ea
136 changed files with 2212 additions and 1236 deletions
|
@ -4,7 +4,7 @@
|
|||
<title><![CDATA[Category: How-To | Home Assistant]]></title>
|
||||
<link href="https://home-assistant.io/blog/categories/how-to/atom.xml" rel="self"/>
|
||||
<link href="https://home-assistant.io/"/>
|
||||
<updated>2016-08-30T21:57:10+00:00</updated>
|
||||
<updated>2016-08-31T05:03:39+00:00</updated>
|
||||
<id>https://home-assistant.io/</id>
|
||||
<author>
|
||||
<name><![CDATA[Home Assistant]]></name>
|
||||
|
@ -13,6 +13,108 @@
|
|||
<generator uri="http://octopress.org/">Octopress</generator>
|
||||
|
||||
|
||||
<entry>
|
||||
<title type="html"><![CDATA[ESP8266 and MicroPython - Part 2]]></title>
|
||||
<link href="https://home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2/"/>
|
||||
<updated>2016-08-31T04:17:25+00:00</updated>
|
||||
<id>https://home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2</id>
|
||||
<content type="html">< was pretty lame, right? Instead of getting information out of Home Assistant we are going a step forward and create our own sensor which is sending details about its state to a Home Assistant instance.
|
||||
|
||||
<!--more-->
|
||||
|
||||
Beside [HTTP POST](https://en.wikipedia.org/wiki/POST_(HTTP)) requests, MQTT is the quickest way (from the author's point of view) to publish information with DIY devices.
|
||||
|
||||
You have to make a decision: Do you want to pull or to poll? For slowly changing values like temperature it's perfectly fine to wait a couple of seconds to retrieve the value. If it's a motion detector the state change should be available instantly. This means the sensor must take initiative.
|
||||
|
||||
An example for pulling is [aREST](/components/sensor.arest/). This is a great way to work with the ESP8266 based units and the Ardunio IDE.
|
||||
|
||||
### <a class='title-link' name='mqtt' href='#mqtt'></a> MQTT
|
||||
|
||||
You can find a simple examples for publishing and subscribing with MQTT in the [MicroPython](https://github.com/micropython/micropython-lib) library overview in the section for [umqtt](https://github.com/micropython/micropython-lib/tree/master/umqtt.simple).
|
||||
|
||||
The example below is adopted from the work of [@davea](https://github.com/davea) as we don't want to re-invent the wheel. The configuration feature is crafty and simplyfies the code with the usage of a file called `/config.json` which stores the configuration details. The ESP8266 device will send the value of a pin every 5 seconds.
|
||||
|
||||
|
||||
```python
|
||||
import machine
|
||||
import time
|
||||
import ubinascii
|
||||
import webrepl
|
||||
|
||||
from umqtt.simple import MQTTClient
|
||||
|
||||
# These defaults are overwritten with the contents of /config.json by load_config()
|
||||
CONFIG = {
|
||||
"broker": "192.168.1.19",
|
||||
"sensor_pin": 0,
|
||||
"client_id": b"esp8266_" + ubinascii.hexlify(machine.unique_id()),
|
||||
"topic": b"home",
|
||||
}
|
||||
|
||||
client = None
|
||||
sensor_pin = None
|
||||
|
||||
def setup_pins():
|
||||
global sensor_pin
|
||||
sensor_pin = machine.ADC(CONFIG['sensor_pin'])
|
||||
|
||||
def load_config():
|
||||
import ujson as json
|
||||
try:
|
||||
with open("/config.json") as f:
|
||||
config = json.loads(f.read())
|
||||
except (OSError, ValueError):
|
||||
print("Couldn't load /config.json")
|
||||
save_config()
|
||||
else:
|
||||
CONFIG.update(config)
|
||||
print("Loaded config from /config.json")
|
||||
|
||||
def save_config():
|
||||
import ujson as json
|
||||
try:
|
||||
with open("/config.json", "w") as f:
|
||||
f.write(json.dumps(CONFIG))
|
||||
except OSError:
|
||||
print("Couldn't save /config.json")
|
||||
|
||||
def main():
|
||||
client = MQTTClient(CONFIG['client_id'], CONFIG['broker'])
|
||||
client.connect()
|
||||
print("Connected to {}".format(CONFIG['broker']))
|
||||
while True:
|
||||
data = sensor_pin.read()
|
||||
client.publish('{}/{}'.format(CONFIG['topic'],
|
||||
CONFIG['client_id']),
|
||||
bytes(str(data), 'utf-8'))
|
||||
print('Sensor state: {}'.format(data))
|
||||
time.sleep(5)
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_config()
|
||||
setup_pins()
|
||||
main()
|
||||
```
|
||||
|
||||
Subscribe to the topic `home/#` or create a [MQTT sensor](/components/sensor.mqtt/) to check if the sensor values are published.
|
||||
|
||||
```bash
|
||||
$ mosquitto_sub -h 192.168.1.19 -v -t "home/#"
|
||||
```
|
||||
|
||||
```yaml
|
||||
sensor:
|
||||
- platform: mqtt
|
||||
state_topic: "home/esp8266_[last part of the MAC address]"
|
||||
name: "MicroPython"
|
||||
```
|
||||
|
||||
[@davea](https://github.com/davea) created [sonoff-mqtt](https://github.com/davea/sonoff-mqtt). This code will work on ESP8622 based devices too and shows how to use a button to control a relay.
|
||||
|
||||
]]></content>
|
||||
</entry>
|
||||
|
||||
<entry>
|
||||
<title type="html"><![CDATA[Github-style calendar heatmap of device data]]></title>
|
||||
<link href="https://home-assistant.io/blog/2016/08/19/github-style-calendar-heatmap-of-device-data/"/>
|
||||
|
@ -390,115 +492,6 @@ One of the graphs created with this tutorial.
|
|||
Thanks to the magic of Jupyter, all of the code is customizable: want to selectively display your data, only covering a specific entity? Sure thing! Want to change the properties of the plots? No problem!
|
||||
|
||||
While you learn and explore your IoT data, we will be working on providing more ready-to-use Jupyter Notebooks. Feel free to ask questions or provide suggestions. Would you like to see a specific visualization? Is there a particular facet of data you’re interested in? Let’s talk about it, let’s dive into the world of data together!
|
||||
]]></content>
|
||||
</entry>
|
||||
|
||||
<entry>
|
||||
<title type="html"><![CDATA[Visualize your IoT data]]></title>
|
||||
<link href="https://home-assistant.io/blog/2016/07/19/visualizing-your-iot-data/"/>
|
||||
<updated>2016-07-19T16:00:00+00:00</updated>
|
||||
<id>https://home-assistant.io/blog/2016/07/19/visualizing-your-iot-data</id>
|
||||
<content type="html">< is tracking everything that is going on within Home Assistant. This means that you have access to all stored information about your home. Our history is not a full-fledged graphical processing and visualization component as you may know from systems and network monitoring tools. The current limitation is that you only can select a day for a visual output of your information and not a period. Also, there is no possibility to drill down on a specific entity.
|
||||
|
||||
This blog post will show you ways to export data for reporting, visualization, or further analysis of automation rules.
|
||||
|
||||
<!--more-->
|
||||
|
||||
In this blog post I use the temperature of the [Aare](https://en.wikipedia.org/wiki/Aare) river close to where I live as a show case. The temperatures were recorded with the [Swiss Hydrological Data sensor](/components/sensor.swiss_hydrological_data/) and the name of the sensor is `sensor.aare`.
|
||||
|
||||
The database is stored at `<path to config dir>/.homeassistant/home-assistant_v2.db` as [SQLite database](https://www.sqlite.org/). In all examples we are going to use the path: `/home/ha/.homeassistant/home-assistant_v2.db`
|
||||
|
||||
If you are just curious what's stored in your database then you can use the `sqlite3` command-line tool or a graphical one like [DB Browser for SQLite](http://sqlitebrowser.org/).
|
||||
|
||||
The table that is holding the states is called `states`. The `events` tables is responsible for storing the events which occurred. So, we will first check how many entries there are in the `states` table. `sqlite3` needs to know where the databases is located. To work with your database make sure that Home Assistant is not running or create a copy of the existing database. It's recommended to work with a copy.
|
||||
|
||||
```bash
|
||||
$ sqlite3 /home/ha/.homeassistant/home-assistant_v2.db
|
||||
SQLite version 3.11.0 2016-02-15 17:29:24
|
||||
sqlite> SELECT count(*) FROM states;
|
||||
24659
|
||||
```
|
||||
|
||||
Let's have a look at a sample [SQL](https://en.wikipedia.org/wiki/SQL) query. This query will show all states in a period for the sensor `sensor.aare`.
|
||||
|
||||
```sql
|
||||
SELECT state, last_changed FROM states
|
||||
WHERE
|
||||
entity_id = 'sensor.aare'
|
||||
AND
|
||||
last_changed BETWEEN
|
||||
'2016-07-05 00:00:00.000000' AND '2016-07-07 00:00:00.000000';
|
||||
```
|
||||
|
||||
The SQL statement can be formed that it fits exactly what you need. This means that you can process the data in any way you want for further use. Often it makes sense to eliminate certain entries like `Unknown` or peaks.
|
||||
|
||||
If the above query is executed in DB Browser for SQLite you would be able to save the sensor's graph as png.
|
||||
|
||||
<p class='img'>
|
||||
<img src='https://home-assistant.io/images/blog/2016-07-reporting/db-browser.png' />
|
||||
Visualization with DB Browser for SQLite
|
||||
</p>
|
||||
|
||||
You may ask: Why not do this with LibreOffice Calc or another spreadsheet application? As most spreadsheet applications are not able to work directly with SQLite database we are going to export the data from the database to [CSV](https://en.wikipedia.org/wiki/Comma-separated_values).
|
||||
|
||||
```bash
|
||||
$ sqlite3 -header -csv /home/ha/.homeassistant/home-assistant_v2.db "SELECT last_changed, state FROM states WHERE entity_id = 'sensor.aare' AND last_changed BETWEEN '2016-07-05 00:00:00.000000' AND '2016-07-07 00:00:00.000000';" > sensor.csv
|
||||
```
|
||||
|
||||
The ordering for the `SELECT` was changed to get the time stamps first and then the state. Now we can import the CSV file into the application of your choice, here it's LibreOffice Calc.
|
||||
|
||||
<p class='img'>
|
||||
<img src='https://home-assistant.io/images/blog/2016-07-reporting/libreoffice-import.png' />
|
||||
Import of the CSV file
|
||||
</p>
|
||||
|
||||
After the import a graph can be created over the existing data.
|
||||
|
||||
<p class='img'>
|
||||
<img src='https://home-assistant.io/images/blog/2016-07-reporting/libreoffice-graph.png' />
|
||||
Graph in LibreOffice
|
||||
</p>
|
||||
|
||||
You can also use [matplotlib](http://matplotlib.org/) to generate graphs as an alternative to a spreadsheet application. This is a powerful Python 2D plotting library. With the built-in support for SQLite in Python it will only take a couple lines of code to visualize your data.
|
||||
|
||||
```python
|
||||
import sqlite3
|
||||
from matplotlib import dates
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import homeassistant.util.dt as dt
|
||||
|
||||
values = []
|
||||
timestamps = []
|
||||
|
||||
conn = sqlite3.connect('/home/ha/.homeassistant/home-assistant_v2.db')
|
||||
data = conn.execute("SELECT state, last_changed FROM states WHERE "
|
||||
"entity_id = 'sensor.aare' AND last_changed BETWEEN "
|
||||
"'2016-07-05 00:00:00.000000' AND "
|
||||
"'2016-07-07 00:00:00.000000'")
|
||||
|
||||
for x in data:
|
||||
timestamps.append(dates.date2num(dt.parse_datetime(x[1])))
|
||||
values.append(float(x[0]))
|
||||
|
||||
plt.plot_date(x=timestamps, y=values, fmt="r-")
|
||||
plt.ylabel('Temperature')
|
||||
plt.xlabel('Time line')
|
||||
|
||||
plt.savefig('sensor.png')
|
||||
```
|
||||
|
||||
Creating a connection to the database and executing a query is similar to the ways already seen. The return values from the query are splitted into two lists. The time stamps must be converted in an value which is accepted by matplotlib and then the graph is generated and saved as image.
|
||||
|
||||
<p class='img'>
|
||||
<img src='https://home-assistant.io/images/blog/2016-07-reporting/mpl-sensor.png' />
|
||||
Sensor graph generated by matplotlib
|
||||
</p>
|
||||
|
||||
Most of the graphs are pretty ugly. So, further beautification will be needed. If you have created a nice report including some amazing graphs then the Home Assistant community would be grateful for sharing them in our [forum](https://community.home-assistant.io/).
|
||||
|
||||
]]></content>
|
||||
</entry>
|
||||
|
||||
|
|
|
@ -98,6 +98,44 @@
|
|||
|
||||
<h2>2016</h2>
|
||||
|
||||
<article>
|
||||
<div class="grid">
|
||||
|
||||
<div class="grid__item one-fifth palm-one-whole">
|
||||
<time datetime="2016-08-31T04:17:25+00:00" pubdate>
|
||||
<span class='month'>Aug</span> <span class='day'>31</span>
|
||||
</time>
|
||||
</div>
|
||||
<div class="grid__item four-fifths palm-one-whole">
|
||||
<h1 class="gamma"><a href="/blog/2016/08/31/esp8266-and-micropython-part2/">ESP8266 and MicroPython - Part 2</a></h1>
|
||||
|
||||
<footer class="meta">
|
||||
<span>
|
||||
<i class="icon-tags"></i>
|
||||
<ul class="tags unstyled">
|
||||
|
||||
|
||||
<li><a class='category' href='/blog/categories/esp8266/'>ESP8266</a></li>
|
||||
|
||||
<li><a class='category' href='/blog/categories/how-to/'>How-To</a></li>
|
||||
|
||||
<li><a class='category' href='/blog/categories/mqtt/'>MQTT</a></li>
|
||||
|
||||
<li><a class='category' href='/blog/categories/micropython/'>Micropython</a></li>
|
||||
|
||||
|
||||
</ul>
|
||||
</span>
|
||||
</footer>
|
||||
|
||||
<hr class="divider">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</article>
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
<div class="grid">
|
||||
|
||||
|
@ -720,6 +758,12 @@
|
|||
<ul class="divided">
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2016/08/31/esp8266-and-micropython-part2/">ESP8266 and MicroPython - Part 2</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2016/08/28/notifications-hue-fake-unification/">0.27 is here to break eggs and take names: notifications, Hue fakery, safety and unification come to Home Assistant</a>
|
||||
</li>
|
||||
|
@ -743,12 +787,6 @@
|
|||
</li>
|
||||
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2016/08/07/optimizing-the-home-assistant-mobile-web-app/">Optimizing the Home Assistant mobile web app</a>
|
||||
</li>
|
||||
|
||||
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue