MODBUS and Home Assistant with just a Bash script, mbpoll and MQTT Discovery(Swegon CASA)
So no Node-RED and no extra software to babysit. One small Bash script, run from cron, that reads a Swegon CASA FTX over MODBUS RTU and shows up in Home Assistant as a ready-made device.
That works well, but it means that Node-RED has to be installed, updated
and kept running on both ends. When the data is going to Home Assistant (HA)
anyway, there is a simpler way: read the registers with a command line tool
"mbpoll" and publish them with "mosquitto_pub" using MQTT Discovery. HA then creates the sensors by itself. The whole
solution is one Bash script and a cron job. The same pattern as in my Lightweight DIY Linux Server Monitoring post, just with MODBUS as the data source.
- A RPi, or any Linux box, connected to the CASA via the SEM interface and a USB to RS 485 converter, exactly as in the first post
- An MQTT broker that HA is connected to, in my case Mosquitto within HA
-
HA with the MQTT integration and discovery enabled (default prefix
is
homeassistant)
Install the two tools:
sudo apt install mbpoll mosquitto-clients
"mbpoll" is a free command line MODBUS master. It is the open source cousin of the
well known "modpoll", and it is found in the standard repositories, so no manual download is
needed.
/dev/ttyUSB0", which can change to "ttyUSB1" after a reboot or when you plug in another USB device, use the persistent
name of the converter:
ls -l /dev/serial/by-id/
It looks something like "usb-FTDI_FT232R_USB_UART_<serial number>-if00-port0" and it is tied to the converter itself. Please note that only one program
at a time can use the serial port, so if you have a Node-RED MODBUS client
running on the same device, stop it first.
-
-m rtu -b 38400 -d 8 -s 1 -P noneis MODBUS RTU, 38400 baud, 8 data bits, 1 stop bit, no parity -
-a 1is the unit id -
-t 3is input registers. Nice detail:mbpolluses the same "3x / 4x" naming as Swegon does, so-t 3is the Swegon 3x registers (FC 4 in Node-RED) and-t 4is the 4x registers -
-0means that the first register is address 0, the same addressing as I use in the Node-RED flow. If your values look shifted by one register, that is the first thing to check -
-r 6200 -c 12is start register and number of registers, and-1means poll once and exit
The output is one line per register, something like [6202]: 198.
The temperatures are 16 bit signed values in tenths of a degree, so a value above 32767 is below zero. That is handled with one line of awk.
The heat recovery efficiency is not a register, it is calculated from four of the temperatures, with the same formula as in my Node-RED flow, and published in real percent. It is set to 0 when the unit or the rotor is stopped, or when the result is unreasonable (negative, above 120 % or a division by zero).
read_regs() { # start, count -> one value per line
mbpoll -m rtu -a "$UNIT" -b "$BAUD" -d 8 -s 1 -P none -t 3 -0 \
-r "$1" -c "$2" -1 -o 2 "$PORT" 2>/dev/null \
| awk '/^\[[0-9]+\]:/ {print $NF}'
}
mapfile -t TEMP < <(read_regs 6200 12)
If a read fails, the script publishes "offline" on the status topic and exits. Otherwise all values are published in one
go.
swegonFtx/state:"
{"temp_outdoor":4.2,"temp_supply":19.8,"temp_extract":21.5,"temp_exhaust":6.1,"efficiency":84.3,"fan_supply":45,"fan_exhaust":45,"rotor":100,"heater":0,"humidity":38,"mode":"Home"}
That is one connection to the broker instead of a dozen, and all values
belong to the same measurement. The price is that every sensor needs a
value_template in the discovery message, but
that is just one extra line per sensor.
homeassistant/sensor/<node>/<sensor>/config" so there is nothing to configure in HA.
The whole trick is one small helper function:
disc() { # key, name, extra json (starts with a comma)
mosquitto_pub "${MQTT_ARGS[@]}" -r -q 1 \
-t "homeassistant/sensor/$NODE_ID/$1/config" \
-m "{\"name\":\"$2\",\"unique_id\":\"${NODE_ID}_$1\",\"state_topic\":\"$BASE/state\",\"value_template\":\"{{ value_json.$1 }}\",\"availability_topic\":\"$BASE/status\",\"expire_after\":600$3,\"device\":$DEVICE}"
}
disc temp_outdoor "Outdoor temperature" ',"device_class":"temperature","unit_of_measurement":"°C","state_class":"measurement"'
To use it for something else, change the names, the reading part and the
list of disc lines, and you are done. All
sensors share one device block, so they end up under one
device, "Swegon FTX", in HA. Details about the discovery format are found in
the HA MQTT documentation.
- the first time it runs
- when the script file has changed, for example when you add a sensor
- every 24 hours
It can also be forced with "./swegonFtx_HA.sh --discovery"
online" on "swegonFtx/status" after every successful run, and "offline" if the MODBUS read fails. All sensors also have expire_after: 600, so if cron stops, or the whole RPi is down, the entities become
unavailable in HA after ten minutes instead of showing old values.
flock, so two runs never use the serial port at the same time.
*/2 * * * * /home/pi/swegonFtx_HA.sh
dialout" group to be allowed to use the serial port.
- Outdoor, supply, extract and exhaust air temperature
- Heat recovery efficiency
- Supply fan, exhaust fan, rotor and post heater in %
- Humidity
- Mode (Stopped, Traveling, Away, Home, Home +, Boost, Fireplace)
From there it is standard HA: history graphs, dashboards and automations.
Remarks
- The script opens and closes the serial port for each read, which is no problem with a two minute interval. With a much faster poll rate a permanently running solution, like Node-RED, would be a better choice.
- With Node-RED you get a live dashboard with just a few nodes. Here you use the HA dashboards and history instead.
-
The "
mbpoll"options can be a bit different between versions, so run the test command above first and check that the output looks as expected before you start the script. - Full script: Gist link !
- "mbpoll" could also be used with MODBUS TCP so it could be something like this
mbpoll,
mosquitto_pub"
and some Bash, the CASA is reached from HA with no Node-RED at all, and
with MQTT Discovery the sensors create themselves. Less software to install,
update and keep running, and the discovery helper can be reused for whatever
else you want to push into HA from a Bash script.

Comments
Post a Comment
Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !