Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
LarsBergqvist / mqtt_to_influxdb.py
Created May 16, 2017 16:29
MQTT to InfluxDB with more print tracing
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import datetime
import time
from influxdb import InfluxDBClient
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
client.subscribe("Home/#")
//
// This is an example on MQTT publish from an ESP8266 board
// to an MQTT broker (I have used a local Mosquitto running on a Raspberry Pi)
// This example uses the PubSub client library (https://github.com/knolleary/pubsubclient)
// Install it in the Arduino IDE before compiling the sketch
// Sensor values are fetched from an indoor DHT22 sensor and an outdoor DHT22 sensor
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import datetime
import time
from Adafruit_IO import MQTTClient
from adafruit_credentials import ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY
from mylogger import logger
#
# Subscribes to messages via a local MQTT broker
@LarsBergqvist
LarsBergqvist / automation.yaml
Last active November 13, 2018 04:46
Home Assistant automation based on presence detection
automation:
- alias: Turn on window lights when Lars gets home
trigger:
platform: zone
entity_id: device_tracker.my_iphone
zone: zone.home
event: enter
action:
service: switch.turn_on
entity_id: group.window_lights
@LarsBergqvist
LarsBergqvist / zone_examples.yaml
Created April 2, 2017 09:47
Zone examples for Home Assistant
zone 1:
name: Work (Lars)
latitude: 61.422185
longitude: 18.560426
radius: 100
icon: mdi:worker
zone 2:
name: School
latitude: 61.494772
@LarsBergqvist
LarsBergqvist / systemmonitor.py
Created March 15, 2017 04:03
Make MQTT messages from system monitoring values on a Raspberry Pi
#!/usr/bin/env python3
import paho.mqtt.publish as publish
from subprocess import check_output
from re import findall
import psutil
import sys
def get_temp():
temp = check_output(["vcgencmd","measure_temp"]).decode("UTF-8")
return(findall("\d+\.\d+",temp)[0])
@LarsBergqvist
LarsBergqvist / subscriber_influxdb.py
Last active October 27, 2020 02:14
A service that subscribes to MQTT topics and stores the received data in an InfluxDB database
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import datetime
import time
from influxdb import InfluxDBClient
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("Home/#")
@LarsBergqvist
LarsBergqvist / influxdb_demo
Last active March 2, 2017 18:11
Example on creating and using an InfluxDB database
$ influx -precision rfc3339 // starts the InfluxDB shell and shows time in rfc3339 format
Connected to http://localhost:8086 version 1.2.0
InfluxDB shell version: 1.2.0
> create database demo // Creates a new database
> use demo
Using database demo
> insert mysensor1 value=0.51 // Insert new datapoints
> insert mysensor1 value=0.53 // for the measurement
> insert mysensor1 value=0.45 // "mysensor1"
> select * from mysensor1 // Get the whole time series
@LarsBergqvist
LarsBergqvist / configuration.yaml
Last active February 9, 2021 22:53
Indoor light group and good night automation rule for Home Assistant
group:
indoor_lights:
name: Indoor lights
entities:
- switch.window_lights_downstairs
- switch.wall_lights_downstairs
- switch.window_lights_upstairs
- switch.floor_light_upstairs
automation:
@LarsBergqvist
LarsBergqvist / automation.yaml
Last active November 8, 2021 18:02
Home Assistant automation for turning lights on and off based on sunset and sunrise
automation:
- alias: Turn on garden lights when sun sets
trigger:
platform: sun
event: sunset
action:
service: switch.turn_on
entity_id: switch.garden_lights
- alias: Turn off garden lights when sun rises