Browsing States

The thing will periodically upload its state to the Kii Cloud. You can browse these states from the mobile application. See here for the overview.

You can get the latest state based on the design made in the Schema Definition. For example, you can get the following JSON for our "air conditioner" sample scenario shown in the schema definition page.

{
  "power" : true,
  "presetTemperature" : 25,
  "fanspeed" : 5,
  "currentTemperature" : 28,
  "currentHumidity" : 65
}

The following sample code shows how you can read the latest state.

Prior to the execution, you need to get an ThingIFAPI instance (api) that is initialized by the steps described in Initializing and Onboarding.

  • // Get the thing state.
    api.getState().then((state: any) => {
      // Do something.
      console.log(JSON.stringify(state));
    }).catch((error: ThingIF.ThingIFError) => {
      // Handle the error.
    });
  • // Get the thing state.
    api.getState().then(
      function(state) {
        // Do something.
        console.log(JSON.stringify(state));
      }
    ).catch(
      function(error) {
        // Handle the error.
      }
    );

By executing the getState method, you can get an object which represents the state in JSON from the promise. The above sample code outputs a string which is similar to the JSON string presented earlier in this topic to the console.

Once you get the state, you can access state values by specifying the keys, like state.power.

Note that TypeScript can handle the state type more clearly than JavaScript by defining the data type as below.

// AirConditionerState.ts
export class AirConditionerState {
  power: boolean;
  presetTemperature: number;
  fanspeed: number;
  currentTemperature: number;
  currentHumidity: number;
}
import {AirConditionerState} from './AirConditionerState';

...

// Get the thing state.
api.getState().then((state: AirConditionerState) => {
  // Do something.
  console.log(state.power);
  console.log(state.presetTemperature);
  console.log(state.fanspeed);
  console.log(state.currentTemperature);
  console.log(state.currentHumidity);
}).catch((error: ThingIF.ThingIFError) => {
  // Handle the error.
});

You cannot query or browse state history via Thing-IF SDK for JavaScript. Please use either REST API or developer portal.