Pull just number from attribute

Hey all, looking for a little guidance with working with a template.


{{ state_attr('binary_sensor.jarvis_server', 'arrayTotalSpace') }} results in 51 TB.

How would I get it to give me just 51 and not the full 51 TB

You could have searched the forum, plenty examples…try this in devtools > template

{{ '51 TB'[:2] }}

This slices off the last three characters of the string and reports whatever remains.

{{ state_attr('binary_sensor.jarvis_server', 'arrayTotalSpace')[:-3] }} 

Reference: Python String Slicing

1 Like

Yeah…3… indeed :slight_smile:

Thank you!!! Worked perfect

This is the correct solution! You can also split by string:

...'blah').split()[0]

This gives you the first part before the space in the string.

You can also use replace like this:

{{ state_attr('binary_sensor.jarvis_server', 'arrayTotalSpace').replace(' TB', '') }}

However, the most concise method is to simply slice off the last 3 characters using [:-3]. Using [:2] will return the first 2 characters and assumes the desired value will never exceed 2 characters.

1 Like