Templating without parenthesis

Please could someone advise how to overcome this problem with templating. I have created the following template:

- platform: template
  sensors:
    unraid_array_space:
      friendly_name: UnRAID Array Space
      value_template: '{{state_attr("binary_sensor.storage_server", "diskSpace")}}'

The template senso outputs the the following state.

23,063 GB used of 33 TB (69.9 %)

However I only want the % value without the parenthesis, please could someone show me how this might be done?

This works for me

value_template: >
  {% set s = state_attr("binary_sensor.storage_server", "diskSpace") %}
  {{ s.split('(')[1].split(')')[0] }}

Thanks a lot I’ll give that a go in the morning :slight_smile:

or this (change 2nd string only)
I’m not a Python guru but there are many ways to do that

{{ s[s.find('(')+1 : s.rfind(')')] }}

Yet another way to do it:

- platform: template
  sensors:
    unraid_array_space:
      friendly_name: UnRAID Array Space
      value_template: >
        {{ state_attr("binary_sensor.storage_server", "diskSpace")
           | regex_findall_index(".*\((\d+.?\d+) %\)") | float }}
      unit_of_measurement: '%'

The result is a float value so you can optionally add the round filter to the template to round the value. If you just want the numerical value without the % symbol, just remove unit_of_measurement.

@123 @AhmadK Thank you both for your replies they both worked.

image

I used your template @123 for my solution thanks.

1 Like

you are welcome.
just keep in mind that neither of the solutions above will work if you receive state like unknown/unavailable.
maybe it’s a good idea to add a simple check to your value_template

@AhmadK @123 have a another templating question.

- platform: snmp
  name: 'Server DISK1'
  host: 1.2.3.4
  port: 161
  community: public
  baseoid: 1.3.6.1.4.1.8072.1.3.2.4.1.2.8.100.105.115.107.116.101.109.112.2
  accept_errors: true
  value_template: ????

outputs the following value

WDC_WD60EFRX-68L0BN1_WD-WX51D586RAA9: 31

Please could you help me with the value template to only output the numer?

I think it’s time to learn some python :wink:
One version:

{{ value.split(':')[1].strip() }}

Thans for your quick response I get this error when I use your suggestion

expected <block end>, but found '<scalar>'

I tried enclosing it in ’ ’ but that didnt work either

value_template: >
  {{ value.split(':')[1].strip() }}

using regular expressions

value_template: >
  {{ value | regex_findall_index("(\d+)$") }}

@AhmadK I do need to learn regex :safety_vest:

Legend, thanks thats working great :slight_smile:

I’d recommend to start with string methods.
And btw, your previous issue was related to YAML so it’s a good idea to learn some basics.
Take a look at this book, might be worth to read.

1 Like

Yeah I realised that I misssed the ‘>’ sorry that was my bad :expressionless:

Ideal thanks for the read I’ve bookmarked the site and I’ll get reading…

1 Like