Trying to convert kb/s sent to Mb/s sent

hi all,

i have connected my router to HA and i’m able to get a reading of the data sent and received in kbyte per second, I’d like to convert that to MB/s if possible.

i done a search on here and found the following post:

https://community.home-assistant.io/t/solved-template-sensor-convert-file-sizes-bytes-to-kb-to-mb/38915

from that i have tried creating a sensor with the following…

- platform: template
  sensors:
    bytes_received:
    friendly_name: "MB Received"
    unit_of_measurement: 'MB/s'
    value_template: "{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_received|filesizeformat()}}/s"
  bytes_sent:
    friendly_name: "MB Sent"
    unit_of_measurement: 'MB/s'
    value_template: "{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_sent|filesizeformat()}}/s"

but i get the following errors:

Invalid config for [sensor.template]: expected a dictionary for dictionary value @ data['sensors']['bytes_received']. Got None
expected a dictionary for dictionary value @ data['sensors']['friendly_name']. Got 'MB Received'
expected a dictionary for dictionary value @ data['sensors']['unit_of_measurement']. Got 'MB/s'
expected a dictionary for dictionary value @ data['sensors']['value_template']. Got '{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_received|filesizeformat()}}/s'
extra keys not allowed @ data['bytes_sent']. Got OrderedDict([('friendly_name', 'MB Sent'), ('unit_of_measurement', 'MB/s'), ('value_template', '{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_sent|filesizeformat()}}/s')]). (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

i have no idea what i am doing really so don’t understand how to fix it… can anyone help please?

edit:

brain fart … i had a look at the yaml again and noticed that my indentation was off…

changed it to


- platform: template
  sensors:
    bytes_received:
      friendly_name: "MB Received"
      unit_of_measurement: 'MB/s'
      value_template: "{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_received|filesizeformat()}}/s"
    bytes_sent:
      friendly_name: "MB Sent"
      unit_of_measurement: 'MB/s'
      value_template: "{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_sent|filesizeformat()}}/s"

and the errors have gone away… however when i select it in a gauge card in lovelace i am hit with the following message…

the card below showing 4.2kbyte/sec recieved is how I would like it to look, though in MB what have I done wrong???

Think there should also be a space before and after the pipe symbol…
Blah blah | more blah

And I think cause it you have to float the thing as it’s numbers…

Tho I’m not 100% at this time of night.

Lovelace error message says: Entity is non-numeric sensor.bytes_sent

It would appear it wants a numeric value and sensor.bytes_sent isn’t providing one.

I assume the arris sensor is reporting a purely numeric value. However, then you subject its state to the filesizeformat filter which not only divides the value by 1000, it also appends kB to the result:
Screenshot%20from%202019-09-29%2018-04-15

Then your template appends /s to that so the final result is a something like 12.5 kB/s which is clearly a string, not a number.

Try this:

      value_template: "{{ (states('sensor.arris_tg2492lg_85_router_kbyte_sec_sent') | int / 1000) | round(1) }}"

It simply divides the arris sensor’s value by 1000 then rounds the result to one decimal place.

Should that be / 1024 ?

Well, they do seem to be decimalising binary numbers, bought a memory stick lately.? :man_shrugging:

Dividing by 1000, and rounding to a single decimal place, produces the same result as using filesizeformat.

Ah, I see filesizeformat does not use binary by default.

filesizeformat ( value , binary=False )

Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega, Giga, etc.), if the second parameter is set to True the binary prefixes are used (Mebi, Gibi).

thank you all for your input and replies.

ive tried the following with the same errors… Entity is non-numeric sensor.bytes_sent

- platform: template
  sensors:
    bytes_received:
      friendly_name: "MB Received"
      unit_of_measurement: 'MB/s'
      value_template: "{{ states.sensor.arris_tg2492lg_85_router_kbyte_sec_received | filesizeformat() }}/s"
    bytes_sent:
      friendly_name: "MB Sent"
      unit_of_measurement: 'MB/s'
      value_template: "{{ (states('sensor.arris_tg2492lg_85_router_kbyte_sec_sent') | int / 1024) | round(1) }}"

also tried to alter the filesizeformat(,bytes=true) (dunno if thats right or not but it didnt work… )

That’s … unusual.

You can tack on a float filter at the end of the template to be doubly certain it produces a numeric value.

  value_template: "{{ (states('sensor.arris_tg2492lg_85_router_kbyte_sec_sent') | int / 1024) | round(1) | float }}"

If that still causes it to report: ‘Entity is non-numeric sensor.bytes_sent’ then there’s something else wrong, beyond what is visible to me given the information you’ve provided.

Out of curiosity, paste this into the Template Editor and tell me what it reports (or show me a screenshot of the result):

{{ (states('sensor.arris_tg2492lg_85_router_kbyte_sec_sent') | int / 1024) | round(1) }}

just got back to sorting this out and your ‘code’ worked perfectly. the problem was me, I made a typo somehow and on top of that when i was testing it i somehow managed to select the wrong sensor… I changed the round(1) to round(3) and everything is working perfectly now… thank you!!

again the problem was human error on my part… Sorry

1 Like

Hi all, apologises for reviving an old post but I too am having issues here. I have tired all different types of codes.

I have my Entity ID which is pulling through in Bytes.

image

I have added this code but the figures are not converted. Has I missed a step?

  • platform: template
    sensors:
    bytes_sent:
    friendly_name: “MB Sent”
    unit_of_measurement: ‘MB/s’
    value_template: “{ (states(‘sensor.rt_ax58u_29d0_b_sent’) | int / 1000) | round(1) }}”

I’m no expert (still a novice in fact.) But your sensors don’t match here… in your top entity_id you have sensor.rt_ax58u_29d0_b_recieved but in the template you have sensor.rt_ax58u_29d0_b_sent as far as I can see your top one should be sensor.bytes_sent as that’s your new converted sensor

I suspect he has a similar template for both rx and tx.

@wideboys182, please format your pasted configuration correctly. See point 11 here, How to help us help you - or How to ask a good question

@Rdoull apologises I have mixed up my code with the wrong sensor as @tom_l states I have one for bytes sent and bytes received.

@tom_l thank you for the advice, I see copy and paste in to the forum loses its formatting and still very new to all this. The code I have is:

   - platform: template
     sensors:
      bytes_sent:
       friendly_name: "MB Sent"
       unit_of_measurement: 'MB/s'
       value_template: "{ (states('sensor.rt_ax58u_29d0_b_sent') | int / 1000) | round(1) }}"

an image from the configuration.yaml if this is more suitable:

Should be:

value_template: "{{ (

Images of text are not usually much use as it is not possible to copy and paste corrections. Exceptions being: where demonstrating something in the UI, template editor results, or where line numbers are needed.

Thanks. I have added that amendment to my code and rebooted but data is still showing in B:

image

You need to change the Lovelace card to graph your new template sensor, sensor.bytes_sent.

I do not have this sensor available?

Is my understanding correct that with this template it should create a new sensor called sensor.bytes_sent which will use the data from sensor.rt_ax58u_29d_b_sent but in a different format as defined by the template?

What am I missing?

Did you restart after creating the template sensor?

Yep no new sensor has been created. Even rebooted 2 more times for good will and still nothing