Skip to content

Conversation

psi-4ward
Copy link

@psi-4ward psi-4ward commented Jan 22, 2024

Add the ability that a template could return an array of entities.

Ie:

triggers_update: >-
  [[[ 
    const result = [];
    for(let i = 0; i<10; i++) result.push(`lights.light${i}`);
    return result;
  ]]]

https://community.home-assistant.io/t/lovelace-button-card/65981/7129?u=psi

Add the ability that a template could return an array of entities.
@RomRider RomRider changed the base branch from master to dev January 22, 2024 12:45
@RomRider
Copy link
Collaborator

Good idea, thanks!
I'd add the possibility to do this also:

triggers_update:
  - >-
    [[[ 
      const result = [];
      for(let i = 0; i<10; i++) result.push(`lights.light${i}`);
      return result;
    ]]]
  - sensor.other_entity

Also update the documentation to reflect this change, please

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for returning an array of entities from the triggers_update template, allowing dynamic generation of multiple entity IDs through JavaScript templates.

  • Add array handling for triggers_update template results
  • Enable templates to return multiple entities via array instead of single entity string

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@@ -214,7 +214,11 @@ class ButtonCard extends LitElement {
} else if (typeof this._config!.triggers_update === 'string') {
const result = this._getTemplateOrValue(this._stateObj, this._config!.triggers_update);
if (result && result !== 'all') {
this._entities.push(result);
if(Array.isArray(result)) {
Copy link
Preview

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after 'if' keyword. Should be 'if (Array.isArray(result))' to maintain consistent code formatting.

Suggested change
if(Array.isArray(result)) {
if (Array.isArray(result)) {

Copilot uses AI. Check for mistakes.

@@ -214,7 +214,11 @@ class ButtonCard extends LitElement {
} else if (typeof this._config!.triggers_update === 'string') {
const result = this._getTemplateOrValue(this._stateObj, this._config!.triggers_update);
if (result && result !== 'all') {
this._entities.push(result);
if(Array.isArray(result)) {
result.forEach(entity => this._entities.push(entity));
Copy link
Preview

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing input validation for array elements. The code should validate that each entity in the array is a valid string before pushing to _entities to prevent potential runtime errors.

Suggested change
result.forEach(entity => this._entities.push(entity));
result.forEach(entity => {
if (typeof entity === 'string' && entity.length > 0) {
this._entities.push(entity);
}
});

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants