Skip to content

Persistence: support direct arithmetics against PersistedState #288

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

Merged
merged 1 commit into from
Jun 28, 2024

Conversation

jimtng
Copy link
Contributor

@jimtng jimtng commented Jun 28, 2024

Currently, adding up PersistedStates requires adding .state

    spot_prices = SpotPrice.all_states_between(start, start + 2.days)

    time_series = TimeSeries.new # the default policy is replace
    spot_prices.each do |spot_price|
      total_price = spot_price.state +
                    GridTariff.persisted_state(spot_price.timestamp).state +
                    SystemTariff.persisted_state(spot_price.timestamp).state +
                    TransmissionGridTariff.persisted_state(spot_price.timestamp).state +
                    ElectricityTax.persisted_state(spot_price.timestamp).state
      time_series.add(spot_price.timestamp, total_price)
    end

This PR makes this possible (only the relevant part shown here):

total_price = spot_price +
                    GridTariff.persisted_state(spot_price.timestamp) +
                    SystemTariff.persisted_state(spot_price.timestamp) +
                    TransmissionGridTariff.persisted_state(spot_price.timestamp) +
                    ElectricityTax.persisted_state(spot_price.timestamp)
 

Is this a good idea or not?

@jimtng jimtng marked this pull request as draft June 28, 2024 15:59
@jimtng jimtng marked this pull request as draft June 28, 2024 15:59
@jimtng jimtng force-pushed the persistedstate-arithmetics branch 2 times, most recently from 2e86018 to edace20 Compare June 28, 2024 16:09
@jimtng jimtng force-pushed the persistedstate-arithmetics branch from edace20 to 00ce56f Compare June 28, 2024 16:12
@jimtng jimtng added the enhancement New feature or request label Jun 28, 2024
@jimtng jimtng marked this pull request as ready for review June 28, 2024 16:18
@jimtng jimtng requested a review from ccutrer June 28, 2024 16:19
Copy link
Contributor

@ccutrer ccutrer left a comment

Choose a reason for hiding this comment

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

given PersistedState is supposed to act like a subclass of State, with the two extra fields (timestamp, and which item it came from), this seems reasonable to me.

@ccutrer ccutrer merged commit caf032a into openhab:main Jun 28, 2024
18 checks passed
@jimtng jimtng deleted the persistedstate-arithmetics branch June 29, 2024 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants