Commit Graph

27 Commits (spacegaier-patch-3)

Author SHA1 Message Date
Kryštof Korb 80f2e66e89
Fix history stats (missing endraw) (#25023) 2022-11-22 11:15:42 +01:00
Kryštof Korb c1422c5734
Add previous month example to history stats (#24997) 2022-11-22 10:11:22 +01:00
ildar170975 3091d230cd
Update history_stats.markdown (#23405) 2022-07-16 17:47:17 +02:00
J. Nick Koston a207a66983
Update history_stats count description to reflect actual implementation (#22808) 2022-05-23 11:04:28 +02:00
J. Nick Koston 9f073ab210
Quote durations on history_stats examples (#22777) 2022-05-16 16:57:42 +02:00
Franck Nijhof 5d72c2a03c
Sync codebase with docs for 2022.4.0b0 2022-03-30 21:50:39 +02:00
Franck Nijhof 0b04252537
Move /lovelace -> /dashboards, and renames resulting from that (#22036) 2022-03-15 21:36:33 +01:00
elyobelyob 5ddb6529c1
I found that a 4pm or other non midnight required buffering. (#16182)
* Update history_stats.markdown

---
title: History Stats
description: Instructions about how to integrate historical statistics into Home Assistant.
ha_category:
  - Utility
  - Sensor
ha_iot_class: Local Polling
ha_release: 0.39
ha_quality_scale: internal
ha_domain: history_stats
---

The `history_stats` sensor platform provides quick statistics about another integration or platforms, using data from the [`history`](/integrations/history/) integration.

It can track how long the integration has been in a specific state, in a custom time period.

Examples of what you can track:

- How long you were at home this week
- How long the lights were ON yesterday
- How long you watched TV today

## Configuration

To enable the history statistics sensor, add the following lines to your `configuration.yaml`:

{% raw %}

```yaml
# Example configuration.yaml entry
sensor:
  - platform: history_stats
    name: Lamp ON today
    entity_id: light.my_lamp
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'
```

{% endraw %}

{% configuration %}
entity_id:
  description: The entity you want to track.
  required: true
  type: string
state:
  description: The states you want to track.
  required: true
  type: [list, string]
name:
  description: Name displayed on the frontend. Note that it is used by Home Assistant to generate sensor's `object_id` so it is advisable to choose a unique one and change name for frontend using [customization](/docs/configuration/customizing-devices/#friendly_name) or via [Lovelace](/lovelace/entities/#name).
  required: false
  default: unnamed statistics
  type: string
type:
  description: "The type of sensor: `time`, `ratio`, or `count`."
  required: false
  default: time
  type: string
start:
  description: When to start the measure (timestamp or datetime).
  required: false
  type: template
end:
  description: When to stop the measure (timestamp or datetime).
  required: false
  type: template
duration:
  description: Duration of the measure.
  required: false
  type: time
{% endconfiguration %}

<div class='note'>

  You have to provide **exactly 2** of `start`, `end` and `duration`.
<br/>
  You can use [template extensions](/topics/templating/#home-assistant-template-extensions) such as `now()` or `as_timestamp()` to handle dynamic dates, as shown in the examples below.

</div>

## Sensor type

Depending on the sensor type you choose, the `history_stats` integration can show different values:

- **time**: The default value, which is the tracked time, in hours
- **ratio**: The tracked time divided by the length of your period, as a percentage
- **count**: How many times the integration you track was changed to the state you track

## Time periods

The `history_stats` integration will execute a measure within a precise time period. You should always provide 2 of the following :
- When the period starts (`start` variable)
- When the period ends (`end` variable)
- How long is the period (`duration` variable)

As `start` and `end` variables can be either datetimes or timestamps, you can configure almost any period you want.

### Duration

The duration variable is used when the time period is fixed. Different syntaxes for the duration are supported, as shown below.

```yaml
# 6 hours
duration: 06:00
```

```yaml
# 1 minute, 30 seconds
duration: 00:01:30
```

```yaml
# 2 hours and 30 minutes
duration:
  # supports seconds, minutes, hours, days
  hours: 2
  minutes: 30
```

<div class='note'>

  If the duration exceeds the number of days of history stored by the `recorder` component (`purge_keep_days`), the history statistics sensor will not have all the information it needs to look at the entire duration. For example, if `purge_keep_days` is set to 7, a history statistics sensor with a duration of 30 days will only report a value based on the last 7 days of history.

</div>

### Examples

Here are some examples of periods you could work with, and what to write in your `configuration.yaml`:

**Today**: starts at 00:00 of the current day and ends right now.

{% raw %}

```yaml
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'
```

{% endraw %}

**Yesterday**: ends today at 00:00, lasts 24 hours.

{% raw %}

```yaml
    end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    duration:
      hours: 24
```

{% endraw %}

**This morning (6AM - 11AM)**: starts today at 6, lasts 5 hours.

{% raw %}

```yaml
    start: '{{ now().replace(hour=6, minute=0, second=0) }}'
    duration:
      hours: 5
```

{% endraw %}

**Current week**: starts last Monday at 00:00, ends right now.

Here, last Monday is _today_ as a timestamp, minus 86400 times the current weekday (86400 is the number of seconds in one day, the weekday is 0 on Monday, 6 on Sunday).

{% raw %}

```yaml
    start: '{{ as_timestamp( now().replace(hour=0, minute=0, second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'
```

{% endraw %}

**Next 4pm **: ends today at 00:00, lasts 30 days. Easy one.

{% raw %}

```yaml
    end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    duration:
      days: 30
```

{% endraw %}

**Last 30 days**: ends today at 00:00, lasts 30 days. Easy one.

{% raw %}

```yaml
    end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    duration:
      days: 30
```

{% endraw %}


** 4PM always in the future**: ends in the future at 16:00, starts 24 hours before.

{% raw %}

```yaml
    end: '{{ (now().replace(minute=0,second=0) + timedelta(hours=8)).replace(hour=16) }}'
    duration:
      hours: 24
```

{% endraw %}

**All your history** starts at timestamp = 0, and ends right now.

{% raw %}

```yaml
    start: '{{ 0 }}'
    end: '{{ now() }}'
```

{% endraw %}

<div class='note'>

  The `/developer-tools/template` page of your Home Assistant UI can help you check if the values for `start`, `end` or `duration` are correct. If you want to check if your period is right, just click on your component, the `from` and `to` attributes will show the start and end of the period, nicely formatted.

</div>

* $pm - 4pm example implemented

* Tweak

* Update source/_integrations/history_stats.markdown

Very happy with this change ...

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update source/_integrations/history_stats.markdown

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2021-03-23 12:11:16 +01:00
Franck Nijhof 8f82a159f9
Add integration platforms to integration frontmatter (#16525) 2021-02-12 11:59:20 +01:00
Franck Nijhof 09759662a1
Bunch of YAML styling improvements - part 3 (#16394) 2021-02-04 03:00:11 +01:00
Franck Nijhof 7910d17c08
Bunch of YAML styling improvements (#16372) 2021-02-01 10:42:12 +01:00
Franck Nijhof 2f0eb16c66
Merge branch 'current' into next 2020-12-09 21:33:48 +01:00
Klaas Schoute d99252f613
👕 Markdown style changes (#15861) 2020-12-09 18:48:17 +01:00
Indu Prakash 984d31de14
Support for multiple states in history_stats (#15708) 2020-11-21 12:44:49 +01:00
mdegat01 19bc48a2fb
Adding sensor and binary sensor categories to utility integrations (#14282) 2020-08-21 12:49:30 +02:00
Tom L 69f65e1c75
History link format + wording. (#13765) 2020-06-15 17:40:32 +02:00
Franck Nijhof ecce31bb7a
Remove logo frontmatter for all internal integration (#12283) 2020-03-06 15:35:09 +01:00
akasma74 b56d6638c1
history_stats: update examples (#12282)
it is possible to change several attributes in just one datetime.replace call
2020-03-06 15:03:26 +01:00
Franck Nijhof c81de1d658
Add ha_domain to integrations frontmatter (#12277) 2020-03-05 17:28:39 -08:00
Franck Nijhof 752dec4047
Adds Terminology Textlint plugin, fixes use of Home Assistant (#12063) 2020-02-12 11:16:02 +01:00
akasma74 afd1e37956
added note about name variable (#11970)
* added note about name variable

I though that name is just for frontend and spent some time finding out where my sensor had gone when I had changed its name.
Hope it will save others' time.
Actually, I don't think this component's approach is great - would be much more intuitive to have name as a key of a dictionary (as with template sensors, for example) and a friendly_name for UI.
Unfortunately, there are many of similar components - perhaps they are older than current template sensor's implementation?

* links -> relative

* ✏️ Tweak

Co-authored-by: Klaas Schoute <klaas_schoute@hotmail.com>
2020-02-06 10:22:02 +01:00
Franck Nijhof 23b0b57c08
Rename frontmatter ha_qa_scale to ha_quality_scale to match codebase (#11672)
* Rename frontmatter ha_qa_scale to ha_quality_scale to match codebase

* Update all integrations quality scales (done by sync)
2020-01-08 13:10:43 +01:00
Franck Nijhof c50eb55b74 Sync title/names with codebase (A-H) (#11655) 2020-01-07 15:30:05 +01:00
Franck Nijhof b983082cb9
Frontmatter formatting (#11654) 2020-01-07 14:43:49 +01:00
System Tester 8089dddff9 corrected URL reference (#10727) 2019-10-12 08:12:15 +02:00
Franck Nijhof dcc7c10ce4
Rewrites all URLs in our website from components -> integrations 2019-10-02 00:35:28 +02:00
Franck Nijhof 57c6dec6c2
Rename components folders to integrations 2019-10-02 00:04:39 +02:00