LAB02-Research / HASS.Agent

Windows-based client for Home Assistant. Provides notifications, quick actions, commands, sensors and more.
https://hassagent.lab02-research.org
MIT License
1.47k stars 65 forks source link

Misc: Help convert MB to GB #407

Open avenom opened 2 months ago

avenom commented 2 months ago

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 commented 2 months ago
- 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 commented 2 months ago
- 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 commented 2 months ago

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 commented 2 months ago
  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