timroemisch / mqtt-s7-connector

Connect Siemens S7 to Mqtt with Homeassistant discovery support
ISC License
19 stars 9 forks source link
homeassistant mqtt mqtt-bridge nodejs siemens siemens-s7-plcs

mqtt-s7-connector

This is a Node.js tool to connect a Siemens S7 PLC over Mqtt with Home Assistant

This project is intended to use along with Home Assistant, but is also possible to use it as a simple bridge between s7 and mqtt.

Purpose

This tool can receive data over mqtt and can write it to a designated address on a plc and vice versa, enabling smart home data to be displayed in the home assistant.

How to install

docker run -d -v /path/on/host/config.json:/usr/src/app/config.json timroemisch/mqtt-s7-connector

Note: You only have to mount the configuration file, not the entire folder.
Config volume mountpoint: /usr/src/app/config.json

Config File

The configuration file has to be located in the same directory as the installation and has to be named config.json

An example of a correct configuration file is found in config.example.json.

The config file has to be valid JSON (You can check here if it´s correct)
and is separated in 3 sections:

Address formatting

This tool uses the NodeS7 Library and it uses the same address formatting.
An example of correct formatted addresses is found at the NodeS7 Repository

Address examples:
DB56,X150.0 (read from DB56 one bit at 150.0)
DB51,REAL216 (read from DB51 four bytes starting from byte 216)
DB56,BYTE40 (read from DB56 one byte at 40)

Supported data types
X = 1 Bit -> converted to true / false
BYTE = 1 Byte (8 Bit) -> converted to Int
REAL = 4 Bytes (32 Bit) -> converted to Float

For more information see the NodeS7 Repository

Device types and attributes

The device type categories are based on the categories from Home Assistant
It is strongly recommended to look into the example configuration file !!

Current list of supported device types with supported attributes:

Attribute Options

A "simple" device has just the plc address as the value of the attributes,
however it's possible to configure each attribute individually by assigning an object instead of a string to it.

Simple Attribute:

...

"state": "DB56,X150.0",

...

Rewritten Attribute:

...

"state": {
  "plc": "DB56,X150.0"
},

...

Now after rewriting it's possible to add more options inside the brackets of the attribute.

Available options:

Read PLC Write PLC Subscribe MQTT Publish MQTT
r
w
rw
"state": {
    "plc": "DB56,X150.0",
    "rw": "r"
},

Auto Discovery

This tool will send for each device an auto-discovery message over mqtt in the correct format defined by Home Assistant.

The default mqtt topic is homeassistant, it can be changed in the config file. (See the example)

ToDo

Pull requests welcome! 😄

Credits

License

Licensed under ISC
Copyright (c) 2021 Tim Römisch