From fd069f7447922171e5049c596b10cb276bcd1236 Mon Sep 17 00:00:00 2001 From: Lucas Chaim Date: Fri, 3 Jul 2020 18:57:08 -0300 Subject: [PATCH 1/4] Fixed broken README.md links Small updates to match the current URLs. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 862dddb..6d535b1 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,9 @@ resources: | Variable | Description | | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `this.hass` | The [hass](https://developers.home-assistant.io/docs/en/frontend_data.html) object | -| `states` | The [states](https://developers.home-assistant.io/docs/en/frontend_data.html#hassstates) object | -| `user` | The [user](https://developers.home-assistant.io/docs/en/frontend_data.html#hassuser) object | +| `this.hass` | The [hass](https://developers.home-assistant.io/docs/frontend/data/) object | +| `states` | The [states](https://developers.home-assistant.io/docs/frontend/data/#hassstates) object | +| `user` | The [user](https://developers.home-assistant.io/docs/frontend/data/#hassuser) object | | `vars` | Defined by `variables` configuration and accessible in your templates starting at the 0th index as your firstly defined variable to help clean up your templates | ```yaml From d774ec8765020bf7c21a9fe91cd364677492bdae Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Thu, 10 Sep 2020 23:26:51 +0200 Subject: [PATCH 2/4] Added named variables --- README.md | 36 ++++++++++++++++++------------------ src/config-template-card.ts | 26 ++++++++++++++++++++------ src/types.ts | 2 +- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 6d535b1..ed0d4f1 100644 --- a/README.md +++ b/README.md @@ -39,43 +39,43 @@ resources: ## Options -| Name | Type | Requirement | Description | -| --------- | ------ | ------------ | ----------------------------------------------------------------------------------------------------- | -| type | string | **Required** | `custom:config-template-card` | -| card | object | **Required** | Card object | -| entities | list | **Required** | List of entity strings that should be watched for updates. Templates can be used here | -| variables | list | **Optional** | List of variables, which can be templates, that can be used in your `config` and indexed using `vars` | +| Name | Type | Requirement | Description | +| --------- | ----------- | ------------ | ---------------------------------------------------------------------------------------------------------------- | +| type | string | **Required** | `custom:config-template-card` | +| card | object | **Required** | Card object | +| entities | list | **Required** | List of entity strings that should be watched for updates. Templates can be used here | +| variables | list/object | **Optional** | List of variables, which can be templates, that can be used in your `config` and indexed using `vars` or by name | ### Available variables for templating -| Variable | Description | -| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `this.hass` | The [hass](https://developers.home-assistant.io/docs/frontend/data/) object | -| `states` | The [states](https://developers.home-assistant.io/docs/frontend/data/#hassstates) object | -| `user` | The [user](https://developers.home-assistant.io/docs/frontend/data/#hassuser) object | -| `vars` | Defined by `variables` configuration and accessible in your templates starting at the 0th index as your firstly defined variable to help clean up your templates | +| Variable | Description | +| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `this.hass` | The [hass](https://developers.home-assistant.io/docs/frontend/data/) object | +| `states` | The [states](https://developers.home-assistant.io/docs/frontend/data/#hassstates) object | +| `user` | The [user](https://developers.home-assistant.io/docs/frontend/data/#hassuser) object | +| `vars` | Defined by `variables` configuration and accessible in your templates to help clean them up. If `variables` in the configuration is a yaml list, then `vars` is an array starting at the 0th index as your firstly defined variable. If `variables` is an object in the configuration, then `vars` is a string-indexed map and you can also access the variables by name without using `vars` at all. | ```yaml type: 'custom:config-template-card' variables: - - states['light.bed_light'].state - - states['cover.garage_door'].state + LIGHT_STATE: states['light.bed_light'].state + GARAGE_STATE: states['cover.garage_door'].state entities: - light.bed_light - cover.garage_door - alarm_control_panel.alarm - climate.ecobee card: - type: "${vars[0] === 'on' ? 'glance' : 'entities'}" + type: "${LIGHT_STATE === 'on' ? 'glance' : 'entities'}" entities: - entity: alarm_control_panel.alarm - name: "${vars[1] === 'open' && states['alarm_control_panel.alarm'].state === 'armed_home' ? 'Close the garage!' : ''}" + name: "${GARAGE_STATE === 'open' && states['alarm_control_panel.alarm'].state === 'armed_home' ? 'Close the garage!' : ''}" - entity: binary_sensor.basement_floor_wet - entity: climate.ecobee name: "${states['climate.ecobee'].attributes.current_temperature > 22 ? 'Cozy' : 'Too Hot/Cold'}" - entity: cover.garage_door - - entity: "${vars[0] === 'on' ? 'light.bed_light' : 'climate.ecobee'}" - icon: "${vars[1] === 'open' ? 'mdi:hotel' : '' }" + - entity: "${LIGHT_STATE === 'on' ? 'light.bed_light' : 'climate.ecobee'}" + icon: "${GARAGE_STATE === 'open' ? 'mdi:hotel' : '' }" ``` Templated entities example diff --git a/src/config-template-card.ts b/src/config-template-card.ts index a453445..9f23d73 100644 --- a/src/config-template-card.ts +++ b/src/config-template-card.ts @@ -143,15 +143,29 @@ export class ConfigTemplateCard extends LitElement { /* eslint-disable @typescript-eslint/no-unused-vars */ const user = this.hass ? this.hass.user : undefined; const states = this.hass ? this.hass.states : undefined; - const vars: any[] = []; + let vars: any[] | { [key: string]: any }; + let varDef = ''; if (this._config) { - for (const v in this._config.variables) { - const newV = eval(this._config.variables[v]); - vars.push(newV); + if (Array.isArray(this._config.variables)) { + // if variables is an array, create vars as an array + vars = []; + for (const v in this._config.variables) { + const newV = eval(this._config.variables[v]); + vars.push(newV); + } + } else { + // if it is an object, then create a key-value map containing + // the values + vars = {}; + for (const varName in this._config.variables) { + const newV = eval(this._config.variables[varName]); + vars[varName] = newV; + // create variable definitions to be injected: + varDef = varDef + `var ${varName} = vars['${varName}'];\n`; + } } } - - return eval(template.substring(2, template.length - 1)); + return eval(varDef + template.substring(2, template.length - 1)); } } diff --git a/src/types.ts b/src/types.ts index b6d0427..10f5aef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,6 +3,6 @@ import { LovelaceCardConfig } from 'custom-card-helpers'; export interface ConfigTemplateConfig { type: string; entities: string[]; - variables?: string[]; + variables?: string[] | { [key: string]: string }; card?: LovelaceCardConfig; } From 8a54a677cf3bf39f80385d2c5d844ab0310332bb Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Mon, 26 Oct 2020 23:28:48 -0500 Subject: [PATCH 3/4] Update const.ts --- src/const.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/const.ts b/src/const.ts index 38e11f4..6a82ac0 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1 +1 @@ -export const CARD_VERSION = '1.2.0'; +export const CARD_VERSION = '1.2.1'; From 86a1d573820bce60cc2e8e96154470fc61b16500 Mon Sep 17 00:00:00 2001 From: "Filipsson Jens (mmc493)" Date: Sat, 7 Nov 2020 11:12:15 +0100 Subject: [PATCH 4/4] Updated readme with example of global method in variables --- README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ed0d4f1..5bf9852 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ card: icon: "${GARAGE_STATE === 'open' ? 'mdi:hotel' : '' }" ``` -Templated entities example +## Templated entities example ```yaml type: 'custom:config-template-card' @@ -92,6 +92,33 @@ card: name: "${states[vars[0]].state === 'on' ? 'Light On' : 'Light Off'}" ``` +## Defining global functions in variables + +If you find yourself having to rewrite the same logic in multiple locations, you can define global methods inside Config Template Card's variables, which can be called anywhere within the scope of the card: + +```yaml +type: 'custom:config-template-card' + variables: + setTempMessage: | + temp => { + if (temp <= 19) { + return 'Quick, get a blanket!'; + } + else if (temp >= 20 && temp <= 22) { + return 'Cozy!'; + } + return 'It's getting hot in here...'; + } + currentTemp: states['climate.ecobee'].attributes.current_temperature + entities: + - climate.ecobee + card: + type: entities + entities: + - entity: climate.ecobee + name: '${ setTempMessage(currentTemp) }' +``` + ### Note: All templates must be enclosed by `${}` [Troubleshooting](https://github.com/thomasloven/hass-config/wiki/Lovelace-Plugins)