EVNotify - Calculate KMs based on battery level - value_template 'value undefined' error

I’m trying to calculate remaining kms, based on the received battery level from EVNotify, based on the assumption of 33kWh and an average of 11.9kWh/100km.

Can anyone help me figuring out the correct way to do the formulas? I’m getting “value undefined” errors.

Thanks!

- platform: rest
  resource: "https://app.evnotify.de/soc?akey=1234&token=1234567"
  name: evnotify_SOC_battery_level
  device_class: battery
  unit_of_measurement: "%"
  value_template: '{{ value_json.soc_display }}'
  scan_interval: 121
  json_attributes:
    - "soc_bms"
    - "last_soc"

- platform: template
  sensors:
    evnotify_remaining_kwh:
      friendly_name: "evnotify_remaining_kwh"
      value_template: '{{ ((states(sensor.evnotify_SOC_battery_level))*100/33) }}'

- platform: template
  sensors:
    evnotify_remaining_kms:
      friendly_name: "evnotify_remaining_kms"
      value_template: '{{ (((states(sensor.evnotify_remaining_kwh))*100)/11.9) }}'

States are strings. You need to convert them to numbers before doing math. Also you are missing the quotes inside states(). Also way too many unneeded parentheses. You probably want an availability template too. And you can have more than one sensor in the platform dictionary:

- platform: template
  sensors:
    evnotify_remaining_kwh:
      friendly_name: "Evnotify Remaining kWh" # you can uses spaces and capital letters here to make this look nice
      value_template: "{{ states('sensor.evnotify_SOC_battery_level')|float(0)*100/33 }}"
      availability_template: "{{ states('sensor.evnotify_SOC_battery_level')|float('none') != 'none' }}"
      icon_template: "{{'mdi:battery-50'}}" # you can change this icon with the state if you like

    evnotify_remaining_kms:
      friendly_name: "Evnotify Remaining kms" # you can uses spaces and capital letters here to make this look nice
      value_template: "{{ states('sensor.evnotify_remaining_kwh')|float(0)*100/11.9 }}"
      availability_template: "{{ states('sensor.evnotify_remaining_kwh')|float('none') != 'none' }}"
      icon_template: "{{'mdi:counter'}}"
2 Likes

Thanks a lot @tom_l ! That makes sense :slight_smile:

Cheers,
anthonws.

1 Like