Configuration

Configuration of sml2mqtt is done through a yaml file. The path to the file can be specified with -c PATH or --config PATH. If nothing is specified a file with the name config.yml is searched in the subdirectory sml2mqtt in

  • the current working directory

  • the venv directory

  • the user home

If a config file is specified and it does not yet exist a default configuration file will be created.

Example

logging:
  level: INFO         # Log level
  file: sml2mqtt.log  # Log file path (absolute or relative to config file) or stdout

mqtt:
  connection:
    identifier: sml2mqtt-ZqlFvhSBdDGvJ
    host: localhost
    port: 1883
    user: ''
    password: ''
  topic prefix: sml2mqtt
  defaults:
    qos: 0         # Default value for QOS if no other QOS value in the config entry is set
    retain: false  # Default value for retain if no other retain value in the config entry is set
  last will:
    topic: status   # Topic fragment for building this topic with the parent topic

general:
  Wh in kWh: true       # Automatically convert Wh to kWh
  republish after: 120  # Republish automatically after this time (if no other filter configured)

inputs:
- type: serial
  url: COM1   # Device path
  timeout: 3  # Seconds after which a timeout will be detected (default=3)
- type: serial
  url: /dev/ttyS0   # Device path
  timeout: 3        # Seconds after which a timeout will be detected (default=3)

devices:
  # Device configuration by reported id
  device_id_hex:

    mqtt:                           # Optional MQTT configuration for this meter.
      topic: DEVICE_BASE_TOPIC      # Topic fragment for building this topic with the parent topic

    status:                         # Optional MQTT status topic configuration for this meter
      topic: status                 # Topic fragment for building this topic with the parent topic

    skip:                           # OBIS codes (HEX) of values that will not be published (optional)
    - '00112233445566'

    # Configurations for each of the values (optional)
    values:

    - obis: '00112233445566'        # Obis code for this value
      mqtt:                         # Mqtt config for this value (optional)
        topic: OBIS_VALUE_TOPIC     # Topic fragment for building this topic with the topic prefix
      # A sequence of operations that will be evaluated one after another.
      # If one operation blocks nothing will be reported for this frame
      operations:
      - negative on energy meter status: true   # Make value negative based on an energy meter status. Set to "true" to enable or to "false" to disable workaround. If the default obis code for the energy meter is wrong set to the appropriate meter obis code instead
      - factor: 3               # Factor with which the value gets multiplied
      - offset: 100             # Offset that gets added on the value
      - round: 2                # Round to the specified digits
      - refresh action: 300     # Republish value every 300s

Example input Tibber bridge

These input settings can be used to poll data from a Tibber bridge:

inputs:
 - type: http
   url: http://IP_OR_HOSTNAME_OF_TIBBER_BRIDGE/data.json?node_id=1
   interval: 3   # Poll interval secs
   timeout: 10   # After which time the input will change to TIMEOUT
   user: "admin"
   password: "printed on bridge socket"

Example mqtt config

MQTT topics can be configured either by providing a full topic or a topic fragment. With a topic fragment the resulting topic is build with the parent topic. The structure is topic prefix / device / value. Providing a full topic will ignore the fragments. The entries for qos and retain are optional.

full topic: my/full/topic
qos: 1

Configuration Reference

All possible configuration options are described here. Not all entries are created by default in the config file and one should take extra care when changing those entries.

settings Settings
field logging: LoggingSettings [Optional]
field mqtt: MqttConfig [Optional]
field general: GeneralSettings [Optional]
field inputs: list[HttpSourceSettings | SerialSourceSettings] = []
field devices: dict[Annotated[str], SmlDeviceConfig] = {}

Device configuration by ID or url

logging

settings LoggingSettings
field level: str = 'INFO'

Log level

field file: str = 'sml2mqtt.log'

Log file path (absolute or relative to config file) or “stdout”

general

settings GeneralSettings
field Wh in kWh: bool = True

Automatically convert Wh to kWh

field republish after: int = 120

Republish automatically after this time (if no other filter configured)

field report blank energy meters: bool = False

Report blank energy meters (where the value is 0kwh)

field report device id: bool = False

Report the device id even though it does never change

field device id obis: list[Annotated[str]] = ['0100000009ff', '0100600100ff']

Additional OBIS fields for the serial number to configuration matching

inputs

settings SerialSourceSettings
field type: Literal['serial'] [Required]
field url: Annotated[str] [Required]

Device path

Constraints:
  • strip_whitespace = True

  • strict = True

  • min_length = 1

field timeout: Union[Annotated[int], Annotated[float]] = 6

Seconds after which a timeout will be detected (default=6)

field baudrate: int = 9600
field parity: str = 'None'
field stop bits: Union[Annotated[int], Annotated[float]] = 1
field byte size: int = 8

Example:

type: serial
url: COM3
settings HttpSourceSettings
field type: Literal['http'] [Required]
field url: Annotated[Url] [Required]

Url

Constraints:
  • allowed_schemes = [‘http’, ‘https’]

field timeout: Union[Annotated[int], Annotated[float]] = 6

Seconds after which a timeout will be detected (default=6)

field interval: Union[Annotated[int], Annotated[float]] = 2

Delay between requests

Constraints:
  • ge = 0.1

field user: str = ''

User (if needed)

field password: str = ''

Password (if needed)

field request timeout: Union[Annotated[int], Annotated[float], None] = None

Dedicated timeout for the http request

Example:

type: http
url: http://localhost:8080/sml
interval: 3
timeout: 10

mqtt

settings MqttConfig
field connection: MqttConnection [Optional]
field topic prefix: Annotated[str] = 'sml2mqtt'

Prefix for all topics. Set to empty string to disable

Constraints:
  • strict = True

  • strip_whitespace = True

field defaults: MqttDefaultPublishConfig [Optional]
field last will: OptionalMqttPublishConfig [Optional]
settings MqttConnection
field identifier: Annotated[str] = 'sml2mqtt-tNeMNZrhuwjSQ'
Constraints:
  • strict = True

  • strip_whitespace = True

field host: Annotated[str] = 'localhost'
Constraints:
  • strict = True

  • strip_whitespace = True

field port: int = 1883
Constraints:
  • ge = 0

field user: Annotated[str] = ''
Constraints:
  • strict = True

  • strip_whitespace = True

field password: Annotated[str] = ''
Constraints:
  • strict = True

  • strip_whitespace = True

field tls: MqttTlsOptions | None = None
settings OptionalMqttPublishConfig
field topic: Optional[Annotated[str]] = None

Topic fragment for building this topic with the parent topic

field full topic: Optional[Annotated[str]] = None

Full topic - will ignore the parent topic parts

field qos: Optional[Literal[0, 1, 2]] = None

QoS for publishing this value (if set - otherwise use parent)

field retain: Optional[Annotated[bool]] = None

Retain for publishing this value (if set - otherwise use parent)

settings MqttDefaultPublishConfig
field qos: Literal[0, 1, 2] = 0

Default value for QoS if no other QoS value in the config entry is set

field retain: Annotated[bool] = False

Default value for retain if no other retain value in the config entry is set

Constraints:
  • strict = True

settings MqttTlsOptions
field insecure: bool | None = None

Enable/disable server hostname verification when using SSL/TLS.

field ca certificates: str | None = None

Path to Certificate Authority (CA) certificate file in PEM or DER format

field cert file: str | None = None

Path to PEM encoded client certificate file

field key file: str | None = None

Path to PEM encoded private keys file

field file password: str | None = None

Password to encrypt the cert file or the key file if needed

field certificate requirement: Optional[Literal['NONE', 'OPTIONAL', 'REQUIRED']] = None

Certificate requirement that the client imposes on the broker.

field tls version: Optional[Literal['SSLv23', 'TLS', 'TLS_CLIENT', 'TLS_SERVER', 'TLSv1', 'TLSv1_1', 'TLSv1_2']] = None

The version of the SSL/TLS protocol to be used.

field ciphers: str | None = None

Which encryption ciphers are allowable for the connection

devices

settings SmlDeviceConfig

Configuration for a sml device

field mqtt: OptionalMqttPublishConfig | None = None

Optional MQTT configuration for this meter.

field status: OptionalMqttPublishConfig = OptionalMqttPublishConfig(topic='status', full_topic=None, qos=None, retain=None)

Optional MQTT status topic configuration for this meter

field skip: set[Annotated[str]] [Optional]

OBIS codes (HEX) of values that will not be published (optional)

field values: list[SmlValueConfig] = []

Configurations for each of the values (optional)

settings SmlValueConfig
field obis: Annotated[str] [Required]

Obis code for this value

Constraints:
  • strip_whitespace = True

  • to_lower = True

  • strict = True

  • pattern = [0-9a-fA-F]{12}

field mqtt: OptionalMqttPublishConfig | None = None

Mqtt config for this value (optional)

field operations: Annotated[list[Annotated[Union[Annotated[OnChangeFilter], Annotated[DeltaFilter], Annotated[HeartbeatAction], Annotated[RangeFilter], Annotated[RefreshAction], Annotated[ThrottleFilter], Annotated[Factor], Annotated[Offset], Annotated[Round], Annotated[NegativeOnEnergyMeterWorkaround], Annotated[Or], Annotated[Sequence], Annotated[VirtualMeter], Annotated[MaxValue], Annotated[MinValue], Annotated[MaxOfInterval], Annotated[MinOfInterval], Annotated[MeanOfInterval]]]]] = []

A sequence of operations that will be evaluated one after another. If one operation blocks this will return nothing.

Constraints:
  • min_length = 1