Average Sensor

I found this great integration the other day when I was looking for a way to calculate an average of current electricity consumption in real-time. As I’m presenting the value on a wall tablet I don’t want it to update every second but an average of the measurements from the last 10 secs and a new calculation done every 10 secs.

I have two questions:

  • Is it possible to set the rate when the average is calculated? Right now is looks like every 30 secs.
  • Is it possible to remove the decimals to just show the integer? Right now .0 is shown.

My YAML code looks like below:

sensor:
  - platform: average
    name: 'Average Consumption Now'
    entities:
      - sensor.real_time_consumption
    duration: 
      seconds: 10
    precision: 0

So I installed this sensor, and it appears to have an issue. I defined an automation which will trigger whenever the average sensor goes above 1000. It works as expected when value goes from 900 to 1,100, but the problem is that it keeps triggering the action when it goes from 1,100 to 1,500 to 2,000 and so forth.
Any ideas how to solve this?

ca

The problem is your automotion trigger, not the average component.

I don’t know about Home Assistant core automotion but if you use Node Red with an Event State node, be sure to check :

Ignore state change event when:

X Current state equals previous state

Thanks Shaad. Unfortunately I am not familier with node red.

I just realized - my light sensor has many attributes. How can I tell the ‘average sensor’ which attribute to use?

No way. Average sensor only uses sensor value, not attributes. The exception was made only for a few standard entities.

You can use the template sensor as an intermediate to make the attribute the value of the sensor.

Ok, so I will be more specific - if I use the average sensor on Philips Hue light sensor (which is in fact a motion sensor) - so the values per the average sensor will be ‘lightlevel’ (and not for example, lux)?

I’m sorry, I’m not using the Philips Hue Light Sensor and cannot answer you for sure.

The Average Sensor will take the history values of the sensor you provided and calculate the average. It will use the same units as on the original sensor. Attribute values are not included in calculations.

Quite simple…

Ok, everything’s fine. It probably has nothing to do with the average sensor.

The crux of your problem:

  • Every time the source data is updated Average Sensor updates its value;
  • Every time the Average Sensor value is updated, your automation wakes and the conditions for its triggering are checked;
  • If the conditions are acceptable, the automation works completely.

This is where it happens that the automation is triggered many times when the value is above the specified threshold.

In order for it to run only once, you need to come up with a way to check that the value was previously below the threshold and start automation only in this case.

But this does not apply to the Average Sensor topic anymore, sorry.

That’s all right. If there is a calculation period, the sensor is updated approximately once every 30 seconds. The reason is that as the update period decreases, the load to the system increases dramatically. So you have to be careful with that.

As far as I understand, you can add the scan_interval field to the sensor settings indicating the required update period. This can be applied to any entity in HA, not only to this sensor, since this property is handled by the HA core itself.

I will try to solve this, but I’m not sure if this is possible in principle: the sensor does not set the format of the data output — HA itself is responsible for this.

Is anyone experiencing issues after updating HA to 2021.5.1?

My sensors are no longer working and I get the following log error:

Logger: engineio.client
Source: /usr/local/lib/python3.8/site-packages/engineio/asyncio_client.py:559
First occurred: 3:03:00 PM (46 occurrences)
Last logged: 4:15:10 PM

packet queue is empty, aborting

The mechanism for specifying the unique ID of sensors has been changed. To prevent duplicate sensors from being created, add option unique_id: __legacy__ to the settings of already available sensors. For more information, see the component’s documentation.

So I got stung by this and got duplicate _2 sensors.
I just deleted all the original ones which was easy to do with a few clicks in configuration>entities. Then restarted HA and all the _2’s were now the original sensor again complete with their history. No editing config to add legacy needed. Seems way quicker this way than doing that edit…

Yes, it’s an option. But if you have to many average sensors, adding unique_id: __legacy__ is the only way.

If you have many, adding legacy is a nightmare. If you search for average in the entities screen it’s just a few clicks to mark and delete all the old ones then restart.

No. It’s easy work with auto replacement function of editor.

Although, of course, everyone decides for himself how it is easier for him. I just gave the tool.

I’d just rather not use a hack for something like that

1 Like

I’ve add you method to README

2 Likes

Thanks Andrey. Working again. And thanks for the component. It is super valuable.

1 Like