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

Fetch and stream 3-phase power #847

Conversation

daniel-zullo-frequenz
Copy link
Contributor

@daniel-zullo-frequenz daniel-zullo-frequenz commented Jan 19, 2024

The 3-phase active power is temporary needed to calculate the power factor of the microgrid. The power factor is not available through the microgrid API in the current version used in the SDK.

So the 3-phase active power is temporary exposed through the microgrid API until the migration to the latest version is
completed. Then the 3-phase power factor can be fetched and streamed through the microgrid API.

Temporary workaround for #841.

@daniel-zullo-frequenz daniel-zullo-frequenz self-assigned this Jan 19, 2024
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:data-pipeline Affects the data pipeline part:actor Affects an actor ot the actors utilities (decorator, etc.) part:microgrid Affects the interactions with the microgrid labels Jan 19, 2024
@daniel-zullo-frequenz daniel-zullo-frequenz changed the title Add logical component for consumer power formulas Fetch and stream 3-phase power Jan 20, 2024
@llucax llucax modified the milestones: v1.0.0-rc5, v1.0.0-rc4 Jan 29, 2024
@daniel-zullo-frequenz daniel-zullo-frequenz force-pushed the feature/stream-power-metrics branch from 35567c1 to 16e2030 Compare February 1, 2024 09:16
@github-actions github-actions bot removed part:docs Affects the documentation part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) labels Feb 1, 2024
@daniel-zullo-frequenz daniel-zullo-frequenz added the cmd:skip-release-notes It is not necessary to update release notes for this PR label Feb 1, 2024
@daniel-zullo-frequenz daniel-zullo-frequenz marked this pull request as ready for review February 1, 2024 09:17
@daniel-zullo-frequenz daniel-zullo-frequenz requested a review from a team as a code owner February 1, 2024 09:17
@daniel-zullo-frequenz
Copy link
Contributor Author

So the 3-phase active power is temporary exposed through the microgrid API until the migration to the latest version is
completed. Then the 3-phase power factor can be fetched and streamed through the microgrid API.

I skipped release-notes for that ^ reason

@llucax llucax removed this from the v1.0.0-rc4 milestone Feb 1, 2024
Copy link
Contributor

@shsms shsms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just have concerns about the idea of streaming a 3 phase power for an arbitrary meter, because such a value can't be used in a meaningful way, as active power. It can only be used for calculating the power factor, in which case we might as well calculate it in the SDK and stream just that.

If we wanted to stream 3 phase power for its own sake, we'd want to create a formula generator for it instead, and create formulas for grid.power_3_phase, etc.

I'm also unsure of the idea of streaming resampled power values and using it with unresampled voltage values to calculate power factor. It would be much better to do it with values coming from a microgrid.client.meter_data stream rather than going through the resampler/data sourcing actor.

Also, how does this help calculate power factor if we also don't stream the current values for the arbitrary meter?

async for active_power_sample in active_power_recv:
print(active_power_sample)
```
"""
Copy link
Contributor

@shsms shsms Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an ambiguous/confusing interface because the 3phase power values are actually for an arbitrary meter, but active power is not a common quantity like frequency or voltage, and can be different in different places in the microgrid. So microgrid._active_power() without a component id or a location in the graph, tells us nothing.

If you stream the power factor instead using power measurements from the arbitrary meter, then that would be a common value for the whole microgrid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably also be called _power_3_phase if we are going this route.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the current use-case we only need the 3-phase power factor. I thought we can compute the 3-phase Power Factor on the application side given that the 3-phase grid current (formula generator) and 3-phase voltage (resampled voltage) are already available in the SDK. So the idea was to create a temporary method microgrid._active_power() ("not exposed to the user") to calculate PF = Active Power / (Voltage . Current) in the app and drop it once Power Factor is available in the SDK
Anyway I think this was just a bad idea given the implications it might cause.

I can try to (1) calculate the Power Factor in the SDK or (2) create the 3-phase power formula generator. The (1) needs to be dropped once the power factor is available in the SDK and (2) could be helpful for another scenarios

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (3) is just to wait until power factor is available and just drop this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use the grid current for this calculation, unless you're also using the 3-phase grid power. The current ActivePowerStreamer streams the 3-phase power of the first_descendant_component, which would be grid power only if the location has a grid-side main meter, which is not everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1) calculate the Power Factor in the SDK

for this you could just find the first_descendant_component and use raw data from it to do the calculations

or (2) create the 3-phase power formula generator

I guess with this, you'd make a 3-phase power formula for grid power?

I think both options are fine as a temporary solution, which ever is easier for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grid.current is 3-phase current, see https://github.com/frequenz-floss/frequenz-sdk-python/blob/v1.x.x/src/frequenz/sdk/timeseries/grid.py#L99-L118

I guess with this, you'd make a 3-phase power formula for grid power?

Yes, that's what I have in mind.

I'll give a shot to option (2), that should be the easier one 🤞

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, implemented option (2)

Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
Fetch AC active power from meter, inverter and
EV charger components for phase/line 1,2 and 3 respectively.

Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
@daniel-zullo-frequenz daniel-zullo-frequenz force-pushed the feature/stream-power-metrics branch from 16e2030 to da99c00 Compare February 2, 2024 21:10
Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
@daniel-zullo-frequenz daniel-zullo-frequenz force-pushed the feature/stream-power-metrics branch from da99c00 to 1962a0d Compare February 5, 2024 11:23
@daniel-zullo-frequenz daniel-zullo-frequenz force-pushed the feature/stream-power-metrics branch from 1962a0d to 2a9f2bc Compare February 5, 2024 11:30
@daniel-zullo-frequenz
Copy link
Contributor Author

Renamed *_3_phase_power to *_power_3_phase

Comment on lines 67 to 81
if idx > 0:
formula_builder.push_oper("+")

# When EV chargers produce `None` samples, they are excluded from
# the calculation by treating their `None` values as `0`s.
#
# This is not possible for Meters, so when they produce `None`
# values, those values get propagated as the output.
if comp.category in (ComponentCategory.EV_CHARGER,):
nones_are_zeros = True
elif comp.category == ComponentCategory.METER:
nones_are_zeros = False
else:
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to group inverters with ev chargers?

Also I realized there is a bug in this approach: If we encounter an unhandled category like inverter, we will continue. But we've already added a + step, so we're continuing without adding a component after the +.

I think we have to move the if idx > 0: ... stuff after the category check, so that we add a + only if we're going to insert a component.

Might affect other formulas as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Should I apply the same patch for grid current ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, I think so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to group inverters with ev chargers?

Good catch! Yes, I do want to group them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as suggested

),
phase_3=electrical_pb2.AC.ACPhase(
current=metrics_pb2.Metric(value=34.5),
voltage=metrics_pb2.Metric(value=230.2),
power_active=metrics_pb2.Metric(value=3680.1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you want to add an assert for these values as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I do want that. Thanks! Fixed

Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
The 3-phase power is needed to calculate the power factor
of the microgrid. The power factor is not available through
the microgrid API in the current version used in the SDK.

So the 3-phase power is temporary exposed through the grid
until the migration to the latest version is completed.
Then the 3-phase power factor can be fetched and streamed
through the microgrid.

Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
@daniel-zullo-frequenz daniel-zullo-frequenz force-pushed the feature/stream-power-metrics branch from 2a9f2bc to 12f7bf0 Compare February 5, 2024 17:04
@github-actions github-actions bot added the part:docs Affects the documentation label Feb 5, 2024
Add the formula operator if and only if there is a component to add.
Otherwise, this was adding a `+` operator, but not adding a component
after the `+`.

Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
@daniel-zullo-frequenz daniel-zullo-frequenz force-pushed the feature/stream-power-metrics branch from 12f7bf0 to ca22472 Compare February 5, 2024 17:06
@daniel-zullo-frequenz daniel-zullo-frequenz removed the cmd:skip-release-notes It is not necessary to update release notes for this PR label Feb 6, 2024
@daniel-zullo-frequenz daniel-zullo-frequenz added this pull request to the merge queue Feb 6, 2024
Merged via the queue into frequenz-floss:v1.x.x with commit 1d4ccce Feb 6, 2024
15 checks passed
@daniel-zullo-frequenz daniel-zullo-frequenz deleted the feature/stream-power-metrics branch February 6, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:actor Affects an actor ot the actors utilities (decorator, etc.) part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:microgrid Affects the interactions with the microgrid part:tests Affects the unit, integration and performance (benchmarks) tests
Projects
Development

Successfully merging this pull request may close these issues.

3 participants