-
Notifications
You must be signed in to change notification settings - Fork 0
Value Mapping Guide
"I read a Modbus int value (example '41'), but I want to output text instead. If '41' then 'On', if '43' then 'Off'. Is this possible?"
The options field allows you to map raw values to display text.
In Protocol Wizard options flow:
- Go to your device settings
- Edit the entity (the one reading value 41/43)
- Find the
optionsfield
In the options field, enter a JSON object mapping values to text:
{"41": "On", "43": "Off"}Important:
- Use strings as keys (even for numbers):
"41"not41 - Use valid JSON syntax (double quotes, no trailing comma)
Save the entity. The display will now show:
- Value 41 → "On"
- Value 43 → "Off"
{
"0": "Idle",
"1": "Running",
"2": "Error",
"3": "Maintenance"
}{
"0": "No",
"1": "Yes"
}{
"0": "Cold",
"1": "Normal",
"2": "Hot",
"3": "Critical"
}{
"1": "Mode A - Economy",
"2": "Mode B - Normal",
"3": "Mode C - Performance"
}When the coordinator reads a value:
-
Read raw value from device:
41 -
Check if
optionsexists in entity config -
Convert value to string:
"41" -
Look up in options dict:
{"41": "On", ...} -
Return mapped value:
"On" - Display in Home Assistant: Entity shows "On" instead of 41
format: "{:.1f}°C"
- Formatting numbers (decimals, units)
- Mathematical transformations
- Date/time formatting
options: {"41": "On", "43": "Off"}
- Mapping discrete values to text
- Status codes → readable text
- Enum values → descriptions
Yes! They're applied in sequence:
- Scale/offset applied
- Format string applied
- Options mapping applied last
Example:
scale: 10
format: "{:.0f}"
options: {"410": "On", "430": "Off"}Raw value 41 → scaled to 410 → formatted to "410" → mapped to "On"
Check 1: Valid JSON?
❌ Wrong: {41: "On"} # Missing quotes on key
❌ Wrong: {"41": "On",} # Trailing comma
✅ Correct: {"41": "On"}Check 2: Key is a string?
❌ Wrong: {41: "On"} # Number key
✅ Correct: {"41": "On"} # String keyCheck 3: Exact match?
- Value is
41but options has"41.0"→ Won't match! - Use exact string representation
Check 4: Coordinator implements it?
- Check if
_decode_value()has options mapping logic - See "Coordinator Implementation" section above
This means:
- Mapped value exists → Shows text ("On")
- Unmapped value received → Shows number (42)
Solution: Add all possible values to options:
{
"41": "On",
"42": "Unknown State",
"43": "Off"
}No, options only supports exact matches. For ranges, use format string or custom logic.
In the Protocol Wizard UI, the options field appears as:
┌─────────────────────────────────────┐
│ Options (JSON mapping) │
│ ┌─────────────────────────────────┐ │
│ │ {"41": "On", "43": "Off"} │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────┘
Tips:
- Validate JSON syntax online jsonlint.com before entering
- Start simple with 2-3 mappings, add more later
- Document what each code means in entity name or description
Address: 40100 Raw Values:
- 0 = Stopped
- 1 = Starting
- 2 = Running
- 3 = Stopping
- 4 = Error
- 5 = Maintenance
Name: Pump Status
Address: 40100
Data Type: uint16
Options: {
"0": "Stopped",
"1": "Starting",
"2": "Running",
"3": "Stopping",
"4": "Error",
"5": "Maintenance"
}Entity displays "Running" instead of 2 ✅
✅ Use options field for value-to-text mapping
✅ JSON format: {"value": "text", "value2": "text2"}
✅ String keys: Always use quotes around keys
✅ Works for all protocols: Modbus, SNMP, MQTT
✅ Applied last: After scale, offset, and format