Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc: Help convert MB to GB #407

Open
avenom opened this issue Apr 11, 2024 · 4 comments
Open

Misc: Help convert MB to GB #407

avenom opened this issue Apr 11, 2024 · 4 comments

Comments

@avenom
Copy link

avenom commented Apr 11, 2024

Please help me convert MB to GB in this code:

- platform: template
  sensors:
    storage_capacity_d:
      friendly_name: "Storage Capacity Drive D"
      unit_of_measurement: "MB"
      value_template: "{{ state_attr('sensor.avenompc_avenompc_storage_d', 'AvailableSpaceMB')}}"

Result: 181 953,0 MB

@ruifung
Copy link

ruifung commented Apr 11, 2024

- platform: template
  sensors:
    storage_capacity_d:
      friendly_name: "Storage Capacity Drive D"
      unit_of_measurement: "GB"
      value_template: "{{ state_attr('sensor.avenompc_avenompc_storage_d', 'AvailableSpaceMB') | float / 1000}}"

In case you're not aware, you can do math in HA templates.
Note: I made the changes off the top of my head, if it don't work, there's probably a typo somewhere.

Also note, since your example is in MB (not MiB), you divide by 1000 to get GB.
If the source data is actually in binary units (i.e. MiB), divide by 1024 to get GiB instead.

@avenom
Copy link
Author

avenom commented Apr 11, 2024

- platform: template
  sensors:
    storage_capacity_d:
      friendly_name: "Storage Capacity Drive D"
      unit_of_measurement: "GB"
      value_template: "{{ state_attr('sensor.avenompc_avenompc_storage_d', 'AvailableSpaceMB') | float / 1000}}"

In case you're not aware, you can do math in HA templates. Note: I made the changes off the top of my head, if it don't work, there's probably a typo somewhere.

Also note, since your example is in MB (not MiB), you divide by 1000 to get GB. If the source data is actually in binary units (i.e. MiB), divide by 1024 to get GiB instead.

Result: 181,953 GB

@ruifung
Copy link

ruifung commented Apr 11, 2024

maybe you need parentheses for that. Try plugging the template into the HA devtools template tester and play around with it until you get the output you want.

@avenom
Copy link
Author

avenom commented Apr 13, 2024

  1. I generated a UUID for the sensor (https://www.uuidgenerator.net/version4) and can now round up in the sensor properties in HA
  2. Converted bytes to the binary system of gibibytes (GiB).

Not perfect, but it works.

Sample:

- platform: template
  sensors:
    storage_capacity_d:
      friendly_name: "Storage Capacity Drive D"
      unique_id: 30a103c2-3147-4de3-be98-3a9a80ed7fc3
      unit_of_measurement: "GB"
      value_template: "{{ state_attr('sensor.avenompc_avenompc_storage_d', 'AvailableSpaceMB') | float / 1073.741824 }}"

storage-mebibyte_measurements

2024-04-13 17-30-10 Home Assistant

2024-04-13 17-28-49 Home Assistant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants