BottlecapDave / HomeAssistant-OctopusEnergy

Unofficial Home Assistant integration for interacting with Octopus Energy
https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/
MIT License
571 stars 57 forks source link

Calculated cost attributes not aligning with charges attributes #768

Closed pacemaker82 closed 5 months ago

pacemaker82 commented 7 months ago

Describe the bug

Expecting that the charges in the "charges" attribute add up to the same value as the total without standing charge. Given the code below, iterating through the charges I retrieve two values:

Calculated Balance: 1.58
Given Balance (without SC): 1.65

Where Calculated Balance is me looping through the charges and adding them up, and Given Balance is the total without standing charge attribute value.

Reproduction steps

    const octopusPreviousDayCharges = hass.states['sensor.octopus_energy_electricity_123456789_previous_accumulative_cost'].attributes['charges'];
    const octopusPreviousDayGivenTotal = hass.states['sensor.octopus_energy_electricity_123456789_previous_accumulative_cost'].attributes['total_without_standing_charge'];

    let balance = 0;
    octopusPreviousDayCharges.forEach(obj => {

        balance += obj.cost;
    });

    console.log("Calculated Balance: " + balance);
    console.log("Given Balance (without SC): " + octopusPreviousDayGivenTotal);

Pasting below my attributes:

Tariff code
E-1R-INTELLI-BB-VAR-23-03-01-J
Standing charge
0.46
Total without standing charge
1.65
Total
2.1
Charges
- start: '2024-02-17T00:00:00+00:00'
end: '2024-02-17T00:30:00+00:00'
rate: 0.074999
consumption: 1.605
cost: 0.12
- start: '2024-02-17T00:30:00+00:00'
end: '2024-02-17T01:00:00+00:00'
rate: 0.074999
consumption: 1.596
cost: 0.12
- start: '2024-02-17T01:00:00+00:00'
end: '2024-02-17T01:30:00+00:00'
rate: 0.074999
consumption: 1.587
cost: 0.12
- start: '2024-02-17T01:30:00+00:00'
end: '2024-02-17T02:00:00+00:00'
rate: 0.074999
consumption: 1.603
cost: 0.12
- start: '2024-02-17T02:00:00+00:00'
end: '2024-02-17T02:30:00+00:00'
rate: 0.074999
consumption: 1.167
cost: 0.09
- start: '2024-02-17T02:30:00+00:00'
end: '2024-02-17T03:00:00+00:00'
rate: 0.074999
consumption: 0
cost: 0
- start: '2024-02-17T03:00:00+00:00'
end: '2024-02-17T03:30:00+00:00'
rate: 0.074999
consumption: 0.163
cost: 0.01
- start: '2024-02-17T03:30:00+00:00'
end: '2024-02-17T04:00:00+00:00'
rate: 0.074999
consumption: 2.964
cost: 0.22
- start: '2024-02-17T04:00:00+00:00'
end: '2024-02-17T04:30:00+00:00'
rate: 0.074999
consumption: 0.055
cost: 0
- start: '2024-02-17T04:30:00+00:00'
end: '2024-02-17T05:00:00+00:00'
rate: 0.074999
consumption: 2.713
cost: 0.2
- start: '2024-02-17T05:00:00+00:00'
end: '2024-02-17T05:30:00+00:00'
rate: 0.074999
consumption: 3.339
cost: 0.25
- start: '2024-02-17T05:30:00+00:00'
end: '2024-02-17T06:00:00+00:00'
rate: 0.074999
consumption: 1.657
cost: 0.12
- start: '2024-02-17T06:00:00+00:00'
end: '2024-02-17T06:30:00+00:00'
rate: 0.312084
consumption: 0.011
cost: 0
- start: '2024-02-17T06:30:00+00:00'
end: '2024-02-17T07:00:00+00:00'
rate: 0.312084
consumption: 0.008
cost: 0
- start: '2024-02-17T07:00:00+00:00'
end: '2024-02-17T07:30:00+00:00'
rate: 0.312084
consumption: 0.004
cost: 0
- start: '2024-02-17T07:30:00+00:00'
end: '2024-02-17T08:00:00+00:00'
rate: 0.312084
consumption: 0.014
cost: 0
- start: '2024-02-17T08:00:00+00:00'
end: '2024-02-17T08:30:00+00:00'
rate: 0.312084
consumption: 0.009
cost: 0
- start: '2024-02-17T08:30:00+00:00'
end: '2024-02-17T09:00:00+00:00'
rate: 0.312084
consumption: 0.006
cost: 0
- start: '2024-02-17T09:00:00+00:00'
end: '2024-02-17T09:30:00+00:00'
rate: 0.312084
consumption: 0.012
cost: 0
- start: '2024-02-17T09:30:00+00:00'
end: '2024-02-17T10:00:00+00:00'
rate: 0.312084
consumption: 0.01
cost: 0
- start: '2024-02-17T10:00:00+00:00'
end: '2024-02-17T10:30:00+00:00'
rate: 0.312084
consumption: 0.007
cost: 0
- start: '2024-02-17T10:30:00+00:00'
end: '2024-02-17T11:00:00+00:00'
rate: 0.312084
consumption: 0.006
cost: 0
- start: '2024-02-17T11:00:00+00:00'
end: '2024-02-17T11:30:00+00:00'
rate: 0.312084
consumption: 0.007
cost: 0
- start: '2024-02-17T11:30:00+00:00'
end: '2024-02-17T12:00:00+00:00'
rate: 0.312084
consumption: 0.007
cost: 0
- start: '2024-02-17T12:00:00+00:00'
end: '2024-02-17T12:30:00+00:00'
rate: 0.312084
consumption: 0.018
cost: 0.01
- start: '2024-02-17T12:30:00+00:00'
end: '2024-02-17T13:00:00+00:00'
rate: 0.312084
consumption: 0.013
cost: 0
- start: '2024-02-17T13:00:00+00:00'
end: '2024-02-17T13:30:00+00:00'
rate: 0.312084
consumption: 0.012
cost: 0
- start: '2024-02-17T13:30:00+00:00'
end: '2024-02-17T14:00:00+00:00'
rate: 0.312084
consumption: 0.008
cost: 0
- start: '2024-02-17T14:00:00+00:00'
end: '2024-02-17T14:30:00+00:00'
rate: 0.312084
consumption: 0.02
cost: 0.01
- start: '2024-02-17T14:30:00+00:00'
end: '2024-02-17T15:00:00+00:00'
rate: 0.312084
consumption: 0.009
cost: 0
- start: '2024-02-17T15:00:00+00:00'
end: '2024-02-17T15:30:00+00:00'
rate: 0.312084
consumption: 0.011
cost: 0
- start: '2024-02-17T15:30:00+00:00'
end: '2024-02-17T16:00:00+00:00'
rate: 0.312084
consumption: 0.023
cost: 0.01
- start: '2024-02-17T16:00:00+00:00'
end: '2024-02-17T16:30:00+00:00'
rate: 0.312084
consumption: 0.016
cost: 0
- start: '2024-02-17T16:30:00+00:00'
end: '2024-02-17T17:00:00+00:00'
rate: 0.312084
consumption: 0.008
cost: 0
- start: '2024-02-17T17:00:00+00:00'
end: '2024-02-17T17:30:00+00:00'
rate: 0.312084
consumption: 0.009
cost: 0
- start: '2024-02-17T17:30:00+00:00'
end: '2024-02-17T18:00:00+00:00'
rate: 0.312084
consumption: 0.085
cost: 0.03
- start: '2024-02-17T18:00:00+00:00'
end: '2024-02-17T18:30:00+00:00'
rate: 0.312084
consumption: 0.011
cost: 0
- start: '2024-02-17T18:30:00+00:00'
end: '2024-02-17T19:00:00+00:00'
rate: 0.312084
consumption: 0.006
cost: 0
- start: '2024-02-17T19:00:00+00:00'
end: '2024-02-17T19:30:00+00:00'
rate: 0.312084
consumption: 0.004
cost: 0
- start: '2024-02-17T19:30:00+00:00'
end: '2024-02-17T20:00:00+00:00'
rate: 0.312084
consumption: 0.006
cost: 0
- start: '2024-02-17T20:00:00+00:00'
end: '2024-02-17T20:30:00+00:00'
rate: 0.312084
consumption: 0.004
cost: 0
- start: '2024-02-17T20:30:00+00:00'
end: '2024-02-17T21:00:00+00:00'
rate: 0.312084
consumption: 0.006
cost: 0
- start: '2024-02-17T21:00:00+00:00'
end: '2024-02-17T21:30:00+00:00'
rate: 0.312084
consumption: 0.006
cost: 0
- start: '2024-02-17T21:30:00+00:00'
end: '2024-02-17T22:00:00+00:00'
rate: 0.312084
consumption: 0.004
cost: 0
- start: '2024-02-17T22:00:00+00:00'
end: '2024-02-17T22:30:00+00:00'
rate: 0.312084
consumption: 0.005
cost: 0
- start: '2024-02-17T22:30:00+00:00'
end: '2024-02-17T23:00:00+00:00'
rate: 0.312084
consumption: 0.001
cost: 0
- start: '2024-02-17T23:00:00+00:00'
end: '2024-02-17T23:30:00+00:00'
rate: 0.312084
consumption: 0.083
cost: 0.03
- start: '2024-02-17T23:30:00+00:00'
end: '2024-02-18T00:00:00+00:00'
rate: 0.074999
consumption: 1.547
cost: 0.12

Expected behaviour

Both outputs should match.

Tariff Code

E-1R-INTELLI-BB-VAR-23-03-01-J

Integration Version

10.1.2

Home Assistant Version

2024.2.2

Fresh Install?

Fresh install

Home Assistant Logs

n/a

Confirmation

pacemaker82 commented 7 months ago

It seems this might be due to rounding. the attribute cost is rounded up/down which when totalled up is missing some fractional costs. If I loop through the rate * consumption I do return the same total as the total_without_standing_charge attribute.

Leaving this open in case you might consider having the cost field be more accurate.

BottlecapDave commented 7 months ago

This has been like this for quite a while and originates for when the field was originally a string with £/p denominations representations. Agreed this can probably afford to be a bit more accurate, but would be a breaking change in the integration (as people might be relying on this to be more "human readable", for instance if plotted on a graph).

As I try and group up breaking changes in batches (to make it less annoying for users), the fix will be delayed. But for now, I'll update the docs to make this clear that it's rounded.

edwardhorsford commented 7 months ago

I just came across this whilst plotting my half hourly costs - where the nigh time values were all suspiciously 1p exactly. I've now worked around it by calculating my own cost - but it might be good to consider exposing the real cost or including more significant figures.

github-actions[bot] commented 6 months ago

This issue has become stale because it has been open for 30 days with no activity. If you still think it's an issue, please respond soon.

github-actions[bot] commented 5 months ago

This issue has been closed because it has been inactive for 14 days since being marked as stale. This is done to help keep on top of active issues. If you still think it's an issue, please respond to this issue