Rounding off in template to 0 decimal places

I have a template that’s used to control the size and alignment of the text in a custom:button card:

custom_fields:
  fart: |
    [[[
      return `<span style="align: center; font-size: 120px">${states['sensor.fart'].state}</span>`
    ]]]

I tried to remove the decimals, but I couldn’t get it to work.

This gave me an error:

custom_fields:
  fart: |
    [[[
      return `<span style="align: center; font-size: 120px">${states['sensor.fart'].state | float | round(0) }</span>`
    ]]]
SyntaxError: Unexpected end of input in 'return `<span style="align: center; font-size: 120px">${states['sensor.fart'].state | float | rou...'

I have tried to sprinkle paranthesis around, but I couldn’t get it to work with any combination I could think of. What am I missing here?

A float rounded is still a float. So when you round you’ll get things like 15.0. You can either just convert it directly to int (which I think might just truncate the number) or after the round you could add | int(0).

edit: now that I look, that might not matter with that error. I’d trying using a single quote ' to bracket the text rather than whatever that little tic mark is below the tilde on the keyboard.

That’s a javascript string literal not a jinja template, try something like…

custom_fields:
  fart: |
    [[[
      return `<span style="align: center; font-size: 120px">${ Math.round(states['sensor.fart'].state) } /span>`
    ]]]

Also half farts count too you know, they add up.

@pkscout That is a part of the statement, without it I get another error.

@HappyCadaver Thank you! That worked! And I know, even a fraction of fart can kill… :joy: