Closed madsciencetist closed 1 month ago
The connection_entity_id
fix looks good but the other one is tricky and I want to test it first. Hopefully I'll have time for it this weekend.
Thanks for the fixes
I agree that _getUnitOfMeasurement()
is a hack and that the solution is to refactor such that units are always tracked with the values. It got tricky in that original unit conversion PR because kWh -> gCO2 happens in getStatistics()
, but then gCO2 -> kgCO2 happens in normalizeStateValue()
, which has no real tracking of the unit of the value it is given.
The new problem I found is as follows. Assume the following configuration:
convert_units_to: monetary
monetary_unit: USD
electricity_price: 0.5
sections:
- entities:
- entity_id: sensor.energy_1 # 100 kWh
add_entities:
- entity_id: sensor.energy_2 # 20 kWh
We want the bar to evaluate to $60. Instead, this happened:
getStatistics()
returns $50 for energy_1
and $10 for energy_2
(correct)_getMemoizedState()
:
entity.attributes.unit_of_measurement
is kWh but _getUnitOfMeasurement()
sees that config.convert_units_to
is monetary and thus sets unit_of_measurement = ''
to pass to normalizeStateValue()
. Then normalizeStateValue('', 50, '')
returns 50 (dollars; correct)add_entities
:subEntity.state
is 10 (dollars; correct)subEntity.attributes.unit_of_measurement
is kWh, which is wrong because we converted to dollars, but unlike above we now pass this wrong unit into normalizeStateValue
normalizeStateValue('', 10, 'kWh')
sees that k and multiplies by 1000, returning 10000 (incorrect)I understand now. The problem is that we don't use this._getUnitOfMeasurement
there.
What confused me is that you also changed subEntity.attributes.unit_of_measurement || unit_of_measurement
to entityConf.unit_of_measurement || subEntity.attributes.unit_of_measurement
. Basically reversed the order, which I think is a problem. Can you change it to this._getUnitOfMeasurement(subEntity.attributes.unit_of_measurement || unit_of_measurement)
so the order is preserved?
Yes, that makes sense
connection_entity_id
connection_entity_id
was specified, it was not added to the list of entities for which to retrieve the state. It seems thatconnection_entity_id
only happened to work if thatentity_id
was also used elsewhere.add_entities
/subtract_entities
normalization with unit conversionenergy.ts
, and the results can be directly added together. However, when_getMemoizedState
was adding the values ofadd_entities
, it didn't realize that the entities to add were already the correct values, and it erroneously normalized them. Now, the same "this has already been converted" logic used for the main entity state is also used for theadd_entity
state.