Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Weather API Condition returns 33

ANSWERED

Using the Weather API for SDK 4.3 and using the default code provided in the documentation

here, cond gives 33 instead of SunnyDay for example. And this worked perfectly before. So I highly doubt that my implementation is wrong.

 

import { me as companion } from "companion";
import weather from "weather";

if (companion.permissions.granted("access_location")) {
   weather
     .getWeatherData()
     .then((data) => {
       if (data.locations.length > 0) {
         const temp = Math.floor(data.locations[0].currentWeather.temperature);
         const cond = data.locations[0].currentWeather.weatherCondition;
         const loc = data.locations[0].name;
         const unit = data.temperatureUnit;
         console.log(`It's ${temp}\u00B0 ${unit} and ${cond} in ${loc}`);
       }
     })
     .catch((ex) => {
       console.error(ex);
     });
}

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Sorry for bringing this old topic up,

But i know how to "link" the numbers to the old "Condition Names"

just do something like this:

 

import {weather,WeatherCondition} from "weather";
function findWeatherConditionName(WeatherCondition, conditionCode) {
  for (const condition of Object.keys(WeatherCondition)) {
    if (conditionCode === WeatherCondition[condition]) return condition;
  }
}

 

And then call it like this:

 

if (companion.permissions.granted("access_location")) {
   weather.getWeatherData().then((data) => {
       if (data.locations.length > 0) {
const cond = findWeatherConditionName(WeatherCondition,data.locations[0].currentWeather.weatherCondition);

 

I hope it solves your problem

Phone: OnePlus Nord Android 11, Watch: Versa 3

View best answer in original post

Best Answer
0 Votes
7 REPLIES 7

Hello

did you find somewhere the link between enum and numbers?

Best Answer
0 Votes

Nope, the numbers don't match up with anything as far as I know

Best Answer
0 Votes

Bump, as I've tried the example in the API and it has given me numbers. Is this now substituing the strings?

 

 

Best Answer
0 Votes
Best Answer

Sorry for bringing this old topic up,

But i know how to "link" the numbers to the old "Condition Names"

just do something like this:

 

import {weather,WeatherCondition} from "weather";
function findWeatherConditionName(WeatherCondition, conditionCode) {
  for (const condition of Object.keys(WeatherCondition)) {
    if (conditionCode === WeatherCondition[condition]) return condition;
  }
}

 

And then call it like this:

 

if (companion.permissions.granted("access_location")) {
   weather.getWeatherData().then((data) => {
       if (data.locations.length > 0) {
const cond = findWeatherConditionName(WeatherCondition,data.locations[0].currentWeather.weatherCondition);

 

I hope it solves your problem

Phone: OnePlus Nord Android 11, Watch: Versa 3
Best Answer
0 Votes

Is there any reason why this works on Fitbit Simulator but not on device?

 

When I run this code on device it returns “undefined” and the WeatherCondition itself returns function (){ [native code] }. 

When I run in the simulator it returns a weather condition text and the WeatherCondition itself returns [object Object]. 

As an interim I am using the returned WeatherCondition texts from the simulator and manually creating an array to look up to on device. 

Best Answer

Plod-Along, I am having this same issue. It works fine in the simulator, but when I run it on the physical device (in my case a Sense 2), it always comes back as undefined. The for loop in Aaroneisele55's example never gets entered. Have you found another solution other than creating your own look up array? I'm worried about that being the solution as it seems like it might be brittle in the event of future API changes. 

Best Answer
0 Votes