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

convert units to mm in the js scripts. closes #14. #23

Merged
merged 2 commits into from
Dec 8, 2024

Conversation

akarras
Copy link
Contributor

@akarras akarras commented Dec 1, 2024

I haven't figured out how to test this- I'm fairly confident this will work, but haven't been able to verify. 😓

didn't notice that zed formatted the code until I went to open the PR. I can revert if desired, but I will be lazy and not fix it unless you ask 😄

Copy link
Contributor

@MenesesPT MenesesPT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this and seems to work for the most part. However on the conversion sometimes values need to be truncated (i.e. 12 inches would result in 304.79999999999995 mm). As this values are shown on the table it gets really hard to read since the width of the cells keep changing.
I've suggested a potential fix.

}

/// Returns the entity's state converted from whatever unit is configured in the UI converted to millimeters
function getEntityStateMM(entity) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to round the values that this function returns. I've adapted the function below:

/// Returns the entity's state converted from whatever unit is configured in the UI converted to millimeters
function getEntityStateMM(entity) {
  const state = entity ? parseFloat(entity.state) || 0 : 0;
  let result = state;
  // cm, in, ft, km, m, mi, nmi, yd and mm are supported in home assistant.
  switch (entity.attributes.unit_of_measurement) {
    case 'mm':
      break; // Avoid checking every unit for the most common case
    case "in":
      result = state * 25.4; // Convert inches to millimeters
      break;
    case "ft":
      result = state * 304.8; // Convert feet to millimeters
      break;
    case "km":
      result = state * 1000000; // Convert kilometers to millimeters
      break;
    case "in":
      result = state * 1000; // Convert meters to millimeters
      break;
    case "in":
      result = state * 1.609e6; // Convert miles to millimeters
      break;
    case "in":
      result = state * 1.852e6; // Convert nautical miles to millimeters
      break;
    case "in":
      result = state * 914.4; // Convert yards to millimeters
      break;
  }
  return Math.round(result);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for testing!
I Integrated your suggestion- I did fixup the last couple cases for miles/nautical miles/yards that were still case 'in' . This looks like it's working well on my end though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups 😅 thanks!

@EverythingSmartHome EverythingSmartHome merged commit ba3c0a2 into EverythingSmartHome:main Dec 8, 2024
1 check failed
@EverythingSmartHome
Copy link
Owner

Thanks guys, works great from my testing!

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

Successfully merging this pull request may close these issues.

3 participants