iantrich / config-template-card

📝 Templatable Lovelace Configurations
MIT License
425 stars 55 forks source link

Addition doesn't work in templates (but subtraction does) #140

Closed mcmanigle closed 6 months ago

mcmanigle commented 6 months ago

Checklist:

Release with the issue: 1.3.6 running on HA 2024.1.3 via HACS

Last working release (if known): N/A

Browser and Operating System: MacOS 14.0 running Safari 17.0

Description of problem: There seems to be an odd bug where addition in the templated fields doesn't work (but subtraction does). For example, trying to make a gauge card with a target temperature:

type: custom:config-template-card
variables:
  TARGET_TEMP: states['input_number.grill_probe_1_target'].state
entities:
  - input_number.grill_probe_1_target
card:
  type: gauge
  entity: sensor.igrill_probe_1
  min: 10
  max: 250
  needle: true
  segments:
    - from: 0
      color: '#aaa'
    - from: ${TARGET_TEMP - 10}
      color: '#dd3'
    - from: ${TARGET_TEMP}
      color: '#3d3'
    - from: ${TARGET_TEMP + 10}
      color: '#933'

The above does not work (shows the last segment at the value of TARGET_TEMP, while the below works just fine:

type: custom:config-template-card
variables:
  TARGET_TEMP: states['input_number.grill_probe_1_target'].state
entities:
  - input_number.grill_probe_1_target
card:
  type: gauge
  entity: sensor.igrill_probe_1
  min: 10
  max: 250
  needle: true
  segments:
    - from: 0
      color: '#aaa'
    - from: ${TARGET_TEMP - 10}
      color: '#dd3'
    - from: ${TARGET_TEMP}
      color: '#3d3'
    - from: ${TARGET_TEMP - -10}
      color: '#933'

Note the second-to-last line of each YAML chunk.

Javascript errors shown in the web inspector (if applicable): None

Additional information:

ildar170975 commented 6 months ago

States are strings. Should be converted to numbers first. Any math operations with strings may give unpredicted results. Try

from: ${parseFloat(TARGET_TEMP) - 10}
mcmanigle commented 6 months ago

Thank you! Added parseFloat to the variable declaration and it indeed works fine.