KNX to Zabbix Gateway

Hey there,

I’ve created a gateway between KNX and Zabbix: if a value is written to a certain GroupAddress on KNX, its GA and corresponding value are sent via a simple bash script and via zabbix_sender to a Zabbix monitoring system. Via Zabbix you can collect and visualize smart home data as well as create notifications if something is wrong.

Prerequisites:

A working configuration looks like this:

knx.things:

Type switch : basement_storageroom_presence	"Basement storage room presence"	[ ga="1/1/2" ]

knx.items:

Switch basement_storageroom_presence	"Basement storage room presence [%s]"	{ channel="knx:device:bridge:generic:basement_storageroom_presence" }

knx.rules:

// Basement storage room presence
rule "basement_storageroom_presence"
when
	Item basement_storageroom_presence changed
then
	if(basement_storageroom_presence.state instanceof OnOffType) {
		val basement_storageroom_presence_value = if (basement_storageroom_presence.state == ON) 1 else 0

		executeCommandLine(Duration.ofSeconds(1), "/usr/local/bin/zabbix_send.sh", "1/1/2", basement_storageroom_presence_value.toString)
	}
end

zabbix_send.sh:

#!/bin/bash
#####################################################################
# /usr/local/bin/zabbix_send.sh
#
# Send a combination of GA and value to zabbix_sender.
#####################################################################


#####################################################################
# Configuration
#####################################################################

#-----------------------------
# General options
#-----------------------------
# URL or ip address of your Zabbix server
zabbix_server="zabbix.example.com"

# Technical name of monitored host (as registered in Zabbix frontend)
registered_host="knx_gateway"

# Arguments in the order '[GA] [data]', e.g. '1/1/2 54'
# where '1/1/2' is the GA you want to send
# and '54' is the value you want to send (e.g. temperature)
group_address="$1"
data="$2"


#####################################################################
# Preparations
#####################################################################

#-----------------------------
# Make sure an option argument has been specified
#-----------------------------
if [ -z "$group_address" ]; then
	echo "No 'GA' option specified. Please call this script as '$0 [GA] [data]', e.g. '$0 1/1/2 1'. Aborting."
	exit 1
fi


#-----------------------------
# Make sure an option argument has been specified
#-----------------------------
if [ -z "$data" ]; then
	echo "No 'data' option specified. Please call this script as '$0 [GA] [data]', e.g. '$0 1/1/2 1'. Aborting."
	exit 1
fi


#-----------------------------
# Check if the zabbix_sender binary is present
#-----------------------------
if [ ! -x "/usr/bin/zabbix_sender" ]; then
	echo "'zabbix_sender' binary not found or not executable, cannot continue. Aborting."
	exit 1
fi


#####################################################################
# Send to Zabbix
#####################################################################

# Re-format this GA from 'x/x/x' to a canonified 'x_x_x',
# since Zabbix cannot parse slashes
group_address_canonified=$(echo $group_address | sed 's/\//_/g;' )


# Debug
#echo "/usr/bin/zabbix_sender -z $zabbix_server -s openhab -k $group_address_canonified -o $data"


# Send a key value pair to Zabbix
/usr/bin/zabbix_sender -z $zabbix_server -s $registered_host -k $group_address_canonified -o $data

# Feedback
if [ "$?" != "0" ]; then
	echo "Sending message to Zabbix server failed. Aborting."
	exit 1
fi

Comments, ideas and suggestions are welcome!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.