Get % left on counter (initial is 200) - Solved

This maybe simple, but I am just not getting it right…

I created a counter with the initial value at 200. Every “trigger” will decrement the counter by 3.

I need to create a template sensor that will tell me how much (%) is left until I get to 0% with the initial 200 being 100%

I tried this, but just can’t seem to get it right…

- platform: template
  sensors:
    hydrocortisone_bottle:
      friendly_name: "Hydrocortisone"
      entity_id: sensor.volume
      value_template: >-
          {{ states.counter.hydrocortisone | float  /200  | round }}
      unit_of_measurement: "%"

Can someone give me some pointers?

Many thanks!

Try this:

      value_template: >-
          {{ states.counter.hydrocortisone | float  / 200 * 100  | round }}

hey @ondras12345 thanks for getting back to me.

I changed it to this as per your siuggestion, and I am getting a % now which is great! Thanks!

The counter is however currently at 200, however the template sensor is showing 0.0% Should this not be 100%?

Thanks again for your help

Try this:

      value_template: >
          {{ ( 100 * states('counter.hydrocortisone')|float  / 200 )  | round }}

The previous template was not accessing the counter state correctly and had a couple of “order of operations” issues. Like dividing by 20000 and rounding the integer 100 instead of the result of the calculation.

BRILLIANT!

Thank you very much