Skip to content

Instantly share code, notes, and snippets.

@stas-dovgodko
Created March 2, 2026 12:56
Show Gist options
  • Select an option

  • Save stas-dovgodko/5d691fc030db254e98abfa15ed46ffa4 to your computer and use it in GitHub Desktop.

Select an option

Save stas-dovgodko/5d691fc030db254e98abfa15ed46ffa4 to your computer and use it in GitHub Desktop.
Tuya local EV algo
// openHAB JS (GraalJS): Base64 -> byte[] через Java, далі парсимо uint16 BE
const Base64 = Java.type("java.util.Base64");
function u16be(bytes, offset) {
// bytes: Java byte[] (signed), тому маскуємо до 0..255
const hi = bytes[offset] & 0xFF;
const lo = bytes[offset + 1] & 0xFF;
return (hi << 8) | lo;
}
/**
* Payload format (8 bytes, BE):
* [0..1] reg1 = voltage * 10
* [6..7] reg4 = power (W)
*/
function parseEvPhase(b64) {
const bytes = Base64.getDecoder().decode(b64); // byte[] length must be 8
if (bytes.length !== 8) throw new Error("Expected 8 bytes, got " + bytes.length);
const reg1 = u16be(bytes, 0);
const reg4 = u16be(bytes, 6);
return {
voltageV: reg1 / 10.0,
powerW: reg4,
// якщо треба — сирі значення:
reg2: u16be(bytes, 2),
reg3: u16be(bytes, 4)
};
}
// Example:
console.log(parseEvPhase("CIQAVVwAEqY=")); // { voltageV: 218, powerW: 4774, ... }
console.log(parseEvPhase("COgAAAAAAAA=")); // { voltageV: 228, powerW: 0, ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment