Floating a value_json from a value_template

Hi there.

I’m pretty new on HA and I’m discovering the templating… :smiley:

Here is a snippet of config in my configuration.yaml :

rest:
  - resource: http://###.###.###.###:55555/
    sensor:
      - name: "CPU Temp"
        value_template: "{{value_json
          | selectattr('SensorClass', 'equalto', 'CPU [#0]: Intel Core i7-14700K: DTS')
          | selectattr('SensorName', 'equalto', 'CPU Entier')
          | map(attribute='SensorValue')
          | first}}"
        unit_of_measurement: "°C"
      - name: "CPU Conso"
        value_template: "{{value_json
          | selectattr('SensorClass', 'equalto', 'CPU [#0]: Intel Core i7-14700K: Enhanced')
          | selectattr('SensorName', 'equalto', 'CPU Conso')
          | map(attribute='SensorValue')
          | first}}"
        unit_of_measurement: "W"
    verify_ssl: false
    timeout: 60
    scan_interval: 6

When I check the logs my issue is :

ValueError: could not convert string to float: '32,9164541194986'

The scrapped json part from my server is :

  {
    "SensorApp": "HWiNFO",
    "SensorClass": "CPU [#0]: Intel Core i7-14700K: Enhanced",
    "SensorName": "CPU Conso",
    "SensorValue": "32,9164541194986",
    "SensorUnit": "W",
    "SensorUpdateTime": 1716806569
  },

After many researches I didn’t find how to float my json value to a rounded 2 decimals float value.

Any one to help on this little thing ?

Thanks in advance for your time and help :slight_smile:

It’s the presence of the comma that is preventing conversion to a float. It expects a decimal point (a period character).

Add the following to the end of the template. It replaces a comma with a period.

replace(',', '.')

For example:


value_template: "{{ value_json | selectattr('SensorClass', 'equalto', 'CPU [#0]: Intel Core i7-14700K: DTS') | selectattr('SensorName', 'equalto', 'CPU Entier') | map(attribute='SensorValue') | first | replace(',', '.') }}"
1 Like

Awesome I understand why my tries were uneffectives :smiley:

Thanks for your help it works like a charm :slight_smile:

1 Like