How to use the thingtalk.Units.transformToBaseUnit function in thingtalk

To help you get started, we’ve selected a few thingtalk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            }
            return [{state: undefined, value: value}];
        } else if (this.domain === 'binary_sensor') {
            let state = this.deviceStateMapping[this.state.state];
            if (['gas', 'smoke'].includes(this.device_class)) {
                state = state === 'detecting' ? this.device_class : 'nothing';
            }
            return [{state: state, value: undefined}];
        }
    }
    // note: subscribe_ must NOT be async, or an ImplementationError will occur at runtime
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
let value = parseFloat(this.state.state);
            if (this.device_class === 'temperature') {
                if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            }
            return [{state: undefined, value: value}];
        } else if (this.domain === 'binary_sensor') {
            let state = this.deviceStateMapping[this.state.state];
            if (['gas', 'smoke'].includes(this.device_class)) {
                state = state === 'detecting' ? this.device_class : 'nothing';
            }
            return [{state: state, value: undefined}];
        }
    }
    // note: subscribe_ must NOT be async, or an ImplementationError will occur at runtime
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
subscribe_state() {
        if (this.domain === 'sensor') {
            let unit = this.state.attributes.unit_of_measurement;
            let value = parseFloat(this.state.state);
            if (this.device_class === 'temperature') {
                if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            }
            return this._subscribeState(() => {
                return {state: undefined, value: value};
            });
        } else if (this.domain === 'binary_sensor') {
            let state = this.deviceStateMapping[this.state.state];
            if (['gas', 'smoke'].includes(this.device_class)) {
                state = state === 'detecting' ? this.device_class : 'nothing';
            }
            return this._subscribeState(() => {
                return {state: state, value: undefined};
            });
        }
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
async get_state() {
        if (this.domain === 'sensor') {
            let value = parseFloat(this.state.state);
            if (this.device_class === 'temperature') {
                if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
subscribe_state() {
        if (this.domain === 'sensor') {
            let unit = this.state.attributes.unit_of_measurement;
            let value = parseFloat(this.state.state);
            if (this.device_class === 'temperature') {
                if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            }
            return this._subscribeState(() => {
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
let value = parseFloat(this.state.state);
            if (this.device_class === 'temperature') {
                if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            }
            return this._subscribeState(() => {
                return {state: undefined, value: value};
            });
        } else if (this.domain === 'binary_sensor') {
            let state = this.deviceStateMapping[this.state.state];
            if (['gas', 'smoke'].includes(this.device_class)) {
                state = state === 'detecting' ? this.device_class : 'nothing';
            }
            return this._subscribeState(() => {
                return {state: state, value: undefined};
github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
async get_state() {
        if (this.domain === 'sensor') {
            let value = parseFloat(this.state.state);
            if (this.device_class === 'temperature') {
                if (unit != '°C' && unit != 'C') {
                    if (unit === '°F' || unit === 'F')
                        value = Units.transformToBaseUnit(value, 'F');
                    else if (unit === 'K')
                        value = Units.transformToBaseUnit(value, 'K');
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            } else if (this.device_class === 'pressure') {
                if (unit != 'Pa') {
                    if (unit === 'hPa' || unit === 'hpa')
                        value *= 100;
                    else if (unit === 'mbar')
                        value = Units.transformToBaseUnit(value * 0.001, 'bar');
                    else if (['bar', 'psi', 'mmHg', 'inHg', 'atm'].includes(unit))
                        value = Units.transformToBaseUnit(value * 0.001, unit);
                    else
                        throw new Error(`Unrecognized unit ${unit}`)
                }
            }
            return [{state: undefined, value: value}];