You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-30Lines changed: 28 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,19 +48,16 @@ address_offset: 0
48
48
variant: sungrow
49
49
scan_batching: 100
50
50
```
51
-
52
-
`ip` (Required) The IP address of the modbus device to be polled. Presently only modbus TCP/IP is supported.
53
-
54
-
`port` (Optional: default 502) The port on the modbus device to connect to.
55
-
56
-
`update_rate` (Optional: default 5) The number of seconds between polls of the modbus device.
57
-
58
-
`address_offset` (Optional: default 0) This offset is applied to every register address to accommodate different Modbus addressing systems. In many Modbus devices the first register is enumerated as 1, other times 0. See section 4.4 of the Modbus spec.
59
-
60
-
`variant`(Optional) Allows variants of the ModbusTcpClient library to be used. Setting this to 'sungrow' enables support of SungrowModbusTcpClient. This library transparently decrypts the modbus comms with sungrow SH inverters running newer firmware versions.
61
-
62
-
`scan_batching` (Optional: default 100) Must be between 1 and 100 inclusive. Modbus read operations are more efficient in bigger batches of contiguous registers, but different devices have different limits on the size of the batched reads. This setting can also be helpful when building a modbus register map for an uncharted device. In some modbus devices a single invalid register in a read range will fail the entire read operation. By setting `scan_batching` to `1` each register will be scanned individually. This will be very inefficient and should not be used in production as it will saturate the link with many read operations.
63
-
51
+
| Field name | Required | Default | Description |
52
+
| ---------- | -------- | ------- | ----------- |
53
+
| ip | Required | N/A | The IP address of the modbus device to be polled. Presently only modbus TCP/IP is supported. |
54
+
| port | Optional | 502 | The port on the modbus device to connect to. |
55
+
| update_rate | Optional | 5 | The number of seconds between polls of the modbus device. |
56
+
| address_offset | Optional | 0 | This offset is applied to every register address to accommodate different Modbus addressing systems. In many Modbus devices the first register is enumerated as 1, other times 0. See section 4.4 of the Modbus spec. |
57
+
| variant | Optional | N/A | Allows variants of the ModbusTcpClient library to be used. Setting this to 'sungrow' enables support of SungrowModbusTcpClient. This library transparently decrypts the modbus comms with sungrow SH inverters running newer firmware versions. |
58
+
| scan_batching | Optional | 100 | Must be between 1 and 100 inclusive. Modbus read operations are more efficient in bigger batches of contiguous registers, but different devices have different limits on the size of the batched reads. This setting can also be helpful when building a modbus register map for an uncharted device. In some modbus devices a single invalid register in a read range will fail the entire read operation. By setting `scan_batching` to `1` each register will be scanned individually. This will be very inefficient and should not be used in production as it will saturate the link with many read operations. |
59
+
60
+
### Register settings
64
61
```yaml
65
62
registers:
66
63
- pub_topic: "forced_charge/mode"
@@ -83,24 +80,25 @@ registers:
83
80
- pub_topic: "first_bit_of_second_byte"
84
81
address: 13001
85
82
mask: 0x0010
83
+
- pub_topic: "load_control/optimized/end_time"
84
+
address: 13013
85
+
json_key: hours
86
+
- pub_topic: "load_control/optimized/end_time"
87
+
address: 13014
88
+
json_key: minutes
86
89
```
87
90
88
91
This section of the YAML lists all the modbus registers that you consider interesting.
89
92
90
-
`address`(Required) The decimal address of the register to read from the device, starting at 0. Many modbus devices enumerate registers beginning at 1, so beware.
91
-
92
-
`pub_topic`(Optional) This is the topic to which the value of this register will be published.
93
-
94
-
`set_topic`(Optional) Values published to this topic will be written to the Modbus device.
95
-
96
-
`retain` (Optional: default false) Controls whether the value of this register will be published with the retain bit set.
97
-
98
-
`pub_only_on_change` (Optional: default true) Controls whether this register will only be published if its value changed from the previous poll.
99
-
100
-
`table` (Optional: default 'holding') The Modbus table to read from the device. Must be 'holding' or 'input'.
101
-
102
-
`value_map`(Optional) A series of human-readable and raw values for the setting. This will be used to translate between human-readable values via MQTT to raw values via Modbus. If a value_map is set for a register the interface will reject raw values sent via MQTT. If value_map is not set the interface will try to set the Modbus register to that value. Note that the scale is applied after the value is read from Modbus and before it is written to Modbus.
103
-
104
-
`scale` (Optional: default 1) After reading a value from the Modbus register it will be multiplied by this scalar before being published to MQTT. Values published on this register's `set_topic` will be divided by this scalar before being written to Modbus.
105
-
106
-
`mask` (Optional: default 0xFFFF) This is a 16-bit number that can be used to select a part of a Modbus register to be referenced by this register. For example a mask of `0xFF00` will map to the most significant byte of the 16-bit Modbus register at `address`. A mask of `0x0001` will reference only the least significant bit of this register.
93
+
| Field name | Required | Default | Description |
94
+
| ---------- | -------- | ------- | ----------- |
95
+
| address | Required | N/A | The decimal address of the register to read from the device, starting at 0. Many modbus devices enumerate registers beginning at 1, so beware. |
96
+
| pub_topic | Optional | N/A | This is the topic to which the value of this register will be published. |
97
+
| set_topic | Optional | N/A | Values published to this topic will be written to the Modbus device. Cannot yet be combined with json_key. See https://github.com/tjhowse/modbus4mqtt/issues/23 for details. |
98
+
| retain | Optional | false | Controls whether the value of this register will be published with the retain bit set. |
99
+
| pub_only_on_change | Optional | true | Controls whether this register will only be published if its value changed from the previous poll. |
100
+
| table | Optional | holding | The Modbus table to read from the device. Must be 'holding' or 'input'. |
101
+
| value_map | Optional | N/A | A series of human-readable and raw values for the setting. This will be used to translate between human-readable values via MQTT to raw values via Modbus. If a value_map is set for a register the interface will reject raw values sent via MQTT. If value_map is not set the interface will try to set the Modbus register to that value. Note that the scale is applied after the value is read from Modbus and before it is written to Modbus. |
102
+
| scale | Optional | 1 | After reading a value from the Modbus register it will be multiplied by this scalar before being published to MQTT. Values published on this register's `set_topic` will be divided by this scalar before being written to Modbus. |
103
+
| mask | Optional | 0xFFFF | This is a 16-bit number that can be used to select a part of a Modbus register to be referenced by this register. For example a mask of `0xFF00` will map to the most significant byte of the 16-bit Modbus register at `address`. A mask of `0x0001` will reference only the least significant bit of this register. |
104
+
| json_key | Optional | N/A | The value of this register will be published to its pub_topic in JSON format. E.G. `{ key: value }` Registers with a json_key specified can share a pub_topic. All registers with shared pub_topics must have a json_key specified. In this way, multiple registers can be published to the same topic in a single JSON message. If any of the registers that share a pub_topic have the retain field set that will affect the published JSON message. Conflicting retain settings are invalid. The keys will be alphabetically sorted. |
# This throws ValueError exceptions if the imported registers are invalid
171
+
@staticmethod
172
+
def_validate_registers(registers):
173
+
all_pub_topics=set()
174
+
duplicate_pub_topics=set()
175
+
# Key: shared pub_topics, value: list of json_keys
176
+
duplicate_json_keys= {}
177
+
# Key: shared pub_topics, value: set of retain values (true/false)
178
+
retain_setting= {}
179
+
180
+
# Look for duplicate pub_topics
181
+
forregisterinregisters:
182
+
ifregister['pub_topic'] inall_pub_topics:
183
+
duplicate_pub_topics.add(register['pub_topic'])
184
+
duplicate_json_keys[register['pub_topic']] = []
185
+
retain_setting[register['pub_topic']] =set()
186
+
if'json_key'inregisterand'set_topic'inregister:
187
+
raiseValueError("Bad YAML configuration. Register with set_topic '{}' has a json_key specified. This is invalid. See https://github.com/tjhowse/modbus4mqtt/issues/23 for details.".format(register['set_topic']))
188
+
all_pub_topics.add(register['pub_topic'])
189
+
190
+
# Check that all registers with duplicate pub topics have json_keys
191
+
forregisterinregisters:
192
+
ifregister['pub_topic'] induplicate_pub_topics:
193
+
if'json_key'notinregister:
194
+
raiseValueError("Bad YAML configuration. pub_topic '{}' duplicated across registers without json_key field. Registers that share a pub_topic must also have a unique json_key.".format(register['pub_topic']))
raiseValueError("Bad YAML configuration. pub_topic '{}' duplicated across registers with a duplicated json_key field. Registers that share a pub_topic must also have a unique json_key.".format(register['pub_topic']))
0 commit comments