How to get a percentage with Value_Template?

I have the below.

- platform: snmp
  name: Dell 2130CN Cyan
  host: 192.168.0.4
  baseoid: 1.3.6.1.2.1.43.11.1.1.9.1.1
  accept_errors: true
  scan_interval: 86400
  unit_of_measurement: '%'

So this shows how many pages are left that it can print for each color. Which doesn’t make sense to me as each page isn’t only printing one color. I just want to know the percent of each color left. So assumption is when it’s full there is 2500 sheets it can print. So I was thinking divide how many pages the above oid returns by 2500 and multiply it by 100. Without a value_template I get 2500 for one I just replaced. But when I try value templates it just gives me a dash.

Any help would be appreciated.

1 Like

Paste your value_template code and we’ll have a look see :slight_smile:

Not sure I fully understand what you want but my guess is you’ll have to convert the value to a number, so add | int after the value before you divide. May need some parenthesis too

- platform: snmp
  name: Dell 2130CN Cyan
  host: 192.168.0.4
  baseoid: 1.3.6.1.2.1.43.11.1.1.9.1.1
  accept_errors: true
  scan_interval: 86400
  unit_of_measurement: '%'
  value_template: "{{ states('sensor.dell_2130cn_cyan')|float * 1024 }}"

Also as apparently my rambling didn’t make sense, the above baseoid returns 2500

That’s 2500 pages of black left. I want it in % left

Well if 2500 is full then take the pages left / 2500 * 100 to get percent ink left

Replace states('sensor.dell_2130cn_cyan') with value

Actually from what you’ve said about the unit of the number returned, don’t you want

value_template: "{{ (value | float / 1024)*100 }}" ?

I tried that but will try again. Do I have to leave the sensor as is without the value_template and create a separate template sensor or something? Leaving the above without the value_template then creating the below gives me 100% but is another sensor to maintain:

- platform: template
  sensors:
    dell_cyan_level:
      entity_id: sensor.dell_cyan_level
      value_template: "{{ states('sensor.dell_2130cn_cyan')|int /2500 * 100 }}"
1 Like

According to the docs the SNMP sensor accepts the value_template argument so should work

That worked. I must have had the brackets in the wrong place or something as tried value before. End value for my situation

value_template: "{{ (value | float / 2500)*100 }}"

By the way, one other thing I’m wondering with value_template. If I have a value that has a semi-colon in it and I want the left half of it, how would I do that? So if an SNMP call returns:

Dell 2130cn Color Laser; Net 13.22,ESS 200906261554

How would I just get the Dell 2130cn Color Laser part?

Thanks.

JR

Figured it out

value_template: "{{ value.split(';')[0] }}"