Skip to content

Commit

Permalink
[Meteostick] Add windvane calibration (#16270)
Browse files Browse the repository at this point in the history
* [Meteostick] Add windvane calibration
* [Meteostick] Added daily rain accumulation

Signed-off-by: Cor Hoogendoorn <chiuaua@hotmail.com>
  • Loading branch information
Chiuaua79 committed Feb 10, 2024
1 parent d1caa31 commit d72c5b5
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 40 deletions.
19 changes: 12 additions & 7 deletions bundles/org.openhab.binding.meteostick/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ Set mode to one of the following depending on your device and region:

### meteostick_davis_iss Configuration Options

| Option | Description |
|---------|-------------------------------------------|
| channel | Sets the RF channel used for this sensor |
| spoon | Size of rain spoon assembly for this sensor in mm. Default value is 0.254 (0.01") for use with Davis part number 7345.280. Set to 0.2 for use with Davis part number 7345.319 |
| Option | Description |
|--------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| channel | Sets the RF channel used for this sensor |
| spoon | Size of rain spoon assembly for this sensor in mm. Default value is 0.254 (0.01") for use with Davis part number 7345.280. Set to 0.2 for use with Davis part number 7345.319 |
| deltaWindDirection | For Davis 6410, 7911 & 7914 anemometers, if your anemometer cannot be mounted aiming true North set the direction it is aiming here (0 to 359 degrees). Default is 0 (for North) |

## Channels

Expand All @@ -69,15 +70,17 @@ Set mode to one of the following depending on your device and region:
| rain-raw | Number | Raw rain counter from the tipping spoon sensor |
| rain-currenthour | Number:Length | The rainfall in the last 60 minutes |
| rain-lasthour | Number:Length | The rainfall in the previous hour |
| rain-today | Number:Length | Accumulated rainfall for today
| solar-power | Number | Solar power from the sensor station |
| signal-strength | Number | Received signal strength |
| low-battery | Switch | Low battery warning |

#### Rainfall

There are three channels associated with rainfall.
There are four channels associated with rainfall.
The raw counter from the tipping bucket is provided, the rainfall in the last 60 minutes is updated on each received rainfall and provides the past 60 minutes of rainfall.
The rainfall in the previous hour is the rainfall for each hour of the day and is updated on the hour.
The accumulated rainfall for today provides the amount of rain for the current date and will reset to 0 at timezone's midnight.

## Full Example

Expand All @@ -95,10 +98,10 @@ Things can be defined in the .things file as follows:

```java
meteostick:meteostick_bridge:receiver [ port="/dev/tty.usbserial-AI02XA60", mode=1 ]
meteostick:meteostick_davis_iss:iss (meteostick:meteostick_bridge:receiver) [ channel=1, spoon=0.2 ]
meteostick:meteostick_davis_iss:iss (meteostick:meteostick_bridge:receiver) [ channel=1, spoon=0.2, deltaWindDirection=0 ]
```

Note the configuration options for `port`, `mode`, `channel` and `spoon` above and adjust as needed for your specific hardware.
Note the configuration options for `port`, `mode`, `channel`, `deltaWindDirection` and `spoon` above and adjust as needed for your specific hardware.

### items/meteostick.items

Expand All @@ -112,6 +115,7 @@ Number:Speed DavisVantageVueWindSpeed "ISS Wind Speed [%.1f m/s]" { channel="met
Number:Speed DavisVantageVueWindSpeedAverage "ISS Average Wind Speed [%.1f m/s]" { channel="meteostick:meteostick_davis_iss:iss:wind-speed-last2min-average" }
Number:Speed DavisVantageVueWindSpeedMaximum "ISS Maximum Wind Speed [%.1f m/s]" { channel="meteostick:meteostick_davis_iss:iss:wind-speed-last2min-maximum" }
Number:Length DavisVantageVueRainCurrentHour "ISS Rain Current Hour [%.1f mm]" { channel="meteostick:meteostick_davis_iss:iss:rain-currenthour" }
Number:Length DavisVantageVueRainToday "ISS Rain Today [%.1f mm]" { channel="meteostick:meteostick_davis_iss:iss:rain-today" }
```

### rules/meteostick.rules
Expand Down Expand Up @@ -157,6 +161,7 @@ then
'dewptf' -> dewptf,
'tempf' -> DavisVantageVueOutdoorTemperature.getStateAs(QuantityType).toUnit('°F').doubleValue,
'rainin' -> DavisVantageVueRainCurrentHour.getStateAs(QuantityType).toUnit('in').doubleValue,
'dailyrainin' -> DavisVantageVueRainToday.getStateAs(QuantityType).toUnit('in').doubleValue,
'baromin' -> MeteoStickPressure.getStateAs(QuantityType).toUnit('inHg').doubleValue,
'softwaretype' -> 'openHAB 2.4')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class MeteostickBindingConstants {
public static final String CHANNEL_RAIN_RAW = "rain-raw";
public static final String CHANNEL_RAIN_CURRENTHOUR = "rain-currenthour";
public static final String CHANNEL_RAIN_LASTHOUR = "rain-lasthour";
public static final String CHANNEL_RAIN_TODAY = "rain-today";
public static final String CHANNEL_WIND_SPEED = "wind-speed";
public static final String CHANNEL_WIND_DIRECTION = "wind-direction";
public static final String CHANNEL_WIND_SPEED_LAST2MIN_AVERAGE = "wind-speed-last2min-average";
Expand All @@ -51,6 +52,7 @@ public class MeteostickBindingConstants {
public static final String PARAMETER_CHANNEL = "channel";
public static final String PARAMETER_SPOON = "spoon";
public static final String PARAMETER_SPOON_DEFAULT = "0.254";
public static final String PARAMETER_WINDVANE = "deltaWindDirection";

// Miscellaneous constants
public static final long HOUR_IN_SEC = 60 * 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.openhab.binding.meteostick.internal.handler.MeteostickBridgeHandler;
import org.openhab.binding.meteostick.internal.handler.MeteostickSensorHandler;
import org.openhab.core.io.transport.serial.SerialPortManager;
import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
Expand All @@ -41,10 +42,13 @@ public class MeteostickHandlerFactory extends BaseThingHandlerFactory {
private Logger logger = LoggerFactory.getLogger(MeteostickHandlerFactory.class);

private final SerialPortManager serialPortManager;
private final CronScheduler scheduler;

@Activate
public MeteostickHandlerFactory(final @Reference SerialPortManager serialPortManager) {
public MeteostickHandlerFactory(final @Reference SerialPortManager serialPortManager,
final @Reference CronScheduler scheduler) {
this.serialPortManager = serialPortManager;
this.scheduler = scheduler;
}

@Override
Expand All @@ -64,7 +68,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
}

if (MeteostickSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
return new MeteostickSensorHandler(thing);
return new MeteostickSensorHandler(thing, scheduler);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.scheduler.ScheduledCompletableFuture;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
Expand All @@ -48,25 +50,33 @@
*
* @author Chris Jackson - Initial contribution
* @author John Cocula - Added variable spoon size, UoM, wind stats, bug fixes
* @author Cor Hoogendoorn - Added option for wind vanes not facing North and cumulative rainfall for today
*/
public class MeteostickSensorHandler extends BaseThingHandler implements MeteostickEventListener {
private static final String DAILY_MIDNIGHT = "1 0 0 * * ? *";
private final CronScheduler cronScheduler;
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DAVIS);

private final Logger logger = LoggerFactory.getLogger(MeteostickSensorHandler.class);

private int channel = 0;
private int deltawinddir = 0;
private int rainspoonold = -1;
private BigDecimal rainfallToday = BigDecimal.ZERO;
private BigDecimal spoon = new BigDecimal(PARAMETER_SPOON_DEFAULT);
private MeteostickBridgeHandler bridgeHandler;
private RainHistory rainHistory = new RainHistory(HOUR_IN_MSEC);
private WindHistory windHistory = new WindHistory(2 * 60 * 1000); // 2 minutes
private ScheduledFuture<?> rainHourlyJob;
private ScheduledCompletableFuture<?> rainMidnightJob;
private ScheduledFuture<?> wind2MinJob;
private ScheduledFuture<?> offlineTimerJob;

private Date lastData;

public MeteostickSensorHandler(Thing thing) {
public MeteostickSensorHandler(Thing thing, final CronScheduler scheduler) {
super(thing);
this.cronScheduler = scheduler;
}

@Override
Expand All @@ -79,10 +89,14 @@ public void initialize() {
if (spoon == null) {
spoon = new BigDecimal(PARAMETER_SPOON_DEFAULT);
}
logger.debug("Initializing MeteoStick handler - Channel {}, Spoon size {} mm.", channel, spoon);

deltawinddir = ((BigDecimal) getConfig().get(PARAMETER_WINDVANE)).intValue();

logger.debug("Initializing MeteoStick handler - Channel {}, Spoon size {} mm, Wind vane offset {} °", channel,
spoon, deltawinddir);

Runnable rainRunnable = () -> {
BigDecimal rainfall = rainHistory.getTotal(spoon);
BigDecimal rainfall = rainHistory.getTotal();
rainfall.setScale(1, RoundingMode.DOWN);
updateState(new ChannelUID(getThing().getUID(), CHANNEL_RAIN_LASTHOUR),
new QuantityType<>(rainfall, MILLI(METRE)));
Expand All @@ -92,6 +106,9 @@ public void initialize() {
long start = HOUR_IN_SEC - ((System.currentTimeMillis() % HOUR_IN_MSEC) / 1000);
rainHourlyJob = scheduler.scheduleWithFixedDelay(rainRunnable, start, HOUR_IN_SEC, TimeUnit.SECONDS);

// Scheduling a job at midnight to reset today's rainfall to 0
rainMidnightJob = cronScheduler.schedule(this::dailyJob, DAILY_MIDNIGHT);

Runnable windRunnable = () -> {
WindStats stats = windHistory.getStats();
updateState(new ChannelUID(getThing().getUID(), CHANNEL_WIND_SPEED_LAST2MIN_AVERAGE),
Expand All @@ -114,6 +131,10 @@ public void dispose() {
rainHourlyJob.cancel(true);
}

if (rainMidnightJob != null) {
rainMidnightJob.cancel(true);
}

if (wind2MinJob != null) {
wind2MinJob.cancel(true);
}
Expand Down Expand Up @@ -193,18 +214,43 @@ public void onDataReceived(String[] data) {
processSignalStrength(data[3]);
processBattery(data.length == 5);

rainHistory.put(rain);
rain &= 0x7F;
int totalspoon = 0;
if (rainspoonold < 0) {
rainspoonold = rain;
}
if (rain < rainspoonold) {
totalspoon = 128 - rainspoonold + rain;
} else {
totalspoon = rain - rainspoonold;
}

BigDecimal rainincrease = BigDecimal.valueOf(totalspoon).multiply(spoon);
rainHistory.put(rainincrease);

BigDecimal rainfall = rainHistory.getTotal(spoon);
BigDecimal rainfall = rainHistory.getTotal();
rainfall.setScale(1, RoundingMode.DOWN);
updateState(new ChannelUID(getThing().getUID(), CHANNEL_RAIN_CURRENTHOUR),
new QuantityType<>(rainfall, MILLI(METRE)));

rainfallToday = rainfallToday.add(rainincrease);
rainfallToday.setScale(1, RoundingMode.DOWN);
updateState(new ChannelUID(getThing().getUID(), CHANNEL_RAIN_TODAY),
new QuantityType<>(rainfallToday, MILLI(METRE)));
rainspoonold = rain;
break;
case "W": // Wind
BigDecimal windSpeed = new BigDecimal(data[2]);
int windDirection = Integer.parseInt(data[3]);
updateState(new ChannelUID(getThing().getUID(), CHANNEL_WIND_SPEED),
new QuantityType<>(windSpeed, METRE_PER_SECOND));
if (deltawinddir != 0) {
if (windDirection < (360 - deltawinddir)) {
windDirection += deltawinddir;
} else {
windDirection -= (360 - deltawinddir);
}
}
updateState(new ChannelUID(getThing().getUID(), CHANNEL_WIND_DIRECTION),
new QuantityType<>(windDirection, DEGREE_ANGLE));

Expand Down Expand Up @@ -265,43 +311,24 @@ public void removeOldEntries() {
}
}

class RainHistory extends SlidingTimeWindow<Integer> {
class RainHistory extends SlidingTimeWindow<BigDecimal> {

public RainHistory(long period) {
super(period);
}

public BigDecimal getTotal(BigDecimal spoon) {
public BigDecimal getTotal() {
removeOldEntries();

int least = -1;
int total = 0;

BigDecimal raintotalmap = BigDecimal.ZERO;
synchronized (storage) {
for (int value : storage.values()) {

/*
* Rain counters have been seen to wrap at 127 and also at 255.
* The Meteostick documentation only mentions 255 at the time of
* this writing. This potential difference is solved by having
* all rain counters wrap at 127 (0x7F) by removing the high bit.
*/
value &= 0x7F;

if (least == -1) {
least = value;
continue;
}
for (BigDecimal value : storage.values()) {

raintotalmap = raintotalmap.add(value);

if (value < least) {
total = 128 - least + value;
} else {
total = value - least;
}
}
}

return BigDecimal.valueOf(total).multiply(spoon);
return raintotalmap;
}
}

Expand Down Expand Up @@ -392,4 +419,11 @@ private synchronized void startTimeoutCheck() {
// Scheduling a job on each hour to update the last hour rainfall
offlineTimerJob = scheduler.schedule(pollingRunnable, 90, TimeUnit.SECONDS);
}

private void dailyJob() {
// Daily job to reset the daily rain accumulation
rainfallToday = BigDecimal.ZERO;
updateState(new ChannelUID(getThing().getUID(), CHANNEL_RAIN_TODAY),
new QuantityType<>(rainfallToday, MILLI(METRE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ thing-type.config.meteostick.meteostick_davis_iss.channel.option.5 = Channel 5
thing-type.config.meteostick.meteostick_davis_iss.channel.option.6 = Channel 6
thing-type.config.meteostick.meteostick_davis_iss.channel.option.7 = Channel 7
thing-type.config.meteostick.meteostick_davis_iss.channel.option.8 = Channel 8
thing-type.config.meteostick.meteostick_davis_iss.deltaWindDirection.label = Wind Vane Direction
thing-type.config.meteostick.meteostick_davis_iss.deltaWindDirection.description = Specifies the direction that the wind vane's North is actually pointed towards (0 to 359)
thing-type.config.meteostick.meteostick_davis_iss.spoon.label = Spoon
thing-type.config.meteostick.meteostick_davis_iss.spoon.description = Specifies the amount of rain needed to tip spoon

Expand All @@ -51,6 +53,8 @@ channel-type.meteostick.rain-lasthour.label = Rainfall (previous Hour)
channel-type.meteostick.rain-lasthour.description = Rainfall in the previous hour
channel-type.meteostick.rain-raw.label = Rainfall (Raw)
channel-type.meteostick.rain-raw.description = A counter between 0 and 255 in spoon-sized steps
channel-type.meteostick.rain-today.label = Rainfall Today
channel-type.meteostick.rain-today.description = Total rainfall today
channel-type.meteostick.solar-power.label = Solar Power
channel-type.meteostick.solar-power.description = Solar panel power percentage
channel-type.meteostick.wind-direction-last2min-average.label = Wind Direction (Average)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<channel id="rain-raw" typeId="rain-raw"/>
<channel id="rain-currenthour" typeId="rain-currenthour"/>
<channel id="rain-lasthour" typeId="rain-lasthour"/>
<channel id="rain-today" typeId="rain-today"/>
<channel id="solar-power" typeId="solar-power"/>
<channel id="signal-strength" typeId="system.signal-strength"/>
<channel id="low-battery" typeId="system.low-battery"/>
Expand Down Expand Up @@ -79,6 +80,13 @@
<default>0.254</default>
<unitLabel>mm</unitLabel>
</parameter>

<parameter name="deltaWindDirection" type="integer" required="false" min="0" max="359">
<label>Wind Vane Direction</label>
<description>Specifies the direction that the wind vane's North is actually pointed towards (0 to 359)</description>
<default>0</default>
<unitLabel>°</unitLabel>
</parameter>
</config-description>
</thing-type>

Expand Down Expand Up @@ -190,6 +198,15 @@
</state>
</channel-type>

<channel-type id="rain-today">
<item-type>Number:Length</item-type>
<label>Rainfall Today</label>
<description>Total rainfall today</description>
<category>Rain</category>
<state readOnly="true" pattern="%.1f %unit%">
</state>
</channel-type>

<channel-type id="solar-power">
<item-type>Number</item-type>
<label>Solar Power</label>
Expand Down

0 comments on commit d72c5b5

Please sign in to comment.