> For the complete documentation index, see [llms.txt](https://docs.mclimate.eu/mclimate-lorawan-devices/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mclimate.eu/mclimate-lorawan-devices/devices/mclimate-fan-coil-thermostat-fct/decoding-and-bacnet-object-mapping/mclimate-fan-coil-thermostat-downlink-encoder.md).

# MClimate Fan Coil Thermostat Downlink encoder

## Universal Encoder:

*Supports: The Thinks Network, Milesight*

```
// Milesight
function Encode(port, obj) {
  var encoded = encodeDownlink({ fPort: port, data: obj }).bytes;
  return encoded;
}

function Encoder(port, obj) {
  var encoded = encodeDownlink({ fPort: port, data: obj }).bytes;
  return encoded;
}

// The Things Industries / Main
function encodeDownlink(input) {
  var bytes = [];

  // Some older versions of this file used PascalCase keys (e.g. "SetFanSpeed").
  // Both spellings are accepted: the value is always read from input.data[key].
  for (var key in input.data) {
    var value = input.data[key];

    switch (key) {
      /* ---------------- general commands ---------------- */
      case "SetKeepAlive":
      case "setKeepAlive": {
        bytes.push(0x02);
        bytes.push(value);
        break;
      }
      case "GetKeepAliveTime":
      case "getKeepAliveTime": {
        bytes.push(0x12);
        break;
      }
      case "GetDeviceVersions":
      case "getDeviceVersions": {
        bytes.push(0x04);
        break;
      }
      case "SetJoinRetryPeriod":
      case "setJoinRetryPeriod": {
        // period is given in minutes, transmitted in 5 s steps
        bytes.push(0x10);
        bytes.push(Math.round((value * 60) / 5));
        break;
      }
      case "GetJoinRetryPeriod":
      case "getJoinRetryPeriod": {
        bytes.push(0x19);
        break;
      }
      case "SetUplinkType":
      case "setUplinkType": {
        // 00: unconfirmed, 01: confirmed
        bytes.push(0x11);
        bytes.push(value);
        break;
      }
      case "GetUplinkType":
      case "getUplinkType": {
        bytes.push(0x1b);
        break;
      }
      case "SetWatchDogParams":
      case "setWatchDogParams": {
        bytes.push(0x1c);
        bytes.push(value.confirmedUplinks);
        bytes.push(value.unconfirmedUplinks);
        break;
      }
      case "GetWatchDogParams":
      case "getWatchDogParams": {
        bytes.push(0x1d);
        break;
      }
      case "GetRegion":
      case "getRegion": {
        bytes.push(0xa4);
        break;
      }
      case "RestartDevice":
      case "restartDevice": {
        bytes.push(0xa5);
        break;
      }

      /* ---------------- target temperature ---------------- */
      case "SetTargetTemperature":
      case "setTargetTemperature": {
        // 2 bytes, 0.1 C resolution
        var temp = Math.round(value * 10);
        bytes.push(0x2e);
        bytes.push((temp >> 8) & 0xff);
        bytes.push(temp & 0xff);
        break;
      }
      case "GetTargetTemperature":
      case "getTargetTemperature": {
        bytes.push(0x2f);
        break;
      }
      case "SetTargetTemperatureStep":
      case "setTargetTemperatureStep": {
        // 0.1 C resolution
        bytes.push(0x03);
        bytes.push(Math.round(value * 10));
        break;
      }
      case "GetTargetTemperatureStep":
      case "getTargetTemperatureStep": {
        bytes.push(0x05);
        break;
      }
      case "SetTemperatureRange":
      case "setTemperatureRange": {
        // FW <= 1.5 and >= 1.8
        bytes.push(0x08);
        bytes.push(value.min);
        bytes.push(value.max);
        break;
      }
      case "GetTemperatureRange":
      case "getTemperatureRange": {
        bytes.push(0x15);
        break;
      }
      case "SetHeatingCoolingTargetTempRanges":
      case "setHeatingCoolingTargetTempRanges": {
        // FW >= 1.6
        bytes.push(0x16);
        bytes.push(value.heatingTempMin);
        bytes.push(value.heatingTempMax);
        bytes.push(value.coolingTempMin);
        bytes.push(value.coolingTempMax);
        break;
      }
      case "GetHeatingCoolingTargetTempRanges":
      case "getHeatingCoolingTargetTempRanges": {
        bytes.push(0x17);
        break;
      }
      case "SetHeatingCoolingTargetTempRangesUnoccupied":
      case "setHeatingCoolingTargetTempRangesUnoccupied": {
        // FW >= 1.6
        bytes.push(0x76);
        bytes.push(value.heatingTempMin);
        bytes.push(value.heatingTempMax);
        bytes.push(value.coolingTempMin);
        bytes.push(value.coolingTempMax);
        break;
      }
      case "GetHeatingCoolingTargetTempRangesUnoccupied":
      case "getHeatingCoolingTargetTempRangesUnoccupied": {
        bytes.push(0x77);
        break;
      }

      /* ---------------- keys lock, device status, power ---------------- */
      case "SetKeysLock":
      case "setKeysLock": {
        // 0: none, 1: all, 2: on/off + mode, 3: on/off, 4: all except on/off, 5: mode
        bytes.push(0x07);
        bytes.push(value);
        break;
      }
      case "GetKeysLock":
      case "getKeysLock": {
        bytes.push(0x14);
        break;
      }
      case "SetDeviceStatus":
      case "setDeviceStatus": {
        // 00: off, 01: on
        bytes.push(0x66);
        bytes.push(value);
        break;
      }
      case "GetDeviceStatus":
      case "getDeviceStatus": {
        bytes.push(0x67);
        break;
      }
      case "SetReturnOfPowerOperation":
      case "setReturnOfPowerOperation": {
        // 00: last status, 01: on, 02: off
        bytes.push(0x68);
        bytes.push(value);
        break;
      }
      case "GetReturnOfPowerOperation":
      case "getReturnOfPowerOperation": {
        bytes.push(0x69);
        break;
      }

      /* ---------------- valve & operational modes ---------------- */
      case "SetValveOpenCloseTime":
      case "setValveOpenCloseTime": {
        bytes.push(0x31);
        bytes.push(value);
        break;
      }
      case "GetValveOpenCloseTime":
      case "getValveOpenCloseTime": {
        bytes.push(0x32);
        break;
      }
      case "SetFctOperationalMode":
      case "setFctOperationalMode": {
        // 00: ventilation, 01: heating, 02: cooling
        bytes.push(0x52);
        bytes.push(value);
        break;
      }
      case "GetFctOperationalMode":
      case "getFctOperationalMode": {
        bytes.push(0x53);
        break;
      }
      case "SetAllowedOperationalModes":
      case "setAllowedOperationalModes": {
        bytes.push(0x54);
        bytes.push(value);
        break;
      }
      case "GetAllowedOperationalModes":
      case "getAllowedOperationalModes": {
        bytes.push(0x55);
        break;
      }
      case "SetWiringDiagram":
      case "setWiringDiagram": {
        bytes.push(0x60);
        bytes.push(value);
        break;
      }
      case "GetWiringDiagram":
      case "getWiringDiagram": {
        bytes.push(0x61);
        break;
      }

      /* ---------------- display ---------------- */
      case "SetDisplayRefreshPeriod":
      case "setDisplayRefreshPeriod": {
        bytes.push(0x33);
        bytes.push(value);
        break;
      }
      case "GetDisplayRefreshPeriod":
      case "getDisplayRefreshPeriod": {
        bytes.push(0x34);
        break;
      }
      case "SetCurrentTemperatureVisibility":
      case "setCurrentTemperatureVisibility": {
        bytes.push(0x40);
        bytes.push(value);
        break;
      }
      case "GetCurrentTemperatureVisibility":
      case "getCurrentTemperatureVisibility": {
        bytes.push(0x41);
        break;
      }
      case "SetHumidityVisibility":
      case "setHumidityVisibility": {
        bytes.push(0x42);
        bytes.push(value);
        break;
      }
      case "GetHumidityVisibility":
      case "getHumidityVisibility": {
        bytes.push(0x43);
        break;
      }
      case "SetDisplayColorMode":
      case "setDisplayColorMode": {
        // FW >= 2.5. 00: light (default), 01: dark
        bytes.push(0x7e);
        bytes.push(value);
        break;
      }
      case "GetDisplayColorMode":
      case "getDisplayColorMode": {
        bytes.push(0x7f);
        break;
      }
      case "SetUserInterfaceLanguage":
      case "setUserInterfaceLanguage": {
        // FW >= 1.8. 00: English, 01: French, 02: German, 03: Spanish
        bytes.push(0x9a);
        bytes.push(value);
        break;
      }
      case "GetUserInterfaceLanguage":
      case "getUserInterfaceLanguage": {
        bytes.push(0x9b);
        break;
      }

      /* ---------------- external temperature ---------------- */
      case "SetExtAutomaticTemperatureControl":
      case "setExtAutomaticTemperatureControl": {
        bytes.push(0x35);
        bytes.push(value);
        break;
      }
      case "GetExtAutomaticTemperatureControl":
      case "getExtAutomaticTemperatureControl": {
        bytes.push(0x36);
        break;
      }
      case "SetExtTemperatureSensor1C":
      case "setExtTemperatureSensor1C": {
        // 1 C resolution
        bytes.push(0x3b);
        bytes.push(value);
        break;
      }
      case "SetExtTemperatureSensor":
      case "setExtTemperatureSensor": {
        // 2 bytes, 0.1 C resolution
        var extTemp = Math.round(value * 10);
        bytes.push(0x3c);
        bytes.push((extTemp >> 8) & 0xff);
        bytes.push(extTemp & 0xff);
        break;
      }
      case "GetExtTemperatureSensor":
      case "getExtTemperatureSensor": {
        bytes.push(0x3e);
        break;
      }
      case "SetTempSensorCompensation":
      case "setTempSensorCompensation": {
        // compensation 00: positive, 01: negative; temperature at 0.1 C resolution
        bytes.push(0x5a);
        bytes.push(value.compensation === 0 ? 0 : 1);
        bytes.push(Math.abs(Math.round(value.temperature * 10)));
        break;
      }
      case "GetTempSensorCompensation":
      case "getTempSensorCompensation": {
        bytes.push(0x5b);
        break;
      }

      /* ---------------- fan ---------------- */
      case "SetFanSpeed":
      case "setFanSpeed": {
        bytes.push(0x44);
        bytes.push(value);
        break;
      }
      case "GetFanSpeed":
      case "getFanSpeed": {
        bytes.push(0x45);
        break;
      }
      case "SetFanSpeedLimit":
      case "setFanSpeedLimit": {
        bytes.push(0x46);
        bytes.push(value);
        break;
      }
      case "GetFanSpeedLimit":
      case "getFanSpeedLimit": {
        bytes.push(0x47);
        break;
      }
      case "SetEcmVoltageRange":
      case "setEcmVoltageRange": {
        // 0.1 V resolution
        bytes.push(0x48);
        bytes.push(Math.round(value.min * 10));
        bytes.push(Math.round(value.max * 10));
        break;
      }
      case "GetEcmVoltageRange":
      case "getEcmVoltageRange": {
        bytes.push(0x49);
        break;
      }
      case "SetEcmStartUpTime":
      case "setEcmStartUpTime": {
        bytes.push(0x4a);
        bytes.push(value);
        break;
      }
      case "GetEcmStartUpTime":
      case "getEcmStartUpTime": {
        bytes.push(0x4b);
        break;
      }
      case "SetEcmRelay":
      case "setEcmRelay": {
        bytes.push(0x4c);
        bytes.push(value);
        break;
      }
      case "GetEcmRelay":
      case "getEcmRelay": {
        bytes.push(0x4d);
        break;
      }
      case "SetFanSpeedNotOccupied":
      case "setFanSpeedNotOccupied": {
        bytes.push(0x5c);
        bytes.push(value);
        break;
      }
      case "GetFanSpeedNotOccupied":
      case "getFanSpeedNotOccupied": {
        bytes.push(0x5d);
        break;
      }
      case "SetFanOffDelayTime":
      case "setFanOffDelayTime": {
        // FW >= 1.6
        bytes.push(0x78);
        bytes.push(value);
        break;
      }
      case "GetFanOffDelayTime":
      case "getFanOffDelayTime": {
        bytes.push(0x79);
        break;
      }
      case "SetAdditionalFanMode":
      case "setAdditionalFanMode": {
        // FW >= 1.6
        bytes.push(0x7a);
        bytes.push(value);
        break;
      }
      case "GetAdditionalFanMode":
      case "getAdditionalFanMode": {
        bytes.push(0x7b);
        break;
      }

      /* ---------------- frost protection ---------------- */
      case "SetFrostProtection":
      case "setFrostProtection": {
        bytes.push(0x4e);
        bytes.push(value);
        break;
      }
      case "GetFrostProtection":
      case "getFrostProtection": {
        bytes.push(0x4f);
        break;
      }
      case "SetFrostProtectionSettings":
      case "setFrostProtectionSettings": {
        bytes.push(0x50);
        bytes.push(value.threshold);
        bytes.push(value.setpoint);
        break;
      }
      case "GetFrostProtectionSettings":
      case "getFrostProtectionSettings": {
        bytes.push(0x51);
        break;
      }
      case "GetFrostProtectionStatus":
      case "getFrostProtectionStatus": {
        bytes.push(0x6e);
        break;
      }

      /* ---------------- unoccupied set-points ---------------- */
      case "SetCoolingSetpointNotOccupied":
      case "setCoolingSetpointNotOccupied": {
        bytes.push(0x56);
        bytes.push(value);
        break;
      }
      case "GetCoolingSetpointNotOccupied":
      case "getCoolingSetpointNotOccupied": {
        bytes.push(0x57);
        break;
      }
      case "SetHeatingSetpointNotOccupied":
      case "setHeatingSetpointNotOccupied": {
        bytes.push(0x58);
        bytes.push(value);
        break;
      }
      case "GetHeatingSetpointNotOccupied":
      case "getHeatingSetpointNotOccupied": {
        bytes.push(0x59);
        break;
      }

      /* ---------------- automatic changeover ---------------- */
      case "SetAutomaticChangeover":
      case "setAutomaticChangeover": {
        bytes.push(0x5e);
        bytes.push(value);
        break;
      }
      case "GetAutomaticChangeover":
      case "getAutomaticChangeover": {
        bytes.push(0x5f);
        break;
      }
      case "SetAutomaticChangeoverThreshold":
      case "setAutomaticChangeoverThreshold": {
        bytes.push(0x64);
        bytes.push(value.coolingThreshold);
        bytes.push(value.heatingThreshold);
        break;
      }
      case "GetAutomaticChangeoverThreshold":
      case "getAutomaticChangeoverThreshold": {
        bytes.push(0x65);
        break;
      }
      case "GetModeChangedByAutoChangeover":
      case "getModeChangedByAutoChangeover": {
        bytes.push(0x74);
        break;
      }

      /* ---------------- digital input/output (OCC) & sensors ---------------- */
      case "SetOccFunction":
      case "setOccFunction": {
        bytes.push(0x62);
        bytes.push(value);
        break;
      }
      case "GetOccFunction":
      case "getOccFunction": {
        bytes.push(0x63);
        break;
      }
      case "GetOccupancySensorStatusSetPoint":
      case "getOccupancySensorStatusSetPoint": {
        bytes.push(0x70);
        break;
      }
      case "GetOccupancySensorStatus":
      case "getOccupancySensorStatus": {
        bytes.push(0x71);
        break;
      }
      case "GetDewPointSensorStatus":
      case "getDewPointSensorStatus": {
        bytes.push(0x72);
        break;
      }
      case "GetFilterAlarm":
      case "getFilterAlarm": {
        bytes.push(0x73);
        break;
      }
      case "GetPowerModuleCommunicationStatus":
      case "getPowerModuleCommunicationStatus": {
        bytes.push(0x75);
        break;
      }
      case "GetInternalTemperatureSensorError":
      case "getInternalTemperatureSensorError": {
        // FW >= 1.9
        bytes.push(0x7c);
        break;
      }
      case "GetExternalTemperatureSensorError":
      case "getExternalTemperatureSensorError": {
        // FW >= 1.9
        bytes.push(0x7d);
        break;
      }

      /* ---------------- delta temperatures ---------------- */
      case "SetDeltaTemperature1":
      case "setDeltaTemperature1": {
        // 0.1 C resolution
        bytes.push(0x6a);
        bytes.push(Math.round(value * 10));
        break;
      }
      case "GetDeltaTemperature1":
      case "getDeltaTemperature1": {
        bytes.push(0x6b);
        break;
      }
      case "SetDeltaTemperature2and3":
      case "setDeltaTemperature2and3": {
        // 0.1 C resolution
        bytes.push(0x6c);
        bytes.push(Math.round(value.deltaTemperature2 * 10));
        bytes.push(Math.round(value.deltaTemperature3 * 10));
        break;
      }
      case "GetDeltaTemperature2and3":
      case "getDeltaTemperature2and3": {
        bytes.push(0x6d);
        break;
      }

      /* ---------------- raw passthrough ---------------- */
      case "SendCustomHexCommand":
      case "sendCustomHexCommand": {
        for (var i = 0; i < value.length; i += 2) {
          bytes.push(parseInt(value.substr(i, 2), 16));
        }
        break;
      }

      default: {
      }
    }
  }

  return {
    bytes: bytes,
    fPort: 1,
    warnings: [],
    errors: [],
  };
}

function decodeDownlink(input) {
  return {
    data: {
      bytes: input.bytes,
    },
    warnings: [],
    errors: [],
  };
}

// example downlink commands
//  {"setKeepAlive":10} --> 0x020A
//  {"setTargetTemperature":25.8} --> 0x2E0102 (2 bytes, 0.1 C resolution)
//  {"getTargetTemperature":true} --> 0x2F
//  {"setTemperatureRange":{"min":15,"max":21}} --> 0x080F15
//  {"setHeatingCoolingTargetTempRanges":{"heatingTempMin":11,"heatingTempMax":21,"coolingTempMin":16,"coolingTempMax":22}} --> 0x160B151016
//  {"setFctOperationalMode":2} --> 0x5202 (0 vent, 1 heat, 2 cool)
//  {"setFanSpeed":2} --> 0x4402
//  {"setFrostProtection":1} --> 0x4E01
//  {"setDeviceStatus":1} --> 0x6601
//  {"getDeviceStatus":true} --> 0x67
//  {"setDisplayColorMode":1} --> 0x7E01 (FW >= 2.5; 0 light, 1 dark)
//  {"getDisplayColorMode":true} --> 0x7F
//  {"restartDevice":true} --> 0xA5
//  {"sendCustomHexCommand":"080F15"} --> 0x080F15
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mclimate.eu/mclimate-lorawan-devices/devices/mclimate-fan-coil-thermostat-fct/decoding-and-bacnet-object-mapping/mclimate-fan-coil-thermostat-downlink-encoder.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
