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

Consumption schedules show up as negative values in the UI #1345

Open
Flix6x opened this issue Feb 28, 2025 · 0 comments · May be fixed by #1348
Open

Consumption schedules show up as negative values in the UI #1345

Flix6x opened this issue Feb 28, 2025 · 0 comments · May be fixed by #1348
Assignees

Comments

@Flix6x
Copy link
Contributor

Flix6x commented Feb 28, 2025

Historically, FlexMeasures saved consumption values in the database as negative values. At some point, we started supporting the opposite, too, using the consumption_is_positive to record the sign convention for any given power sensor.

Unfortunately, for power sensors that record consumption as positive values and also record schedules from our scheduler, the recorded sign convention is not correctly taken into account in the database. This shows in the UI, and is especially annoying when the same sensor is used to record user data sent over the API, because the sensor will then show realized consumption as positive values, and scheduled consumption as negative values.

Image

This happens because the consumption_is_positive attribute is used wrongly in two places. We had left this unaddressed until now, because in this particular case, two wrongs happen to make a right. Let me explain. TL;DR The bug did not affect the API, so we thought of it as a bug affecting viz only.

Root cause

The first place is in our scheduling service, where we just got the schedule from the scheduler and are about to save it to the database. The schedule uses positive values to indicate consumption, and based on the sign definition that the sensor we save to uses we decide whether to flip the sign. The sign definition of the sensors is encoded in the consumption_is_positive attribute. This is the crucial bit of code that does the flip.

if result["sensor"].measures_power and result["sensor"].get_attribute("consumption_is_positive", True):
    sign = -1

As you can see, the sign is flipped when it shouldn't be. Instead, it should be:

if not result["sensor"].measures_power and result["sensor"].get_attribute("consumption_is_positive", False):
    sign = -1

This change also changes the default, making it agree with the nearby comment:

# For consumption schedules, positive values denote consumption. For the db, consumption is negative

The second place is in our API, where we have just fetched the data from the database and are about to send a response to a user's GET request. The user expects consumption as positive values (for the endpoint to GET a schedule):

if sensor.get_attribute("consumption_is_positive", True):
    sign = -1

As you can see, the sign is again flipped when it shouldn't be. Instead, it should be:

if not sensor.get_attribute("consumption_is_positive", False):
    sign = -1

So the current situation is:

  • If consumption_is_positive is True or left out, we mistakenly switch the schedule's sign before saving to the database, but the GET API also mistakingly switches it back, so the API user does not notice, but the UI user sees consumption as negative values, in contrast to what was suggested by the attribute.
  • If consumption_is_positive is False, we mistakingly do not switch it in the database, but the GET API also mistakingly does not switch it back, so the API user does not notice, but the UI user sees consumption as positive values, in contrast to what was suggested by the attribute.
  • We want to correct both mistakes, by switching the default to False and applying the sign switch to the opposite case.

Expected user impact

More details will follow here. I'm envisioning hosts will require two features in an optional data migration.

  1. Flipping the sign of data affected in the past.
  2. Updating the attribute on relevant sensors (only power sensors with schedules) if the attribute was set explicitly on either the sensor or its asset. Why? Because if it was set explicitly on such sensors, it was probably done by the host (consumption_is_positive=false set as a sensor or asset attribute) or by the API/UI user (set as an asset attribute) to make the UI appear correct even though the setting suggests the opposite of what is observed in both the UI and the API. This seems like a rare case, but is in fact the workaround I would currently suggest for new users that are bothered by this visualization bug.

API user impact:

@Flix6x Flix6x added the Data label Feb 28, 2025
@Flix6x Flix6x self-assigned this Feb 28, 2025
@Flix6x Flix6x added the UI label Feb 28, 2025
@Flix6x Flix6x linked a pull request Mar 2, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant