iantrich / config-template-card

📝 Templatable Lovelace Configurations
MIT License
434 stars 56 forks source link

Added named variables #43

Closed jpeletier closed 3 years ago

jpeletier commented 3 years ago

This PR adds named variables support to variables: in the configuration.

If variables is a key-value map, then vars[] can be indexed by name. Moreover, the variables are injected when evaluating the template and are directly available by name, which greatly improves the readability of a yaml configuration that uses templates, namely:

type: 'custom:config-template-card'
variables:
  LIGHT_STATE: states['light.bed_light'].state
  GARAGE_STATE: states['cover.garage_door'].state
  CURRENT_TEMP: states['climate.ecobee'].attributes.current_temperature
  ALARM_STATE: states['alarm_control_panel.alarm'].state
entities:
  - light.bed_light
  - cover.garage_door
  - alarm_control_panel.alarm
  - climate.ecobee
card:
  type: "${LIGHT_STATE === 'on' ? 'glance' : 'entities'}"
  entities:
    - entity: alarm_control_panel.alarm
      name: "${GARAGE_STATE === 'open' && ALARM_STATE === 'armed_home' ? 'Close the garage!' : ''}"
    - entity: binary_sensor.basement_floor_wet
    - entity: climate.ecobee
      name: "${CURRENT_TEMP > 22 ? 'Cozy' : 'Too Hot/Cold'}"
    - entity: cover.garage_door
    - entity: "${LIGHT_STATE === 'on' ? 'light.bed_light' : 'climate.ecobee'}"
      icon: "${GARAGE_STATE === 'open' ? 'mdi:hotel' : '' }"

This change is backwards-compatible with the current vars[n] syntax.