---
title: "Filter"
description: "Spook adds an action to the filter sensor that changes its settings, so an automation can filter harder or softer without a trip to the helper's options."
canonical_url: "https://spook.boo/filter"
---
# Filter

> Spook adds an action to the filter sensor that changes its settings, so an automation can filter harder or softer without a trip to the helper's options.

The filter helper cleans up a noisy sensor. It is one kind of filter: a low-pass filter smooths the value, an outlier filter drops the spikes, a range filter keeps the value between two bounds, a throttle lets only every so many changes through, and a time based moving average averages over a stretch of time.

How hard it filters is set in the helper's options, and only there. An automation cannot change it: not a wider margin for the kitchen temperature while the oven heats up the room, when every honest jump looks like a spike.

Spook adds an action for that: set options.

## Actions

Spook adds one action to the filter integration. You find it wherever you add an action, marked with a 👻.

### Set options

Changes the settings of one or more filter sensors, like the options of the helper do. What you leave out stays as it is, the sensor it filters and its kind of filter too.

The change goes through the helper's own options, so Home Assistant checks it the same way it checks what you fill in there. Then Home Assistant reloads the sensor, as it does after you save its options. The action returns once the sensor is reloaded, so the next step of your automation already sees it filter with its new settings. A sensor that already has the settings you give is left alone, without a reload.

Which settings a filter has depends on its kind:

| **Kind of filter** | **Its settings** |
| --- | --- |
| Low-pass | Window size, time constant, precision |
| Outlier | Window size, radius, precision |
| Range | Lower bound, upper bound, precision |
| Throttle | Window size, precision |
| Time based moving average | Window size, precision |
| Time throttle | Window size, precision |

- **Window size**: how many samples the filter looks at, as a number. For a time based moving average and a time throttle, it is how long instead, as a duration like `"00:05:00"`.
- **Radius**: how far a value of an outlier filter may be from the median of its window before the filter replaces it with that median.
- **Time constant**: how slowly a low-pass filter follows its source. The higher, the smoother.
- **Lower bound** and **upper bound**: a range filter turns anything below the lower bound into the lower bound, and anything above the upper bound into the upper bound.
- **Precision**: the number of decimals the filter rounds its value to.

A setting the filter does not have is refused, with an error that names the filter and its kind: a low-pass filter has no radius. So is a window of the wrong shape: a number of samples for a time throttle, or a duration for a throttle.

A range filter can be without a bound. To take one away, give it `null` in YAML: `lower_bound: null` lets anything low through. The options of the helper take no bound below `0`, even though a filter set up in YAML does, so neither does Spook. A lower bound above the upper bound is refused too. The options of the helper take it, but the filter checks the upper bound first: anything above it becomes the upper bound, anything else the lower bound, so only those two values would ever come out. Equal bounds are fine.

A window of no samples or no time, and a time constant of `0`, are refused. The options of the helper take them, and the filter then has nothing to filter with: a low-pass filter or a moving average divides by them, and fails on every new value.

A sensor with the same name, sensor and settings as another filter is refused by Home Assistant, the same as when you make it twice. The error says so, and names the sensor. It stays as it was.

Every entity is checked before the first sensor changes. An entity that is not a filter sensor, one set up in YAML, one that does not have a setting you give, or a range filter the change would leave with its lower bound above its upper bound, is refused with an error, and nothing changes. The sensors change one at a time, in the order you give them. When Home Assistant refuses the new settings of one, like when they match another filter, the action stops with an error that names that sensor. The sensors before it in the list did change; the ones after it did not.

Run by hand, the action takes an administrator, like the options of a helper do. Your automations can always run it. So can your scripts, unless a user who is not an administrator starts one.

**How to use it**

1. Open the automation or script that should change the filter, and select **Add action**.
2. Search for **set options**, and select **Filter: Set options 👻**.
3. Choose the filter sensors.
4. Turn on the settings you want to change, and fill in the new values.
5. Select **Save**.

To take a bound away, switch the action to YAML with the three dots menu, and set it to `null`.

**Technical details**

| Action properties |
| --- |
| Action | Filter: Set options 👻 |
| Action name | `filter.set_options` |
| Action targets | No targets |
| Action response | No |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=filter.set_options) [![Open your Home Assistant instance and show the Actions tool with a specific action selected.](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=filter.set_options) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `entity_id` | string or list | Yes | `sensor.kitchen_temperature_filtered` |
| `window_size` | integer or time period | No | `5` |
| `radius` | float | No | `2` |
| `time_constant` | integer | No | `10` |
| `lower_bound` | float or `null` | No | `0` |
| `upper_bound` | float or `null` | No | `100` |
| `precision` | integer | No | `2` |

Give at least one of `window_size`, `radius`, `time_constant`, `lower_bound`, `upper_bound` and `precision`, and only the ones the kind of filter has. A window size is a number of samples from `1`, or, for a time based moving average and a time throttle, a duration like `"00:05:00"` or hours, minutes and seconds like `{"minutes": 5}`, longer than nothing. A number is always a number of samples. The time constant is at least `1`. The radius, the bounds and the precision are at least `0`, and the lower bound is no higher than the upper bound. `null` takes a bound away.

**Example actions in YAML**

Smooth harder, over the last ten samples:

```yaml
action: filter.set_options
data:
  entity_id: sensor.kitchen_temperature_smoothed
  window_size: 10
  time_constant: 20
```

A time based moving average over the last fifteen minutes:

```yaml
action: filter.set_options
data:
  entity_id: sensor.living_room_power_average
  window_size: "00:15:00"
```

Take the lower bound away:

```yaml
action: filter.set_options
data:
  entity_id: sensor.water_tank_level_clamped
  lower_bound: null
```

The kitchen temperature goes through an outlier filter with a radius of 2 °C, so a sensor that hiccups does not throw the heating off. While the oven is on, the kitchen honestly warms up a few degrees in minutes, and the filter keeps replacing every honest jump with the old median. From the moment the oven turns on until half an hour after it turns off, the radius is 6 °C:

```yaml
alias: Kitchen temperature filter wider while the oven is on
triggers:
  - trigger: state
    entity_id: switch.oven
    from: "off"
    to: "on"
  - trigger: state
    entity_id: switch.oven
    from: "on"
    to: "off"
    for: "00:30:00"
actions:
  - action: filter.set_options
    data:
      entity_id: sensor.kitchen_temperature_filtered
      radius: "{{ 6 if trigger.to_state.state == 'on' else 2 }}"
```

**Known limitations**

- Every change reloads the sensor. Do not drive the settings from something that changes every few seconds: change them when it matters, like when the oven turns on.
- A reload empties the window of the filter, and fills it again from the recorder: the last changes of the source, as many as the window counts or as far back as it reaches. The filter runs them through again with its new settings, so its value can shift a little right after the change. What the recorder no longer keeps, or leaves out, is not in the window.
- While the sensor reloads, its state is `unavailable` for a moment.
- A lower bound above the upper bound is refused, even though the options of the helper take it. Keep the lower bound at or below the upper bound.
- A bound below `0` is refused, as it is in the options of the helper, even though a filter set up in YAML takes one.
- A sensor with the same name, sensor and settings as another filter is refused, as it is in the options of the helper.
- When Home Assistant refuses the change of one sensor, the sensors before it in the list keep their new settings.
- Filter sensors set up in YAML are refused. Their settings are in your configuration: change them there.


## Sitemap

See the full [sitemap](https://spook.boo/sitemap.md) for all pages.
