#Thanks for that, but I still have some
1 messages · Page 1 of 1 (latest)
maybe having an example makes it clearer
this is how i do statistics
sensor:
- platform: statistics
name: "WaschmaschineStromverbrauchMean"
entity_id: sensor.steckdose9_power
state_characteristic: mean
sampling_size: 5
precision: 1
which results in a graph like this
https://imgur.com/a/4YzM68x
statistics are basicly sensors that are "downstream" of another sensor with transformed data
about the longterm statisitcs
in your integration you just do this
self._attr_state_class = SensorStateClass.TOTAL
replace the .TOTAL with the one you need
most likely measurement in your case
you dont do more
ha takes car of the rest
Sounds right, but what you listed here is how to add it in a YAML.
When looking at the code - I need to add statistics like this:
StatisticMetaData(
has_mean=False,
has_sum=True,
name=f"iec meter {device.device_number} consumption",
source=DOMAIN,
statistic_id=consumption_statistic_id,
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR
)
Where do I "link" it to the(/a) sensor?
the "sensor_id" is the "statistics_id" no?
from my understanding you would implement a ‎StatisticsSensor from here
https://github.com/home-assistant/core/blob/dev/homeassistant/components/statistics/sensor.py
I'm using Opower as a reference and I don't see they implemented it there:
https://github.com/home-assistant/core/tree/dev/homeassistant/components/opower
i.e.:
class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity):
i dont see the need for a statistics sensor in your case
i would just implement a normal sensor and put the state there
What would be the state in my case?
I'll write again my case. I have a call to an electric company API. I can get a specific elec meter data from there.
The data is updated every 4 hrs, with 15-min window - i.e.
"data": [
{
"status": 0,
"date": "2024-02-20T00:00:00.000000",
"value": 0.297
},
{
"status": 0,
"date": "2024-02-20T00:15:00.000000",
"value": 0.219
},....
I want to keep this data (i.e. previous data) in HA for Electric Dashboard etc
It sounds like I don't really need the sensor, but only the statistics, no?
(unless I want to keep some state for the sensor like "latest measurement" or so)
The way I read the code of opower (where I definitely could be wrong) - is that they have 2 "types" of data:
- Sensors - where they add various states - i.e. total consumption / expected cost, etc.
- LongTerms Statistics - per meter reading (="Forecast") - it's history
afaik there is no statistics entity
there is only sensor
statistics is a platform
Which brings me back to the question, in code - how to link between a sensor and its statistics.
The YAML you sent is more of a sensor configuration
Looking at opower coordinator code:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/opower/coordinator.py#L72
At each time the coordinator runs, it does 2 things:
-
Fetches the forecasts and returns it (for sensors to fetch data from it (= states) - https://github.com/home-assistant/core/blob/dev/homeassistant/components/opower/sensor.py#L229
-
Update the statistics (with no relation to the forecasts): https://github.com/home-assistant/core/blob/dev/homeassistant/components/opower/coordinator.py#L90
the two seem to me as independent flows
Now, I know the main thing here that I rely blindly on Opower implementation, which could be a wrong one, but... 🤷
i think i get what you are talking about now
holy shit my head now hurts
in senseor line 175 and coordinator 93 to 101 calculate both the same value so i think thats part of it
I'm not sure.
In the relevant parts in coordinator - it creates the names of the statistics-
i.e.
f"{self.api.utility.subdomain()}_{account.meter_type.name.lower()}_{account.utility_account_id}_energy_cost"
while in sensor it creates a sensor_id:
f"{coordinator.api.utility.subdomain()}_{forecast.account.utility_account_id}
They call the same api endpoints to get some properties, but other than that - the strings are created differently
sensor sets its own unique id in 224
and i think thats the relevant id that everything should refernence
I'll rephrase what you say -
In Sensor.py - line 224:
_attr_unique_id = f"{device_id}_{description.key}"
However - if you'll look at the keys (e.g. line 82, 92, etc).
They are different than _energy_cost as the statistics. Similar, but different
jep thats where im struggling
maybe there is some messing around with the unique ids somewhere in the inherited classes
tbh ive no idea at the moment
can you tell me what powermeter this is
that thing seems to be connected to the internet so maybe it has a local api where you can get that data in realtime
otherwise i might be a good idea to ask in #devs_core-archived
they might have an idea what to do