OpenHab Modbus

In this article will be described the use of the Energy Meter SDM120 as an example about interfacing an RS485 device to OpenHAB with the Modbus protocol.


In the CM3-Home (picture below)

the RS485 bus is interfaced to the Compute Module 3 via an FTDI FT4232. It allows 4 full serial ports from a single USB interface. Two of them are dedicated to two separate, fully isolated RS485 ports, including TX enable control, usable for protocols like Modbus and DMX512.

Further details on RS-485 interface article.

The device used in this example is Eastron SDM120


For the details about basic installation and management of a service in the OpenHAB environment, look at CM3-home - OpenHAB getting started article.

Once the binding is installed, there are available the usual three files.


The default configuration of the SDM120 has not been modified. The details about the configuration and the registers exposed by the device are linked below. The system device used in this example is /dev/ttyUSB0, related to the most left RS485 socket on the board.


#the serial object has been attached to the RS485 port through the /dev/ttyUSB0 device
#the SDM120 default speed is 2400bps
#the handshake is in RTU mode

#this is an input device

#device ID = 1

#the starting register for the Voltage variable is 0 (device dependant)

#the variable is two 16bit world lenght (4 byte)

#it's a float32 variable

#for each other variable it needs a similar configuration set






On this example we read the parameters for voltage, current, active power, power factor (CosFi), frequency and energy for the phase 1 from registers 0, 6, 12, 30, 70 e 72 .


Now we can associate the items to these parameters, the key is the name of the objects created in modbus.cfg. The items related to the desired variables must be formatted for the printout similarly to the printf sintax.


Number L1V "Tensione [%.1f V]" {modbus="Voltage:0"}
Number L1A "Corrente [%.1f A]" {modbus="Current:0"}
Number L1P "Potenza A [%.1f W]" {modbus="Power:0"}
Number L1C "Cos Fi [%.2f]" {modbus="CosFi:0"}
Number L1F "Frequenza [%.1f Hz]" {modbus="Freq:0"}
Number L1E "Energia A [%.1f kWh]" {modbus="Ener:0"}


To show the values on the GUI, the variables must be associated to the sitemap. The association is possible through the name of the items.


sitemap knx label="GUIOTT home" 
        Frame label="Energia"
            Text label="Energia" icon=pressure
                Text item=L1V valuecolor=[>240="red",>230="orange",>220="green",<=220="orange"]
                Text item=L1A
                Text item=L1P valuecolor=[>3300="red",>3000="orange",>2500="green",<=2500="blue"]
                Text item=L1C valuecolor=[>0.8="green",<=0.8="orange"]
                Text item=L1F valuecolor=[<49="orange",>49="green",>51="orange"]
                Text item=L1E


Now the service starts polling the energy meter to collect data with the polling time configured on services.cfg file, updating the values on the page:

Classic UI

Basic UI

Or in the Habpanel

using knob widget as instruments, disabling interaction:

you can also enable the measure range of the instrument, so the bar change its color depending on some alarm thresholds:

The values collected can be used to obtain, for example, some audible or visible alarms as in the example below:


var Cmd="python /etc/openhab2/scripts/ "
var Timer set_timer = null

rule "Led"
        Time cron " 0/3 * * ? * * *"
        var Power = (L1P.state as DecimalType).intValue
         if (Power <= 2500)
             //val results = executeCommandLine(Cmd+"G",5000)
             sendCommand(LedG, OFF)
             sendCommand(LedB, ON)
             sendCommand(LedR, ON)
             set_timer = createTimer(now.plusSeconds(0.1))
                sendCommand(LedG, ON)
                set_timer = null
         else if (Power > 2500 && Power <= 3000)
            //val results = executeCommandLine(Cmd+"B",5000)
            sendCommand(LedB, OFF)
            sendCommand(LedG, ON)
            sendCommand(LedR, ON)
            set_timer = createTimer(now.plusSeconds(0.1))
               sendCommand(LedB, ON)
               set_timer = null
         else if (Power > 3000)
            //val results = executeCommandLine(Cmd+"R",5000)
            sendCommand(LedR, OFF)
            sendCommand(LedB, ON)
            sendCommand(LedG, ON)
            sendCommand(Rele1, ON)
            set_timer = createTimer(now.plusSeconds(0.1))
               sendCommand(LedR, ON)
               set_timer = null
            set_timer = createTimer(now.plusSeconds(1))
               sendCommand(Rele1, OFF)
               set_timer = null

In this case the CM3-Home RGB LED blinks with different colors when some electrical power thresholds have been exceeded. Furthermore one of the two relays is used to switch a buzzer, alerting with more evidence that the contractual power threshold has been exceeded. More details here. This is one of the most useful function I use often, hearing the beep I immediately switch off the appliance I'm using, avoiding to go dowstairs to reset the main breaker.

In the picture below the buzzer connected:

The 3.3V power supply come from the 1-wire connector on the left and is switched by the normally open contact of the relay.


Guido Ottaviani (Author/Designer/Developer for electronics-firmware-robotics)
He deals with electronic design and embedded system firmware for Robotics and industrial and civil applications, as well as technical disclosure on the internet. - -
Massimiliano Casini (Coordinatore tecnico Service Assurance presso
Sono cresciuto nel modo IT di aziende ad alto contenuto tecnologico e da sempre ho acquisto esperienze nel settore della domotica e sicurezza residenziale. Negli ultimi dieci anni ho acquisito competenze specifiche nel campo delle TLC in particolare sulla rete GSM e sui protocolli ad essa collegati.
Salvatore Imparato (Docente presso ITT Guglielmo Marconi)
Docente di elettrotecnica e sistemi automatici di istituto tecnico e tecnologico scuola superiore di secondo grado
linkedin -

Le attività del TanzoLab si svolgono ogni mercoledi sera, salvo casi speciali, dalle ore 18:30 presso i locali della Acme Systems srl e consistono in:

  • Talk monotematici a cura di professionisti in vari settori tecnologici
  • Workshop pratici su elettronica embedded, produzione e informatica
  • Progettazione e realizzazione di nuovi prodotti embedded per l'IT

Le attività vengono coordinate tramite questo sito, in cui vengono pubblicati tutti i lavori svolti o in via di sviluppo, e tramite un gruppo Telegram con cui per interagire direttamente via chat con gli altri membri.