Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix device message to stop confusing multiple schedules #231

Merged
merged 7 commits into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion documentation/api/change_log.rst
Expand Up @@ -39,7 +39,14 @@ v2.0-0 | 2020-11-14
- REST endpoints for managing assets: `/assets/` (GET, POST) and `/asset/<id>` (GET, PATCH, DELETE).


v1.3.9 | 2021-04-21
v1.3-10 | 2021-11-08
""""""""""""""""""""

*Affects all versions since v1.3*.

- Fixed the *getDeviceMessage* endpoint for cases in which there are multiple schedules available, by returning only the most recent one.

v1.3-9 | 2021-04-21
"""""""""""""""""""

*Affects all versions since v1.0*.
Expand Down
8 changes: 8 additions & 0 deletions documentation/changelog.rst
Expand Up @@ -18,6 +18,14 @@ Infrastructure / Support
* Improve data specification for forecasting models using timely-beliefs data [see `PR #154 <http://www.github.com/SeitaBV/flexmeasures/pull/154>`_]


v0.7.1 | November XX, 2021
===========================

Bugfixes
-----------
* Fix device messages, which were mixing up older and more recent schedules [see `PR #231 <http://www.github.com/SeitaBV/flexmeasures/pull/231>`_]


v0.7.0 | October 26, 2021
===========================

Expand Down
30 changes: 30 additions & 0 deletions flexmeasures/api/v1_3/implementations.py
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import pandas as pd
from rq.job import Job, NoSuchJobError
from sqlalchemy import and_, func

from flexmeasures.utils.entity_address_utils import (
parse_entity_address,
Expand Down Expand Up @@ -143,12 +144,41 @@ def get_device_message_response(generic_asset_name_groups, duration):
return unknown_schedule(
message + f'no data is known from "{schedule_data_source_name}".'
)

# todo: after moving the Asset's Power data to the corresponding Sensor's TimedBeliefs,
# the query below should be replaced by:
# sensor.search_beliefs(
# event_starts_after=schedule_start,
# event_ends_before=schedule_start + planning_horizon,
# source=scheduler_source,
# most_recent_only=True,
# )

# Subquery to get the most recent schedule only
subq = (
db.session.query(
Power.datetime,
Power.data_source_id,
func.min(Power.horizon).label("most_recent_belief_horizon"),
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
)
.filter(Power.asset_id == asset.id)
.group_by(Power.datetime, Power.data_source_id)
.subquery()
)
power_values = (
Power.query.filter(Power.asset_id == asset.id)
.filter(Power.data_source_id == scheduler_source.id)
.filter(Power.datetime >= schedule_start)
.filter(Power.datetime < schedule_start + planning_horizon)
.order_by(Power.datetime.asc())
.join(
subq,
and_(
Power.datetime == subq.c.datetime,
Power.data_source_id == subq.c.data_source_id,
Power.horizon == subq.c.most_recent_belief_horizon,
),
)
.all()
)
consumption_schedule = pd.Series(
Expand Down