Int to string (or: hide decimal places)?

Hi everybody,

I have this sensor grabbing data from grocy via it’s API:

sensor:
    # Wasser Nordquell Garage
    - platform: rest
      resource: http://home:9283/api/stock/products/1
      headers:
        GROCY-API-KEY: !secret grocy_api_key
      name: "Vorrat Wasser Nordquell Garage"
      value_template: "{{ value_json.stock_amount | int / 12 }}"
      unit_of_measurement: "Kisten"
      scan_interval: 300
      json_attributes:
        - last_purchased
        - last_used

This will display it like this (entity on the very right)

I tried changing the value template to "{{ value_json.stock_amount | int / 12 | round(0) }}" as well as "{{ value_json.stock_amount | int / 12 | string }}". This will both still display 5.0.

The value can and will always be an integer without any decimal places (as it will always divide a number divisable by 12 with 12). So I would prefer to display it as an integer, not a float.

When I display the value in developer tools -> template like this {{ states("sensor.vorrat_wasser_nordquell_garage") }}, it will show 5 only, but lovelace still shows the comma. I thought of something like {{ (value_json.stock_amount | int / 12).split(",")[0] }}, but that just feels wrong. Is there a cleaner way to do this?

Thank you for your input :slight_smile:

"{{ ((value_json.stock_amount | float) / 12) | int }}"

(There are more parentheses than needed, but it helps you understand what’s going on)

Your first attempt is producing an int but then dividing it by 12. Just because you know your starting point is going to be a multiple of 12 isn’t going to make that produce an int. Your other two attempts are rounding and stringifying the 12, not the result.

1 Like

D’oh, of course!! Thank you so much, it works just as expected :slight_smile:

1 Like