# String format

Process

Formats a text string from an input pattern, substituting {} placeholders with input values. Output is deferred until the pattern and all required values have been received, preventing incomplete messages.

String format
P
1
2
3
4
T

# Inputs

IDAbbrevNameTypeDefaultDescription
patternPPatternSTRINGFormat pattern string. Use {} as a placeholder for each input value in order (e.g. "Temperature: {} C, Humidity: {} %"). Can be provided by a String constant block.
v11Value 1STRINGFirst value to substitute into the pattern.
v22Value 2STRINGSecond value to substitute into the pattern.
v33Value 3STRINGThird value to substitute into the pattern.
v44Value 4STRINGFourth value to substitute into the pattern.

# Outputs

IDAbbrevNameTypeDefaultDescription
textTTextSTRINGThe formatted text string with placeholders replaced by input values.

# Configuration

IDNameTypeDefaultUnitDescription
trigger_on_patternTrigger on patternBOOLEANfalseWhen enabled, changing the pattern input triggers output recalculation. When disabled, pattern changes are captured silently.
trigger_on_v1Trigger on V1BOOLEANtrueWhen enabled, changing Value 1 triggers output recalculation. When disabled, value changes are captured silently.
trigger_on_v2Trigger on V2BOOLEANtrueWhen enabled, changing Value 2 triggers output recalculation. When disabled, value changes are captured silently.
trigger_on_v3Trigger on V3BOOLEANtrueWhen enabled, changing Value 3 triggers output recalculation. When disabled, value changes are captured silently.
trigger_on_v4Trigger on V4BOOLEANtrueWhen enabled, changing Value 4 triggers output recalculation. When disabled, value changes are captured silently.

# State

IDNameTypeDefaultUnitDescription
has_patternPattern receivedBOOLEANfalseTracks whether the pattern input has been received at least once.
has_v1Value 1 receivedBOOLEANfalseTracks whether the v1 input has been received at least once.
has_v2Value 2 receivedBOOLEANfalseTracks whether the v2 input has been received at least once.
has_v3Value 3 receivedBOOLEANfalseTracks whether the v3 input has been received at least once.
has_v4Value 4 receivedBOOLEANfalseTracks whether the v4 input has been received at least once.

# Source Code

View Volang source
channel = input::channel()

if (channel == "pattern") {
    state::set("has_pattern", true)
}
if (channel == "v1") {
    state::set("has_v1", true)
}
if (channel == "v2") {
    state::set("has_v2", true)
}
if (channel == "v3") {
    state::set("has_v3", true)
}
if (channel == "v4") {
    state::set("has_v4", true)
}

if (!state::get("has_pattern")) {
    return
}

trigger = false
if (channel == "pattern" and config::get("trigger_on_pattern")) {
    trigger = true
}
if (channel == "v1" and config::get("trigger_on_v1")) {
    trigger = true
}
if (channel == "v2" and config::get("trigger_on_v2")) {
    trigger = true
}
if (channel == "v3" and config::get("trigger_on_v3")) {
    trigger = true
}
if (channel == "v4" and config::get("trigger_on_v4")) {
    trigger = true
}

if (!trigger) {
    return
}

pattern = input::get("pattern")
v1 = input::get("v1")
v2 = input::get("v2")
v3 = input::get("v3")
v4 = input::get("v4")

count = str::count(pattern, "{}")

if (count >= 1 and !state::get("has_v1")) {
    return
}
if (count >= 2 and !state::get("has_v2")) {
    return
}
if (count >= 3 and !state::get("has_v3")) {
    return
}
if (count >= 4 and !state::get("has_v4")) {
    return
}

if (count == 0) {
    output::set("text", pattern)
} else if (count == 1) {
    output::set("text", str::fmt(pattern, v1))
} else if (count == 2) {
    output::set("text", str::fmt(pattern, v1, v2))
} else if (count == 3) {
    output::set("text", str::fmt(pattern, v1, v2, v3))
} else {
    output::set("text", str::fmt(pattern, v1, v2, v3, v4))
}
Formats a text string from an input pattern, substituting {} placeholders with input values. Output is deferred until the pattern and all required values have been received, preventing incomplete messages.