Skip to content

Add info for multiplexing to sml.rst #5062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: current
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions components/sml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,63 @@ These meters can also measure the instantaneous power usage.
device_class: power
state_class: measurement

Reading multiple meters
---------------------------------
If you are reading data from more meters than your controller has UARTs available (e.g. more than 2 for an ESP32), you can use multiplexing to switch between reading data from different meters.

In order to do this, after each SML update, the used UART can be set to listen to a different pin.
An example on how to do this is this:

.. code-block:: yaml

uart:
- baud_rate: 9600
data_bits: 8
rx_pin:
number: 17 # Set to the first of the GPIO pins
id: uart_multiplex_rx_pin
stop_bits: 1
rx_buffer_size: 512
id: uart_multiplexed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

YAML indentation & trailing-space issues will break copy-paste usage

The example block contains:

  • Trailing spaces after uart: (line 236) – these are rendered in the docs and copy-paste ends up with an invalid key.
  • List indentation is off (list items should be indented two spaces under the key).
    Current indentation confuses YAML linters and ESPHome rejects the config.

Proposed fix:

-    uart:    
-     - baud_rate: 9600
-       data_bits: 8
-       rx_pin: 
-        number: 17 # Set to the first of the GPIO pins
-        id: uart_multiplex_rx_pin
-       stop_bits: 1
-       rx_buffer_size: 512
-       id: uart_multiplexed
+  uart:
+    - baud_rate: 9600
+      data_bits: 8
+      rx_pin:
+        number: 17  # first GPIO pin
+        id: uart_multiplex_rx_pin
+      stop_bits: 1
+      rx_buffer_size: 512
+      id: uart_multiplexed
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
uart:
- baud_rate: 9600
data_bits: 8
rx_pin:
number: 17 # Set to the first of the GPIO pins
id: uart_multiplex_rx_pin
stop_bits: 1
rx_buffer_size: 512
id: uart_multiplexed
uart:
- baud_rate: 9600
data_bits: 8
rx_pin:
number: 17 # first GPIO pin
id: uart_multiplex_rx_pin
stop_bits: 1
rx_buffer_size: 512
id: uart_multiplexed
🤖 Prompt for AI Agents
In components/sml.rst around lines 236 to 245, fix the YAML indentation by
removing trailing spaces after the key 'uart:' and ensure list items under
'uart:' are indented exactly two spaces. Adjust the nested keys accordingly to
conform to proper YAML structure so that linters and ESPHome accept the
configuration without errors.

sml:
- id: sml_multiplexed
uart_id: uart_multiplexed
on_data:
- lambda: |-
std::vector<gpio_num_t> multiplex_pins = {::GPIO_NUM_17,::GPIO_NUM_19};
static size_t current_index = 0;
current_index = (current_index + 1) % multiplex_pins.size();
gpio_num_t new_rx_pin = multiplex_pins[current_index];
ESP_LOGD("uart_multiplex", "Switching to RX pin: %d (%d)", new_rx_pin, current_index);
id(uart_multiplex_rx_pin).set_pin(new_rx_pin);
id(uart_multiplexed).load_settings(true);

sensor:
- platform: sml
name: "Solar Roof"
sml_id: sml_multiplexed
server_id: "12345ab" # IMPORTANT! Set the correct server id
obis_code: "1-0:2.8.0"
unit_of_measurement: kWh
accuracy_decimals: 2
device_class: energy
state_class: total_increasing
filters:
- multiply: 0.0001
- platform: sml
name: "Solar Carport"
sml_id: sml_multiplexed
server_id: "67890cd" # IMPORTANT! Set the correct server id
obis_code: "1-0:2.8.0"
unit_of_measurement: kWh
accuracy_decimals: 2
device_class: energy
state_class: total_increasing
filters:
- multiply: 0.0001


See Also
--------

Expand Down
Loading