diff --git a/bin/weewx/units.py b/bin/weewx/units.py index 8a05270f..6e8e7fcd 100644 --- a/bin/weewx/units.py +++ b/bin/weewx/units.py @@ -1286,14 +1286,30 @@ class SeriesHelper(tuple): def data(self): return self[2] - def json(self, order_by='row'): + def json(self, order_by='row', time_series='both'): import json + time_series = time_series.lower() + if time_series not in ['both', 'start', 'stop']: + raise ValueError("Unknown option '%s' for which time series to include" % time_series) + if order_by == 'row': - json_data = [[start_.raw, stop_.raw, data_.raw] - for start_, stop_, data_ in zip(self.start, self.stop, self.data)] + if time_series == 'both': + json_data = [[start_.raw, stop_.raw, data_.raw] + for start_, stop_, data_ in zip(self.start, self.stop, self.data)] + elif time_series == 'start': + json_data = [[start_.raw, data_.raw] + for start_, data_ in zip(self.start, self.data)] + else: + json_data = [[stop_.raw, data_.raw] + for stop_, data_ in zip(self.stop, self.data)] elif order_by == 'column': - json_data = [self.start.raw, self.stop.raw, self.data.raw] + if time_series == 'both': + json_data = [self.start.raw, self.stop.raw, self.data.raw] + elif time_series == 'start': + json_data = [self.start.raw, self.data.raw] + else: + json_data = [self.stop.raw, self.data.raw] else: raise ValueError('Unknown order by option %s' % order_by) diff --git a/docs/series_tags.md b/docs/series_tags.md index 3e1710f2..d536f999 100644 --- a/docs/series_tags.md +++ b/docs/series_tags.md @@ -14,6 +14,18 @@ include the keyword `series`: $period($data_binding=binding_name, $optional_ago=delta).obstype.series[.optional_unit_conversion][.optional_formatting] ``` +## JSON formatting +By adding the suffix `.json()` to the tag, the results will be formatted as JSON. This option has +two optional parameters, `order_by` and `time_series`: +``` +.json(order_by=['row'|'column'], time_series=['start'|'stop'|'both']) +``` +`order_by`: The returned JSON can either be organized by rows, or by columns. The default is `row`. + +`time_series`: This option controls which series are emitted for time. Option `start` selects the +start of each aggregation interval. Option `stop` selects the end of each interval. Option `both` +causes both to be emitted. + ## Series with no aggregation Here's an example of asking for a series with the temperature for all records in the day. We will display it with an HTML `
` tag so that the embedded newlines work.