Skip to content

Commit 4e01d2e

Browse files
authored
Merge pull request #32 from adafruit/tylerdcooper-patch-1
docs: text template update
2 parents 774c373 + 9a0b944 commit 4e01d2e

File tree

3 files changed

+177
-42
lines changed

3 files changed

+177
-42
lines changed

app/blocks/text/template.js

Lines changed: 172 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,211 @@ export default {
66
colour: 180,
77
inputsInline: true,
88
description: `
9-
Render a text template.
10-
119
::: v-pre
12-
Special forms surrounded by {{ curly_braces }}
13-
will be replaced with their current value during the action run.
14-
15-
These templates use the Liquid templating language, from Shopify, and many
16-
helpful functions come built-in. See the
17-
[Liquid Documentation](https://shopify.github.io/liquid/basics/introduction/)
18-
to learn more.
19-
20-
### Template Variables:
21-
22-
\`{{ variables.var_name }}\` - get the value of a variable you have defined
23-
with name 'var_name'
24-
25-
\`{{ vars.var_name }}\` - shorthand for same as above
26-
27-
\`{{ variables['var name'] }}\` - get the value of a variable you have
28-
defined with name 'var name' (allows spaces in variable names
29-
30-
\`{{ vars['var name'] }}\` - shorthand for same as above
31-
32-
\`{{ user.name }}\` - your user's name
33-
34-
\`{{ user.username }}\` - your user's username
35-
36-
\`{{ feeds['group_key.feed_key'].name }}\` - access a feed with key
37-
'group_key.feed_key' and get its name
38-
39-
\`{{ feeds[...].key }}\` - ...get its key
40-
41-
\`{{ feeds[...].value }}\` - ...get its last value
10+
Create dynamic, personalized messages by combining static text with live data from your IoT system. Perfect for intelligent notifications like "Hello {{ user.name }}, your temperature sensor read {{ feeds['sensors.temperature'].value }}°F" or automated reports that include current sensor values, user information, and real-time data. Uses the powerful Liquid templating language for advanced formatting and logic.
11+
12+
## What is a Text Template?
13+
14+
Think of a text template like a form letter where you leave blanks to fill in with specific information. Instead of writing "Dear _____", you write "Dear {{ user.name }}" and the system automatically fills in the actual name when the message is sent.
15+
16+
## How It Works
17+
18+
This block renders (processes) a text template, replacing special placeholders with actual values.
19+
Anything surrounded by {{ double curly braces }} gets replaced with real data when your action runs.
20+
21+
For example:
22+
- Template: "Hello {{ user.name }}!"
23+
- Becomes: "Hello John!"
24+
25+
These templates use the Liquid templating language from Shopify, which includes many helpful built-in functions.
26+
Learn more: [Liquid Documentation](https://shopify.github.io/liquid/basics/introduction/)
27+
28+
## Template Variables Reference
29+
30+
### User Information
31+
- \`{{ user.name }}\` - Your full name (e.g., "John Smith")
32+
- \`{{ user.username }}\` - Your username (e.g., "jsmith123")
33+
34+
### Custom Variables
35+
- \`{{ variables.var_name }}\` - Get value of variable named 'var_name'
36+
- \`{{ vars.var_name }}\` - Shorthand version (same as above)
37+
- \`{{ variables['var name'] }}\` - Use brackets for names with spaces
38+
- \`{{ vars['my special var'] }}\` - Shorthand with spaces
39+
40+
### Feed Data (Sensors & Devices)
41+
- \`{{ feeds['group_key.feed_key'].value }}\` - Current value from a feed
42+
- \`{{ feeds['group_key.feed_key'].name }}\` - Feed's display name
43+
- \`{{ feeds['group_key.feed_key'].key }}\` - Feed's unique key
44+
- \`{{ feeds['group_key.feed_key'].updated_at }}\` - Last update timestamp
45+
46+
## Real-World IoT Examples
47+
48+
### 🌡️ Temperature Alerts
49+
**Basic Alert:**
50+
\`"Temperature Alert: {{ feeds['sensors.temp'].value }}°F detected!"\`
51+
Output: "Temperature Alert: 85°F detected!"
52+
53+
**Detailed Alert with Location:**
54+
\`"⚠️ HIGH TEMP WARNING
55+
Location: {{ feeds['sensors.temp'].name }}
56+
Current: {{ feeds['sensors.temp'].value }}°F
57+
Time: {{ feeds['sensors.temp'].updated_at }}
58+
Please check your {{ vars.device_location }} immediately!"\`
59+
60+
### 📊 Daily Status Reports
61+
**Simple Report:**
62+
\`"Daily Report for {{ user.name }}:
63+
• Temperature: {{ feeds['home.temp'].value }}°F
64+
• Humidity: {{ feeds['home.humidity'].value }}%
65+
• Battery: {{ vars.battery_level }}%"\`
66+
67+
**Comprehensive Home Report:**
68+
\`"Good morning {{ user.name }}! Here's your home status:
69+
70+
🌡️ Climate Control:
71+
- Living Room: {{ feeds['climate.living_temp'].value }}°F
72+
- Bedroom: {{ feeds['climate.bedroom_temp'].value }}°F
73+
- Humidity: {{ feeds['climate.humidity'].value }}%
74+
75+
🔋 Device Status:
76+
- Door Sensor Battery: {{ feeds['security.door_battery'].value }}%
77+
- Motion Detector: {{ vars.motion_status }}
78+
- Last Activity: {{ feeds['security.last_motion'].updated_at }}
79+
80+
Have a great day!"\`
81+
82+
### 🚪 Security Notifications
83+
**Door Alert:**
84+
\`"🚪 {{ feeds['security.front_door'].name }} is {{ feeds['security.front_door'].value }}
85+
Time: {{ feeds['security.front_door'].updated_at }}
86+
User: {{ user.name }}"\`
87+
88+
### 🌱 Garden Monitoring
89+
**Watering Reminder:**
90+
\`"Hey {{ user.name }}, your {{ vars.plant_name }} needs attention!
91+
Soil Moisture: {{ feeds['garden.moisture'].value }}%
92+
Last Watered: {{ vars.last_water_date }}
93+
Recommendation: Water {{ vars.water_amount }}ml"\`
94+
95+
### 🔔 Smart Doorbell Messages
96+
**Visitor Notification:**
97+
\`"{{ user.name }}, someone is at your door!
98+
Camera: {{ feeds['doorbell.camera'].name }}
99+
Motion Level: {{ feeds['doorbell.motion'].value }}
100+
Time: {{ feeds['doorbell.motion'].updated_at }}"\`
101+
102+
## Advanced Liquid Features
103+
104+
### Conditional Logic
105+
Use if/else statements for smart messages:
106+
\`"{% if feeds['sensors.temp'].value > 80 %}
107+
🔥 It's hot! {{ feeds['sensors.temp'].value }}°F
108+
{% else %}
109+
❄️ Nice and cool: {{ feeds['sensors.temp'].value }}°F
110+
{% endif %}"\`
111+
112+
### Formatting Numbers
113+
Round numbers or add decimal places:
114+
\`"Temperature: {{ feeds['sensors.temp'].value | round: 1 }}°F"
115+
"Battery: {{ vars.battery | round }}%"\`
116+
117+
### Date Formatting
118+
Format timestamps for readability:
119+
\`"Last updated: {{ feeds['sensor.temp'].updated_at | date: '%B %d at %I:%M %p' }}"\`
120+
Output: "Last updated: January 15 at 03:45 PM"
121+
122+
### Math Operations
123+
Perform calculations in templates:
124+
\`"Temperature in Celsius: {{ feeds['sensors.temp'].value | minus: 32 | times: 5 | divided_by: 9 | round: 1 }}°C"\`
125+
126+
## Common Patterns & Tips
127+
128+
### 1. Fallback Values
129+
Provide defaults when data might be missing:
130+
\`"Temperature: {{ feeds['sensors.temp'].value | default: 'No reading' }}"\`
131+
132+
### 2. Multiple Feed Access
133+
Combine data from multiple sources:
134+
\`"Indoor: {{ feeds['home.inside_temp'].value }}°F
135+
Outdoor: {{ feeds['home.outside_temp'].value }}°F
136+
Difference: {{ feeds['home.inside_temp'].value | minus: feeds['home.outside_temp'].value }}°F"\`
137+
138+
### 3. Status Indicators
139+
Use emoji based on values (keep in mind emoji will not work with displays):
140+
\`"Battery: {{ vars.battery_level }}%
141+
{% if vars.battery_level < 20 %}🔴{% elsif vars.battery_level < 50 %}🟡{% else %}🟢{% endif %}"\`
142+
143+
### 4. Time-Based Greetings
144+
\`"{% assign hour = 'now' | date: '%H' | plus: 0 %}
145+
{% if hour < 12 %}Good morning{% elsif hour < 17 %}Good afternoon{% else %}Good evening{% endif %}, {{ user.name }}!"\`
146+
147+
## Troubleshooting Common Issues
148+
149+
### Problem: Variable shows as blank
150+
**Solution:** Check that:
151+
- Variable name is spelled correctly (case-sensitive!)
152+
- Feed key matches exactly (including group.feed format)
153+
- Variable has been set before this template runs
154+
155+
### Problem: Template shows raw {{ }} text
156+
**Solution:** Make sure:
157+
- You're using double curly braces {{ }}
158+
- No extra spaces inside braces
159+
- Feed keys use square brackets and quotes: feeds['key']
160+
161+
### Problem: Timestamp looks weird
162+
**Solution:** Format dates using Liquid filters:
163+
\`{{ feeds['sensor'].updated_at | date: '%Y-%m-%d %H:%M' }}\`
164+
165+
## Best Practices
166+
167+
1. **Keep it readable**: Use line breaks and spacing for multi-line messages
168+
2. **Test with sample data**: Try your template with different values
169+
3. **Use meaningful variable names**: 'kitchen_temp' is better than 'temp1'
170+
4. **Add context**: Include units (°F, %, etc.) and location names
42171
:::
43172
`,
44-
45173
connections: {
46174
mode: "value",
47175
output: [ "expression", "string" ],
48176
},
49-
50177
template: "{{ %TEMPLATE",
51-
52178
inputs: {
53179
TEMPLATE: {
180+
description: `
181+
::: v-pre
182+
Create your template text with static content and dynamic {{ variable }} placeholders.
183+
184+
Examples:
185+
- 'Alert: {{ feeds['temp.kitchen'].value }}°F detected'
186+
- 'Daily Report for {{ user.name }}: Battery at {{ vars.battery_level }}%'
187+
188+
Use {{ }} to insert live data into your message.
189+
:::
190+
`,
54191
check: "expression",
55192
shadow: 'io_text_multiline'
56193
}
57194
},
58-
59195
generators: {
60196
json: (block, generator) => {
61197
const
62198
template = generator.valueToCode(block, 'TEMPLATE', 0) || null,
63-
64199
blockPayload = JSON.stringify({
65200
textTemplate: {
66201
template: JSON.parse(template)
67202
},
68203
})
69-
70204
return [ blockPayload, 0 ]
71205
}
72206
},
73-
74207
regenerators: {
75208
json: (blockObject, helpers) => {
76209
const
77210
{ template } = blockObject.textTemplate,
78211
inputs = {
79212
TEMPLATE: helpers.expressionToBlock(template, { shadow: "io_text" })
80213
}
81-
82214
return { type: 'text_template', inputs }
83215
}
84216
}

src/definitions/block_definition.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ BlockDefinition.parseRawDefinition = function(rawBlockDefinition, definitionPath
191191
? niceTemplate(rawBlockDefinition.description)
192192
: ""
193193
blockDef.ioPlus = rawBlockDefinition.ioPlus
194-
blockDef.tooltip = blockDef.description.split("\n")[0]
194+
// take the first line of the description
195+
// blockDef.tooltip = blockDef.description.split("\n")[0]
196+
// take the first sentence of the description
197+
blockDef.tooltip = blockDef.description.split(/\.(\s|$)/)[0] + "."
195198
blockDef.disabled = !!rawBlockDefinition.disabled
196199
blockDef.connections = rawBlockDefinition.connections
197200
blockDef.template = rawBlockDefinition.template

src/docs/render_block_inputs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const
3636
if(input.type === 'label') { return }
3737

3838
lines.push(`### \`${ capitalize(inputName) }\``)
39-
lines.push(input.description)
39+
if(input.description) { lines.push(niceTemplate(input.description)) }
4040
})
4141

4242
return lines.join("\n\n")

0 commit comments

Comments
 (0)