-
Notifications
You must be signed in to change notification settings - Fork 11
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
convert units to mm in the js scripts. closes #14. #23
Conversation
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups 😅 thanks!
Thanks guys, works great from my testing! |
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 😄