2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.openweathermap.internal.handler;
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
16 import static org.openhab.core.library.unit.MetricPrefix.*;
17 import static org.openhab.core.library.unit.SIUnits.*;
18 import static org.openhab.core.library.unit.Units.*;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapOneCallConfiguration;
28 import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
29 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapOneCallAPIData;
30 import org.openhab.binding.openweathermap.internal.dto.forecast.daily.FeelsLikeTemp;
31 import org.openhab.binding.openweathermap.internal.dto.forecast.daily.Temp;
32 import org.openhab.binding.openweathermap.internal.dto.onecall.Alert;
33 import org.openhab.binding.openweathermap.internal.dto.onecall.Precipitation;
34 import org.openhab.core.i18n.CommunicationException;
35 import org.openhab.core.i18n.ConfigurationException;
36 import org.openhab.core.i18n.TimeZoneProvider;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.thing.Channel;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingStatus;
42 import org.openhab.core.thing.ThingStatusDetail;
43 import org.openhab.core.thing.binding.builder.ThingBuilder;
44 import org.openhab.core.types.State;
45 import org.openhab.core.types.UnDefType;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import com.google.gson.JsonSyntaxException;
52 * The {@link OpenWeatherMapOneCallHandler} is responsible for handling commands, which are sent to one of
55 * @author Wolfgang Klimt - Initial contribution
56 * @author Christoph Weitkamp - Added weather alerts
59 public class OpenWeatherMapOneCallHandler extends AbstractOpenWeatherMapHandler {
61 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapOneCallHandler.class);
63 private static final String CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX = "forecastMinutes";
64 private static final String CHANNEL_GROUP_HOURLY_FORECAST_PREFIX = "forecastHours";
65 private static final String CHANNEL_GROUP_DAILY_FORECAST_PREFIX = "forecastDay";
66 private static final String CHANNEL_GROUP_ALERTS_PREFIX = "alerts";
67 private static final Pattern CHANNEL_GROUP_HOURLY_FORECAST_PREFIX_PATTERN = Pattern
68 .compile(CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + "([0-9]*)");
69 private static final Pattern CHANNEL_GROUP_DAILY_FORECAST_PREFIX_PATTERN = Pattern
70 .compile(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + "([0-9]*)");
71 private static final Pattern CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX_PATTERN = Pattern
72 .compile(CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + "([0-9]*)");
73 private static final Pattern CHANNEL_GROUP_ALERTS_PREFIX_PATTERN = Pattern
74 .compile(CHANNEL_GROUP_ALERTS_PREFIX + "([0-9]*)");
76 private @Nullable OpenWeatherMapOneCallAPIData weatherData;
78 private int forecastMinutes = 60;
79 private int forecastHours = 24;
80 private int forecastDays = 8;
81 private int numberOfAlerts = 0;
83 public OpenWeatherMapOneCallHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
84 super(thing, timeZoneProvider);
88 public void initialize() {
90 logger.debug("Initialize OpenWeatherMapOneCallHandler handler '{}'.", getThing().getUID());
91 OpenWeatherMapOneCallConfiguration config = getConfigAs(OpenWeatherMapOneCallConfiguration.class);
93 boolean configValid = true;
94 int newForecastMinutes = config.forecastMinutes;
95 if (newForecastMinutes < 0 || newForecastMinutes > 60) {
96 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
97 "@text/offline.conf-error-not-supported-onecall-number-of-minutes");
100 int newForecastHours = config.forecastHours;
101 if (newForecastHours < 0 || newForecastHours > 48) {
102 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
103 "@text/offline.conf-error-not-supported-onecall-number-of-hours");
106 int newForecastDays = config.forecastDays;
107 if (newForecastDays < 0 || newForecastDays > 8) {
108 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
109 "@text/offline.conf-error-not-supported-onecall-number-of-days");
112 int newNumberOfAlerts = config.numberOfAlerts;
113 if (newNumberOfAlerts < 0) {
114 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
115 "@text/offline.conf-error-not-supported-onecall-number-of-alerts");
120 logger.debug("Rebuilding thing '{}'.", getThing().getUID());
121 List<Channel> toBeAddedChannels = new ArrayList<>();
122 List<Channel> toBeRemovedChannels = new ArrayList<>();
124 .addAll(createChannelsForGroup(CHANNEL_GROUP_ONECALL_CURRENT, CHANNEL_GROUP_TYPE_ONECALL_CURRENT));
125 if (forecastMinutes != newForecastMinutes) {
126 logger.debug("forecastMinutes changed from {} to {}. Rebuilding minutely forecast channel groups.",
127 forecastMinutes, newForecastMinutes);
128 if (forecastMinutes > newForecastMinutes) {
129 for (int i = newForecastMinutes + 1; i <= forecastMinutes; i++) {
130 toBeRemovedChannels.addAll(removeChannelsOfGroup(
131 CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
134 for (int i = forecastMinutes + 1; i <= newForecastMinutes; i++) {
135 toBeAddedChannels.addAll(createChannelsForGroup(
136 CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i),
137 CHANNEL_GROUP_TYPE_ONECALL_MINUTELY_FORECAST));
140 forecastMinutes = newForecastMinutes;
142 if (forecastHours != newForecastHours) {
143 logger.debug("ForecastHours changed from {} to {}. Rebuilding hourly forecast channel groups.",
144 forecastHours, newForecastHours);
145 if (forecastHours > newForecastHours) {
146 for (int i = newForecastHours + 1; i <= forecastHours; i++) {
147 toBeRemovedChannels.addAll(removeChannelsOfGroup(
148 CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
151 for (int i = forecastHours + 1; i <= newForecastHours; i++) {
152 toBeAddedChannels.addAll(createChannelsForGroup(
153 CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i),
154 CHANNEL_GROUP_TYPE_ONECALL_HOURLY_FORECAST));
157 forecastHours = newForecastHours;
159 if (forecastDays != newForecastDays) {
160 logger.debug("ForecastDays changed from {} to {}. Rebuilding daily forecast channel groups.",
161 forecastDays, newForecastDays);
162 if (forecastDays > newForecastDays) {
163 if (newForecastDays < 1) {
164 toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TODAY));
166 if (newForecastDays < 2) {
167 toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TOMORROW));
169 for (int i = newForecastDays; i < forecastDays; ++i) {
170 toBeRemovedChannels.addAll(
171 removeChannelsOfGroup(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + Integer.toString(i)));
174 if (forecastDays == 0 && newForecastDays > 0) {
175 toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TODAY,
176 CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
178 if (forecastDays <= 1 && newForecastDays > 1) {
179 toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TOMORROW,
180 CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
182 for (int i = Math.max(forecastDays, 2); i < newForecastDays; ++i) {
183 toBeAddedChannels.addAll(
184 createChannelsForGroup(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + Integer.toString(i),
185 CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
188 forecastDays = newForecastDays;
189 if (numberOfAlerts != newNumberOfAlerts) {
190 logger.debug("Rebuilding alerts channel groups.");
191 if (numberOfAlerts > newNumberOfAlerts) {
192 for (int i = newNumberOfAlerts + 1; i <= numberOfAlerts; ++i) {
194 .addAll(removeChannelsOfGroup(CHANNEL_GROUP_ALERTS_PREFIX + Integer.toString(i)));
197 for (int i = numberOfAlerts + 1; i <= newNumberOfAlerts; ++i) {
199 .addAll(createChannelsForGroup(CHANNEL_GROUP_ALERTS_PREFIX + Integer.toString(i),
200 CHANNEL_GROUP_TYPE_ONECALL_ALERTS));
203 numberOfAlerts = newNumberOfAlerts;
206 logger.debug("toBeRemovedChannels: {}. toBeAddedChannels: {}", toBeRemovedChannels, toBeAddedChannels);
207 ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
208 for (Channel channel : toBeAddedChannels) {
209 builder.withChannel(channel);
211 updateThing(builder.build());
216 protected boolean requestData(OpenWeatherMapConnection connection)
217 throws CommunicationException, ConfigurationException {
218 logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID());
220 weatherData = connection.getOneCallAPIData(location, forecastMinutes == 0, forecastHours == 0,
221 forecastDays == 0, numberOfAlerts == 0);
223 } catch (JsonSyntaxException e) {
224 logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
230 protected void updateChannel(ChannelUID channelUID) {
231 String channelGroupId = channelUID.getGroupId();
232 logger.debug("OneCallHandler: updateChannel {}, groupID {}", channelUID, channelGroupId);
233 switch (channelGroupId) {
234 case CHANNEL_GROUP_ONECALL_CURRENT:
235 updateCurrentChannel(channelUID);
237 case CHANNEL_GROUP_ONECALL_TODAY:
238 updateDailyForecastChannel(channelUID, 0);
240 case CHANNEL_GROUP_ONECALL_TOMORROW:
241 updateDailyForecastChannel(channelUID, 1);
245 Matcher hourlyForecastMatcher = CHANNEL_GROUP_HOURLY_FORECAST_PREFIX_PATTERN.matcher(channelGroupId);
246 if (hourlyForecastMatcher.find() && (i = Integer.parseInt(hourlyForecastMatcher.group(1))) >= 1
248 updateHourlyForecastChannel(channelUID, (i - 1));
251 Matcher dailyForecastMatcher = CHANNEL_GROUP_DAILY_FORECAST_PREFIX_PATTERN.matcher(channelGroupId);
252 if (dailyForecastMatcher.find() && (i = Integer.parseInt(dailyForecastMatcher.group(1))) >= 1
254 updateDailyForecastChannel(channelUID, i);
257 Matcher minutelyForecastMatcher = CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX_PATTERN
258 .matcher(channelGroupId);
259 if (minutelyForecastMatcher.find() && (i = Integer.parseInt(minutelyForecastMatcher.group(1))) >= 1
261 updateMinutelyForecastChannel(channelUID, i - 1);
264 Matcher alertsMatcher = CHANNEL_GROUP_ALERTS_PREFIX_PATTERN.matcher(channelGroupId);
265 if (alertsMatcher.find() && (i = Integer.parseInt(alertsMatcher.group(1))) >= 1) {
266 updateAlertsChannel(channelUID, i);
274 * Update the channel from the last OpenWeatherMap data retrieved.
276 * @param channelUID the id identifying the channel to be updated
278 private void updateCurrentChannel(ChannelUID channelUID) {
279 String channelId = channelUID.getIdWithoutGroup();
280 String channelGroupId = channelUID.getGroupId();
281 OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
282 if (localWeatherData != null) {
283 State state = UnDefType.UNDEF;
285 case CHANNEL_STATION_LOCATION:
286 state = getPointTypeState(localWeatherData.getLat(), localWeatherData.getLon());
288 case CHANNEL_TIME_STAMP:
289 state = getDateTimeTypeState(localWeatherData.getCurrent().getDt());
291 case CHANNEL_SUNRISE:
292 state = getDateTimeTypeState(localWeatherData.getCurrent().getSunrise());
295 state = getDateTimeTypeState(localWeatherData.getCurrent().getSunset());
297 case CHANNEL_CONDITION:
298 if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
299 state = getStringTypeState(localWeatherData.getCurrent().getWeather().get(0).getDescription());
302 case CHANNEL_CONDITION_ID:
303 if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
304 state = getStringTypeState(
305 Integer.toString(localWeatherData.getCurrent().getWeather().get(0).getId()));
308 case CHANNEL_CONDITION_ICON:
309 if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
310 state = getRawTypeState(OpenWeatherMapConnection
311 .getWeatherIcon(localWeatherData.getCurrent().getWeather().get(0).getIcon()));
314 case CHANNEL_CONDITION_ICON_ID:
315 if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
316 state = getStringTypeState(localWeatherData.getCurrent().getWeather().get(0).getIcon());
319 case CHANNEL_TEMPERATURE:
320 state = getQuantityTypeState(localWeatherData.getCurrent().getTemp(), CELSIUS);
322 case CHANNEL_APPARENT_TEMPERATURE:
323 state = getQuantityTypeState(localWeatherData.getCurrent().getFeelsLike(), CELSIUS);
325 case CHANNEL_PRESSURE:
326 state = getQuantityTypeState(localWeatherData.getCurrent().getPressure(), HECTO(PASCAL));
328 case CHANNEL_HUMIDITY:
329 state = getQuantityTypeState(localWeatherData.getCurrent().getHumidity(), PERCENT);
331 case CHANNEL_DEW_POINT:
332 state = getQuantityTypeState(localWeatherData.getCurrent().getDewPoint(), CELSIUS);
334 case CHANNEL_WIND_SPEED:
335 state = getQuantityTypeState(localWeatherData.getCurrent().getWindSpeed(), METRE_PER_SECOND);
337 case CHANNEL_WIND_DIRECTION:
338 state = getQuantityTypeState(localWeatherData.getCurrent().getWindDeg(), DEGREE_ANGLE);
340 case CHANNEL_GUST_SPEED:
341 state = getQuantityTypeState(localWeatherData.getCurrent().getWindGust(), METRE_PER_SECOND);
343 case CHANNEL_CLOUDINESS:
344 state = getQuantityTypeState(localWeatherData.getCurrent().getClouds(), PERCENT);
346 case CHANNEL_UVINDEX:
347 state = getDecimalTypeState(localWeatherData.getCurrent().getUvi());
350 Precipitation rain = localWeatherData.getCurrent().getRain();
351 state = getQuantityTypeState(rain == null ? 0 : rain.get1h(), MILLI(METRE));
354 Precipitation snow = localWeatherData.getCurrent().getSnow();
355 state = getQuantityTypeState(snow == null ? 0 : snow.get1h(), MILLI(METRE));
357 case CHANNEL_VISIBILITY:
358 State tempstate = new QuantityType<>(localWeatherData.getCurrent().getVisibility(), METRE)
359 .toUnit(KILO(METRE));
360 state = (tempstate == null ? state : tempstate);
363 // This should not happen
364 logger.warn("Unknown channel id {} in onecall current weather data", channelId);
367 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
368 updateState(channelUID, state);
370 logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
374 private void updateMinutelyForecastChannel(ChannelUID channelUID, int count) {
375 String channelId = channelUID.getIdWithoutGroup();
376 String channelGroupId = channelUID.getGroupId();
377 OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
378 if (forecastMinutes == 0) {
380 "Can't update channel group {} because forecastMinutes is set to '0'. Please adjust config accordingly",
384 if (localWeatherData != null && localWeatherData.getMinutely() != null
385 && localWeatherData.getMinutely().size() > count) {
386 org.openhab.binding.openweathermap.internal.dto.onecall.Minutely forecastData = localWeatherData
387 .getMinutely().get(count);
388 State state = UnDefType.UNDEF;
390 case CHANNEL_TIME_STAMP:
391 state = getDateTimeTypeState(forecastData.getDt());
393 case CHANNEL_PRECIPITATION:
394 double precipitation = forecastData.getPrecipitation();
395 state = getQuantityTypeState(precipitation, MILLI(METRE));
398 // This should not happen
399 logger.warn("Unknown channel id {} in onecall minutely weather data", channelId);
402 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
403 updateState(channelUID, state);
405 logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
410 * Update the channel from the last OpenWeatherMap data retrieved.
412 * @param channelUID the id identifying the channel to be updated
413 * @param count the number of the hour referenced by the channel
415 private void updateHourlyForecastChannel(ChannelUID channelUID, int count) {
416 String channelId = channelUID.getIdWithoutGroup();
417 String channelGroupId = channelUID.getGroupId();
418 if (forecastHours == 0) {
420 "Can't update channel group {} because forecastHours is set to '0'. Please adjust config accordingly",
424 OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
425 if (localWeatherData != null && localWeatherData.getHourly().size() > count) {
426 org.openhab.binding.openweathermap.internal.dto.onecall.Hourly forecastData = localWeatherData.getHourly()
428 State state = UnDefType.UNDEF;
430 case CHANNEL_TIME_STAMP:
431 state = getDateTimeTypeState(forecastData.getDt());
433 case CHANNEL_CONDITION:
434 if (!forecastData.getWeather().isEmpty()) {
435 state = getStringTypeState(forecastData.getWeather().get(0).getDescription());
438 case CHANNEL_CONDITION_ID:
439 if (!forecastData.getWeather().isEmpty()) {
440 state = getStringTypeState(Integer.toString(forecastData.getWeather().get(0).getId()));
443 case CHANNEL_CONDITION_ICON:
444 if (!forecastData.getWeather().isEmpty()) {
445 state = getRawTypeState(
446 OpenWeatherMapConnection.getWeatherIcon(forecastData.getWeather().get(0).getIcon()));
449 case CHANNEL_CONDITION_ICON_ID:
450 if (!forecastData.getWeather().isEmpty()) {
451 state = getStringTypeState(forecastData.getWeather().get(0).getIcon());
454 case CHANNEL_TEMPERATURE:
455 state = getQuantityTypeState(forecastData.getTemp(), CELSIUS);
457 case CHANNEL_APPARENT_TEMPERATURE:
458 state = getQuantityTypeState(forecastData.getFeelsLike(), CELSIUS);
460 case CHANNEL_PRESSURE:
461 state = getQuantityTypeState(forecastData.getPressure(), HECTO(PASCAL));
463 case CHANNEL_HUMIDITY:
464 state = getQuantityTypeState(forecastData.getHumidity(), PERCENT);
466 case CHANNEL_DEW_POINT:
467 state = getQuantityTypeState(forecastData.getDewPoint(), CELSIUS);
469 case CHANNEL_WIND_SPEED:
470 state = getQuantityTypeState(forecastData.getWindSpeed(), METRE_PER_SECOND);
472 case CHANNEL_WIND_DIRECTION:
473 state = getQuantityTypeState(forecastData.getWindDeg(), DEGREE_ANGLE);
475 case CHANNEL_GUST_SPEED:
476 state = getQuantityTypeState(forecastData.getWindGust(), METRE_PER_SECOND);
478 case CHANNEL_CLOUDINESS:
479 state = getQuantityTypeState(forecastData.getClouds(), PERCENT);
481 case CHANNEL_VISIBILITY:
482 State tempstate = new QuantityType<>(localWeatherData.getCurrent().getVisibility(), METRE)
483 .toUnit(KILO(METRE));
484 state = (tempstate == null ? state : tempstate);
485 case CHANNEL_PRECIP_PROBABILITY:
486 state = getQuantityTypeState(forecastData.getPop() * 100.0, PERCENT);
489 Precipitation rain = forecastData.getRain();
490 state = getQuantityTypeState(rain == null ? 0 : rain.get1h(), MILLI(METRE));
493 Precipitation snow = forecastData.getSnow();
494 state = getQuantityTypeState(snow == null ? 0 : snow.get1h(), MILLI(METRE));
497 // This should not happen
498 logger.warn("Unknown channel id {} in onecall hourly weather data", channelId);
501 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
502 updateState(channelUID, state);
504 logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
509 * Update the channel from the last OpenWeatherMap data retrieved.
511 * @param channelUID the id identifying the channel to be updated
514 private void updateDailyForecastChannel(ChannelUID channelUID, int count) {
515 String channelId = channelUID.getIdWithoutGroup();
516 String channelGroupId = channelUID.getGroupId();
517 if (forecastDays == 0) {
519 "Can't update channel group {} because forecastDays is set to '0'. Please adjust config accordingly",
523 OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
524 if (localWeatherData != null && localWeatherData.getDaily().size() > count) {
525 org.openhab.binding.openweathermap.internal.dto.onecall.Daily forecastData = localWeatherData.getDaily()
527 State state = UnDefType.UNDEF;
529 FeelsLikeTemp feelsLike;
531 case CHANNEL_TIME_STAMP:
532 state = getDateTimeTypeState(forecastData.getDt());
534 case CHANNEL_SUNRISE:
535 state = getDateTimeTypeState(forecastData.getSunrise());
538 state = getDateTimeTypeState(forecastData.getSunset());
540 case CHANNEL_CONDITION:
541 if (!forecastData.getWeather().isEmpty()) {
542 state = getStringTypeState(forecastData.getWeather().get(0).getDescription());
545 case CHANNEL_CONDITION_ID:
546 if (!forecastData.getWeather().isEmpty()) {
547 state = getStringTypeState(Integer.toString(forecastData.getWeather().get(0).getId()));
550 case CHANNEL_CONDITION_ICON:
551 if (!forecastData.getWeather().isEmpty()) {
552 state = getRawTypeState(
553 OpenWeatherMapConnection.getWeatherIcon(forecastData.getWeather().get(0).getIcon()));
556 case CHANNEL_CONDITION_ICON_ID:
557 if (!forecastData.getWeather().isEmpty()) {
558 state = getStringTypeState(forecastData.getWeather().get(0).getIcon());
561 case CHANNEL_MIN_TEMPERATURE:
562 temp = forecastData.getTemp();
564 state = getQuantityTypeState(temp.getMin(), CELSIUS);
567 case CHANNEL_MAX_TEMPERATURE:
568 temp = forecastData.getTemp();
570 state = getQuantityTypeState(temp.getMax(), CELSIUS);
573 case CHANNEL_MORNING_TEMPERATURE:
574 temp = forecastData.getTemp();
576 state = getQuantityTypeState(temp.getMorn(), CELSIUS);
579 case CHANNEL_DAY_TEMPERATURE:
580 temp = forecastData.getTemp();
582 state = getQuantityTypeState(temp.getDay(), CELSIUS);
585 case CHANNEL_EVENING_TEMPERATURE:
586 temp = forecastData.getTemp();
588 state = getQuantityTypeState(temp.getEve(), CELSIUS);
591 case CHANNEL_NIGHT_TEMPERATURE:
592 temp = forecastData.getTemp();
594 state = getQuantityTypeState(temp.getNight(), CELSIUS);
598 case CHANNEL_APPARENT_DAY:
599 feelsLike = forecastData.getFeelsLike();
600 if (feelsLike != null) {
601 state = getQuantityTypeState(feelsLike.getDay(), CELSIUS);
604 case CHANNEL_APPARENT_MORNING:
605 feelsLike = forecastData.getFeelsLike();
606 if (feelsLike != null) {
607 state = getQuantityTypeState(feelsLike.getMorn(), CELSIUS);
610 case CHANNEL_APPARENT_EVENING:
611 feelsLike = forecastData.getFeelsLike();
612 if (feelsLike != null) {
613 state = getQuantityTypeState(feelsLike.getEve(), CELSIUS);
616 case CHANNEL_APPARENT_NIGHT:
617 feelsLike = forecastData.getFeelsLike();
618 if (feelsLike != null) {
619 state = getQuantityTypeState(feelsLike.getNight(), CELSIUS);
622 case CHANNEL_PRESSURE:
623 state = getQuantityTypeState(forecastData.getPressure(), HECTO(PASCAL));
625 case CHANNEL_HUMIDITY:
626 state = getQuantityTypeState(forecastData.getHumidity(), PERCENT);
628 case CHANNEL_WIND_SPEED:
629 state = getQuantityTypeState(forecastData.getWindSpeed(), METRE_PER_SECOND);
631 case CHANNEL_WIND_DIRECTION:
632 state = getQuantityTypeState(forecastData.getWindDeg(), DEGREE_ANGLE);
634 case CHANNEL_GUST_SPEED:
635 state = getQuantityTypeState(forecastData.getWindGust(), METRE_PER_SECOND);
637 case CHANNEL_CLOUDINESS:
638 state = getQuantityTypeState(forecastData.getClouds(), PERCENT);
640 case CHANNEL_DEW_POINT:
641 state = getQuantityTypeState(forecastData.getDewPoint(), CELSIUS);
643 case CHANNEL_UVINDEX:
644 state = getDecimalTypeState(forecastData.getUvi());
646 case CHANNEL_VISIBILITY:
647 State tempstate = new QuantityType<>(localWeatherData.getCurrent().getVisibility(), METRE)
648 .toUnit(KILO(METRE));
649 state = (tempstate == null ? state : tempstate);
650 case CHANNEL_PRECIP_PROBABILITY:
651 state = getQuantityTypeState(forecastData.getPop() * 100.0, PERCENT);
654 state = getQuantityTypeState(forecastData.getRain(), MILLI(METRE));
657 state = getQuantityTypeState(forecastData.getSnow(), MILLI(METRE));
660 // This should not happen
661 logger.warn("Unknown channel id {} in onecall daily weather data", channelId);
664 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
665 updateState(channelUID, state);
667 logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
672 * Update the channel from the last OpenWeaterhMap data retrieved.
674 * @param channelUID the id identifying the channel to be updated
677 private void updateAlertsChannel(ChannelUID channelUID, int count) {
678 String channelId = channelUID.getIdWithoutGroup();
679 String channelGroupId = channelUID.getGroupId();
680 OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
681 List<Alert> alerts = localWeatherData != null ? localWeatherData.alerts : null;
682 State state = UnDefType.UNDEF;
683 if (alerts != null && alerts.size() > count) {
684 Alert alert = alerts.get(count - 1);
686 case CHANNEL_ALERT_EVENT:
687 state = getStringTypeState(alert.event);
689 case CHANNEL_ALERT_DESCRIPTION:
690 state = getStringTypeState(alert.description);
692 case CHANNEL_ALERT_ONSET:
693 state = getDateTimeTypeState(alert.start);
695 case CHANNEL_ALERT_EXPIRES:
696 state = getDateTimeTypeState(alert.end);
698 case CHANNEL_ALERT_SOURCE:
699 state = getStringTypeState(alert.senderName);
702 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
704 logger.debug("No data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
706 updateState(channelUID, state);