Template Help - Trying to convert sensor value from KB to MB

Hi,

Looking for some help with my configuartion setup, been on this for two days on and off now and just cant get my head around it! i currently have the Qbittorrent intergration installed however this reports KB/s whereas im hoping to report it in MB, i’ve read through the documentation and i’ve set my configuration.yaml file as follows:

template:
  sensor:
    - name: 'Qbit Download 3'
      unique_id: 'Qbit Download'
      state:  '{{ state.sensor.qbittorrent_down_speed | float / 1000 }}'

When i go to home assistant i can see an entity by the name of Qbit Download 3 however it is showing as unavailable is there any chance someone can please help with where i’ve gone wrong with this

I recommend spending time reading up on templating if you plan to use templates. There’s even an example in the docs that show this:

      state: "{{ states('sensor.qbittorrent_down_speed') | float(0) / 1000 }}"

Thank you so much and sorry for wasting your time!

No time wasted, only your own! People tend to avoid the docs. If you take the time learning where things are in the doc’s, you can cut down your HA setup time drastically.

1 Like

@Av4k100
Just for your information: 1Mb is 1024 Kb,so it should be:

state: "{{ states('sensor.qbittorrent_down_speed') | float(0) / 1024 }}"

https://unitconversion.io/1-mb-to-kb

1 Like

I’m going to have to have a good proper read through when i have some more free time any chance you could let me know how to round the output im receiving i tried adding in | round(1) at the end but that didnt work :frowning:

You have to wrap everything you want to round in parenthesis. You might want to brush up on your highschool math and order of operations: Please Excuse My Dear Aunt Sally, or PEDMAS: Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction. With an added bonus of filters being last. Round is a filter, | is the apply filter operation. So wrap everything before the | in parenthesis (not including the {{).

okay im sorry but im still confused i understand PEDMAS but im not that great with coding do you mean wrap the states.sensor bit in another set of brackets or the round? im so confused :persevere:

It’s not coding. It’s math. Don’t let the idea of coding blind you. You’re making it more complicated than it is.

You currently have

X / 1024 | round(1)

where X is states('sensor.qbittorrent_down_speed') | float(0)

You want to round the whole result. You know that you have to wrap the whole result in parenthesis.

(X / 1024) | round(1)
1 Like

Thank you for your patience i understand and im now getting the correct output :smiley: