Skip to content

Instantly share code, notes, and snippets.

View h3xagn's full-sized avatar

h3xagn h3xagn

View GitHub Profile
@h3xagn
h3xagn / telegraf-processor.conf
Last active June 5, 2025 03:59
Telegraf Processor for OPC UA
[[processors.starlark]]
namepass = ["opcua_plc1", "opcua_mplc"]
order = 1
source = '''
def apply(metric):
new_fields = {}
for field_key in list(metric.fields.keys()):
if field_key != "Quality":
tag_value = field_key
metric.tags['tag_name'] = tag_value
@h3xagn
h3xagn / data-tile-query.sql
Created May 24, 2025 15:00
Data queries for Grafana dashboard tiles
-- TimeScaleDB
SELECT
time_bucket('180 seconds', bucket) AS agtime,
AVG(avg) as "lab_indoor_temp"
FROM
timeseries_60s
WHERE
$__timeFilter("bucket") AND
tagname = 'lab_indoor_temp'
GROUP BY agtime
@h3xagn
h3xagn / influxdb.py
Created May 24, 2025 14:56
Bulk insert for InfluxDB 3
def WriteInfluxDB(timestamp: datetime, points_data: list) -> None:
try:
# Create a list of Point objects
points = []
for point_data in points_data:
tag_name = point_data["tag_name"]
tag_value = point_data["tag_value"]
point = (
@h3xagn
h3xagn / timescaledb-v2.py
Created May 24, 2025 14:55
Bulk insert for new data
def WriteSQLToDB(timestamp: datetime, data_points: list) -> None:
try:
sqlEngine = create_engine(
db_conn,
pool_recycle=300,
pool_pre_ping=True,
connect_args={"connect_timeout": 20},
)
# Build a single SQL statement with multiple VALUES
@h3xagn
h3xagn / influxdb3-nodered.js
Last active May 24, 2025 14:52
NodeRed runction to create line protocol message for InfluxDB
let payload = msg.payload;
let timestamp = new Date(payload.timestamp);
timestamp = Math.floor(timestamp.getTime() / 1000);
let lp_msg = {
"payload": "homedb,sensor=lab_indoor_temp value=" + payload.value + " " + timestamp
}
return lp_msg;
@h3xagn
h3xagn / geyserwala.py
Created June 10, 2024 20:09
Building a home energy monitoring dashboard for your clevrHome
import time
import requests
from datetime import datetime
# URL to read the data from Geyserwala
URL = "http://192.168.2.20/api/value?f=status,tank-temp,collector-temp,element-demand,pump-status,element-seconds,element-cycles,pump-seconds,pump-cycles"
# Connect to Geyserwala and read the parameters
while True:
@h3xagn
h3xagn / solarman.py
Created June 10, 2024 20:09
Building a home energy monitoring dashboard for your clevrHome
import time
from datetime import datetime
from deye_controller import HoldingRegisters
from pysolarmanv5 import PySolarmanV5
# Parameters to read from the inverter
# [register, name, unit, multiplier]
parameters = [
[172, "grid_ct_power_W", "W", 1],
@h3xagn
h3xagn / timescaledb.py
Created June 10, 2024 20:08
Building a home energy monitoring dashboard for your clevrHome
from datetime import datetime
from sqlalchemy import create_engine, text
# Database connection string
db_conn = "postgresql://username:password@192.168.2.10:5432/tsdb"
# Write the data to the database
def WriteSQLToDB(timestamp: datetime, tag_name: str, tag_value: float) -> None:
try:
sqlEngine = create_engine(db_conn, pool_recycle=300, pool_pre_ping=True, connect_args={"connect_timeout": 20})
@h3xagn
h3xagn / tags.json
Created June 11, 2023 10:50
Build an OPC UA bridge in Python: https://h3xagn.com
{
"ns=3;s=\"analog data\".\"Level\"": {
"tagName": "analogdata_Level"
},
"ns=3;s=\"analog data\".\"Current\"": {
"tagName": "analogdata_Current"
},
"ns=3;s=\"analog data\".\"Speed\"": {
"tagName": "analogdata_Speed"
},
@h3xagn
h3xagn / opcua-bridge.py
Created June 11, 2023 10:07
Build an OPC UA bridge in Python: https://h3xagn.com
# Import libraries
import asyncio
import json
# Import OPC UA library
from asyncua import Client, Node
# Create handler for subscriptions
class SubscriptionHandler: