question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"variables" undefined in nested configuration templates

See original GitHub issue

Checklist

  • [x ] I updated the card to the latest version available
  • [ x] I cleared the cache of my browser

Describe the bug I’ve declared a configuration template that is nested within another configuration template. The nested template cannot take variables sent to it (from local config), and pass them through to the parent template.

Version of the card Version: 3.4.2

To Reproduce This is the configuration I used:

button_card_templates:

  button_info:
    variables:
      info_icon: information
    show_label: true
    label: '[[[ return variables.info ]]]'
    icon: '[[[ return "mdi:" + variables.info_icon ]]]'

  sensor_battery_info:
    template: button_info
    variables:
      info: '[[[ return states[variables.info_entity].state + "%" ]]]'
      info_icon: battery

# This works fine
type: custom:button-card
  template: button_info
  entity: media_player.player1
  variables:
    info: 'Player1 active'

# This throws the error below
type: custom:button-card
  template: sensor_battery_info
  entity: binary_sensor.contact_sensor_01
  variables:
    info_entity: sensor.contact_sensor_01_power

Error message On Firefox: ButtonCardJSTemplateError: TypeError: variables is undefined in 'return states[variables.info_entity].state + "%"' On Edge: ButtonCardJSTemplateError: TypeError: Cannot read property 'info_entity' of undefined in 'return states[variables.info_entity].state + "%"'

Expected behavior The variables object should be available to the nested template at the time of defining its own variables, to allow passing variables: Local config > nested template > parent template. I cannot think of a workaround to this issue, unfortunately. I don’t think a second (non-nested) template would solve this.

Desktop (please complete the following information):

  • Browser: tested on Firefox and Edge.
  • Version Firefox 91.0.1, Edge 92.0.902.78

Smartphone (please complete the following information):

  • Android 11 on Pixel 5, HASS Companion App

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
RomRidercommented, Aug 22, 2021

You can’t reference variables inside other variables. That’s intended because too complex to handle.

0reactions
amitkeretcommented, Apr 19, 2022

where the variable ‘info_entity’ comes from?

Oooooops that’s a typo… should be the state of entity as expected. I’ve amended my comment above. I’m sorry @u8915055 for all the hairs you pulled 😨

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to keep multilevel undefined variable not change when ...
To make sure an undefined variable always has a default in your template, there is a so called default filter
Read more >
Feature: Support nested templates with inner scope · Issue #484
When expressionEvaluationOptions scope set to 'inner' for nested template, inner parameters and variables are marked as undefined #719.
Read more >
Where variables can be used - GitLab Docs
Supported are all variables defined for a job (project/group variables, variables from .gitlab-ci.yml , variables from triggers, variables from pipeline ...
Read more >
Variables in templates - Azure Resource Manager
Define variable; Use variable; Example template; Configuration variables; Next steps. This article describes how to define and use variables ...
Read more >
Using ES6 To Destructure Deeply Nested Objects in ... - ITNEXT
The Issue: ES6 Object Destructuring & Deeply Nested Objects ... the value unpacked from the object is undefined , is that that variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found