[Solved] Template Sensor convert file sizes bytes to KB to MB

Hi everybody,

could somebody please help me how I need to setup a template sensor to display my up- and download speeds in KB/s or when appropriate in MB/s? Currently the sensor reports the rates in bytes which is not very useful to me. I guess I need to convert the bytes to KB or MB and the also change the unit of measurement when a threshold is reached and this exercise currently exceeds my knowledge.

For what its worth I am using the fritzbox Netmonitor sensor which reports the current speeds in bytes/s.

Thanks for you help!

Something like this

{{ states.sensor.bytes_sent | filesizeformat() }}/s


Oh brilliant! Thank you so much for your fast reply and superb help!

Hello, I am a new user of HA I also have a problem with conversion to Mb / s
where exactly should I put this line to work?
{{states.sensor.bytes_sent | filesizeformat ()}} / s

i’m converting from bytes to mb with this code below:

  - platform: template
        friendly_name: "MB Received"
        unit_of_measurement: 'MB/s'
        value_template: "{{ states('sensor.archerc7v4_bytes_received')|float / 1048576 }}"
        friendly_name: "MB Sent"
        unit_of_measurement: 'MB/s'
        value_template: "{{ states('sensor.archerc7v4_bytes_sent')|float / 1048576 }}"

How can I round the result to a whole number

Have you tried piping it to round like so?:

value_template: "{{ states('sensor.archerc7v4_bytes_sent')|float / 1048576 | round(0) }}"

thank you for your help, unfortunately still not working

        friendly_name: "MB Sent"
        unit_of_measurement: 'MB/s'
        value_template: "{{ states('sensor.archerc7v4_bytes_sent')|float / 1048576 | round(0) }}"

someone has an idea to solve this issue?

I guess you need to realize what you are cutting off, in your case 1048576 is only stripped.
You probably want to have your whole calculation stripped of like this example:

“{{ (states.sensor.fritz_router.attributes.bytes_sent | float / 1073741824) | round(2) }}”


        friendly_name: "MB Received"
        unit_of_measurement: 'MB/s'
        value_template: "{{ states('sensor.frlmx_b_received')|float / 1073741824 | round(2) }}"
        friendly_name: "MB Sent"
        unit_of_measurement: 'MB/s'
        value_template: "{{ states('sensor.frlmx_b_sent')|float / 1073741824 | round(2) }}"


pipes “|” take priority so what you are doing is rounding 1073741824
which surprisingly is 1073741824
use more brackets

ok, but where should I use the brackets?

I’m not sure if you are kidding but …

  • states(‘sensor.frlmx_b_sent’) | float
    (putting spaces in makes it easier to read) takes the value (state) of sensor.frlmx_b_sent and casts it to float (makes a text string into a number value with decimals) This is the first calculation
  • 1073741824 | round(2)
    Takes 1073741824 and casts it to round(2) 2 decimal places, but it has no decimal places so it stays as it is - second calculation
  • float_value / rounded_value
    (\ is way down the pecking order) just divides float_value by 1073741824 third calculation

So you need to add brackets to shift the priorities, so that you | round(2) last of all
So : -
“{{ (states(‘sensor.frlmx_b_sent’) | float / 1073741824) | round(2) }}”
This moves the second calculation to “divide by 1073741824

Each of these are ‘operations’ not strictly ‘calculations’ as changing something from text to a number is a grey area - calculation wise

Thank you, and sorry I am not kidding because I am a bloddy beginner.

The sensor brings this resulat at the moment:


a doublecheck of the entity from fritzbox show


So, I think the value 2,22 MB/s is not correct

You know that these are decimal representaions of binary numbers ?
1,2,4,8,16,32,64,128,256,512,1024 etc (shortcut 2^n)
So a Kilo (1000) bit is 1024
So strictly speaking k to M should be divided by 1024
I have no Idea where you got 1073741824 from (but that was NOT the question you asked)
This is hard for some people to understand so they have ‘decimalised’ binary numbers and it is quite common to see Mb * 1000 = kb (thats Mb = kb / 1000)
If you don’t like the answer - change the divisor

you are absolutly right, 1073741824 bytes = 1 GB

Yes but
kb / 1,073.741824 does not equate to Mb

What level did you complete Maths to ?

sensor.frlmx_b_sent = bytes / 1073741824 = …GB

As I said it depends
see : -