---
title: "Derivative"
description: "Spook adds an action to the derivative sensor that changes its time window, so an automation can smooth it more or less without a trip to the helper's options."
canonical_url: "https://spook.boo/derivative"
---
# Derivative

> Spook adds an action to the derivative sensor that changes its time window, so an automation can smooth it more or less without a trip to the helper's options.

The derivative sensor helper tells you how fast a value changes: how many watts a minute the power use of the house climbs, how many degrees an hour the boiler warms up. A time window smooths it: the sensor averages the changes over that window, so one jumpy reading does not swing it all over the place.

The time window is set in the helper's options, and only there. An automation cannot change it: not a calmer window at night, when the fridge and the heat pump switching on and off make the power meter jump.

Spook adds an action for that: set time window.

## Actions

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

### Set time window

Changes the time window of one or more derivative sensors, like the options of the helper do. The rest of their settings stay as they are: the sensor they follow, the units, the rounding.

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 works with the new window. A sensor that already has the time window you give is left alone, without a reload.

A time window of `0` is no window: every change of the source counts on its own.

The options of the helper only offer sources with the unit of the current one, and they read that unit from the source as it is now. A source that is not there at all, like one of an integration that is still starting up, is not among them, and the options refuse to keep it. When the window of such a sensor has to change, Spook says so with an error naming the source. A source that is `unavailable` is fine.

Every entity is checked before the first sensor changes. An entity that is not a derivative sensor, one set up in YAML, or one whose window has to change while its source is not there, is refused with an error, and nothing changes. The sensors change one at a time, in the order you give them. When the source of a sensor goes away while the ones before it reload, 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 time window, and select **Add action**.
2. Search for **set time window**, and select **Derivative: Set time window 👻**.
3. Choose the derivative sensors.
4. Fill in the new time window.
5. Select **Save**.

**Technical details**

| Action properties |
| --- |
| Action | Derivative: Set time window 👻 |
| Action name | `derivative.set_time_window` |
| 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=derivative.set_time_window) [![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=derivative.set_time_window) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `entity_id` | string or list | Yes | `sensor.house_power_change` |
| `time_window` | time period | Yes | `"00:05:00"` |

The time window is a duration like `"00:05:00"`, hours, minutes and seconds like `{"minutes": 5}`, or a number of seconds. `0` for no window. It cannot be taken away.

**Example actions in YAML**

Smooth over five minutes:

```yaml
action: derivative.set_time_window
data:
  entity_id: sensor.house_power_change
  time_window: "00:05:00"
```

No smoothing at all:

```yaml
action: derivative.set_time_window
data:
  entity_id: sensor.house_power_change
  time_window: 0
```

The change of the power use of the house warns when it climbs fast, like when somebody turns on the oven and the kettle at once. At night, the fridge and the heat pump switching on and off make the power meter jump, and a one minute window warns for every one of them. From 23:00 until 07:00 the window is fifteen minutes, so a compressor starting barely moves it:

```yaml
alias: House power change calmer at night
triggers:
  - trigger: time
    at: "23:00:00"
    id: night
  - trigger: time
    at: "07:00:00"
    id: day
actions:
  - action: derivative.set_time_window
    data:
      entity_id: sensor.house_power_change
      time_window: "{{ '00:15:00' if trigger.id == 'night' else '00:01:00' }}"
```

**Known limitations**

- Every change reloads the sensor. Do not drive the time window from something that changes every few seconds: change it when it matters, like at a set time.
- A reload keeps the value of the sensor, and empties its window. The next changes of the source count against the new window on their own, without the ones before the reload. Until a whole window has passed, the sensor reads closer to zero than it would have.
- While the sensor reloads, its state is `unavailable` for a moment.
- A sensor whose window has to change while its source is not there is refused, even in the rare case the options of the helper would have kept it. Try again once the source is back.
- Derivative 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.
