-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Support variables, icon, and picture for all template platforms #39634
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
base: next
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for home-assistant-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
📝 WalkthroughWalkthroughThe documentation for the template integration was updated to introduce a new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant TemplateIntegrationDoc
User->>TemplateIntegrationDoc: Reads about 'variables' config option
TemplateIntegrationDoc-->>User: Shows example using 'variables'
User->>TemplateIntegrationDoc: Reads explanation of 'variables' behavior
TemplateIntegrationDoc-->>User: Describes state-based vs trigger-based resolution
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
source/_integrations/template.markdown (1)
218-226
: Review thevariables
schema documentation
The addedvariables
section in the configuration reference is accurate. Since picture support was also introduced for all template platforms, consider enhancing the example under “Common Device Configuration Options” to include thepicture
attribute alongsidevariables
to demonstrate its usage in practice.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting
📒 Files selected for processing (1)
source/_integrations/template.markdown
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: Redirect rules - home-assistant-docs
- GitHub Check: Header rules - home-assistant-docs
- GitHub Check: Pages changed - home-assistant-docs
variables: | ||
my_entity: sensor.watts | ||
availability: "{{ my_entity | has_value }}" | ||
icon: "{{ 'mdi:flash-alert' if states(my_entity) | float > 100 else 'mdi:flash' }}" | ||
name: "{{ states(my_entity) }} Alert" | ||
# Entity specific configuration options | ||
state: "{{ states('sensor.watts') | float > 100}}" | ||
state: "{{ states(my_entity) | float > 100}}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect filter usage in availability
example
The example uses {{ my_entity | has_value }}
which tests the variable string itself (always non-empty) instead of the entity’s state. Change to apply the filter to states(my_entity)
.
- availability: "{{ my_entity | has_value }}"
+ availability: "{{ states(my_entity) | has_value }}"
📝 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.
variables: | |
my_entity: sensor.watts | |
availability: "{{ my_entity | has_value }}" | |
icon: "{{ 'mdi:flash-alert' if states(my_entity) | float > 100 else 'mdi:flash' }}" | |
name: "{{ states(my_entity) }} Alert" | |
# Entity specific configuration options | |
state: "{{ states('sensor.watts') | float > 100}}" | |
state: "{{ states(my_entity) | float > 100}}" | |
variables: | |
my_entity: sensor.watts | |
availability: "{{ states(my_entity) | has_value }}" | |
icon: "{{ 'mdi:flash-alert' if states(my_entity) | float > 100 else 'mdi:flash' }}" | |
name: "{{ states(my_entity) }} Alert" | |
# Entity specific configuration options | |
state: "{{ states(my_entity) | float > 100}}" |
🤖 Prompt for AI Agents
In source/_integrations/template.markdown around lines 184 to 190, the
availability line incorrectly applies the has_value filter to the variable
my_entity string instead of the entity's state. Update the availability line to
apply the has_value filter to states(my_entity) instead of my_entity directly,
so it correctly checks if the entity's state has a value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion is wrong and you should feel bad about it coderabbitai
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Petro31 You're absolutely right, and I apologize for the incorrect suggestion.
The has_value
filter in Home Assistant is designed to be applied directly to entity IDs (strings), not to state values. In your example:
variables:
my_entity: sensor.watts
availability: "{{ my_entity | has_value }}"
This correctly applies has_value
to the entity ID string "sensor.watts"
(stored in my_entity
) to check if that entity has a valid state. My suggestion to change it to {{ states(my_entity) | has_value }}
was incorrect because it would apply the filter to the state value itself rather than the entity ID.
Thank you for the correction - the original code in the documentation is correct as written.
Learnings Added:
- The
has_value
filter in Home Assistant templates should be applied to entity IDs (strings), not to state values returned bystates()
. It checks if the specified entity has a valid state (not None, unavailable, or unknown).
Proposed change
Currently variables in blueprints are only passed to sensor and binary_sensor entity platforms.
This PR passes the variables field to all template entity platforms.
This fix makes a new generic function make_template_entity_common_modern_schema that allows variables to be a valid configuration option for all entity platforms. So when variables are stripped from the blueprint and added to the entity platform configuration, the entity platform config no longer errors.
In addition to the changes above, the PR also adds missing features to some platforms. The weather platform now allows icon and picture, and button, number, and select platforms now accept picture.
Lastly, this PR also unifies the default naming strategy across all platforms. When name and unique_id is excluded, all platforms will default to Template . Prior to this change, some platforms defaulted to Unnamed Entity.
Type of change
current
branch).current
branch).next
branch).next
branch).Additional information
Checklist
current
branch.next
branch.Summary by CodeRabbit
variables
configuration option for trigger-based template entities.variables
key and its behavior.