Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions config/devices.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ decimalshiftright=0
input=false
datatype="float"
measurementmapping.templatestring="{\"Test\":{\"Float32\":%% }}"
#timestamp and time are also supported in the template
#measurementmapping.templatestring="{\"Test\":{\"Float32\":%% }, \"timestamp\":\"\"}"


[[device.coils]]
Expand Down
18 changes: 16 additions & 2 deletions tedge_modbus/reader/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def __init__(self, device):
self.device = device
self.data = {"hr": {}, "ir": {}, "co": {}, "di": {}}

def _process_template(self, template_string, value):
"""Process template string with value and timestamp replacements"""
# Replace the main value placeholder
data = template_string.replace("%%", str(value))

# Generate current timestamp in ISO format
current_timestamp = datetime.now(timezone.utc).isoformat()

# Replace timestamp placeholders
data = data.replace('"timestamp":""', f'"timestamp":"{current_timestamp}"')
data = data.replace('"time":""', f'"time":"{current_timestamp}"')

return data

def validate(self, register_def):
"""Validate definition"""
start_bit = register_def["startbit"]
Expand Down Expand Up @@ -141,8 +155,8 @@ def map_register(
has_changed = last_value != scaled_value

if not on_change or last_value is None or has_changed:
data = register_def["measurementmapping"]["templatestring"].replace(
"%%", str(scaled_value)
data = self._process_template(
register_def["measurementmapping"]["templatestring"], scaled_value
)
if register_def["measurementmapping"].get(
"combinemeasurements", device_combine_measurements
Expand Down
Loading