Skip to content

Instantly share code, notes, and snippets.

@sansal54
Created January 21, 2017 19:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sansal54/bea2526ab8e968351a85db3410cc9f65 to your computer and use it in GitHub Desktop.
Save sansal54/bea2526ab8e968351a85db3410cc9f65 to your computer and use it in GitHub Desktop.
InfluxDb Example with Modbus Room Thermostat in Python
import os
import sys
import time
import datetime
import minimalmodbus
from influxdb import InfluxDBClient
minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True
PORT_NAME = 'com7'
SLAVE1_ADDRESS = 1
SLAVE2_ADDRESS = 2
SLAVE3_ADDRESS = 3
BAUDRATE = 9600 # baud (pretty much bits/s).
TIMEOUT = 2 # seconds.
STOPBITS = 1
PARITY = 'N'
MODE = minimalmodbus.MODE_RTU
instrument1 = minimalmodbus.Instrument(PORT_NAME, SLAVE3_ADDRESS, MODE)
instrument1.serial.baudrate = BAUDRATE
instrument1.serial.timeout = TIMEOUT
instrument1.serial.stopbits = STOPBITS
instrument1.serial.parity = PARITY
instrument1.serial.xonxoff = False
instrument1.serial.rtscts = False
instrument1.serial.dsrdtr = False
instrument1.debug = False
instrument1.precalculate_read_size = True
influx_host = 'localhost'
port = 8086
dbname = "environment"
user = "root"
password = "root"
# Generates the necessary payload to post
# temperature data into the InfluxDB
def get_data_points(temperature):
iso = time.ctime()
json_body = [
{
"measurement": "ambient_celcius",
"tags": {"host": influx_host},
"time": iso,
"fields": {
"value": temperature,
}
}
]
return json_body
# Defines the interval on which the capture logic
# will occur
capture_interval = 5.0 # Every 5 seconds
# InfluxDB instance
client = InfluxDBClient(influx_host, port, user, password, dbname)
client.create_database(dbname)
# Read, Report, Repeat
while 1:
temp = instrument1.read_register(40005)
print temp
temperature_data = get_data_points(temp)
client.write_points(temperature_data)
time.sleep(capture_interval)
@trentjw
Copy link

trentjw commented Nov 8, 2021

I have a few April Aire 8800 Thermostats that uses 485 protocol that I can control through a terminal window, I would like to be able to integrate them into Home Assistant, is this something within your Realm of Skills and would you be willing to take a crack at it thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment