{"action":"created","comment":{"url":"https://api.github.com/repos/home-assistant/home-assistant/pulls/comments/176301441","pull_request_review_id":105978902,"id":176301441,"diff_hunk":"@@ -156,7 +161,32 @@ def filter_sensor_state_listener(entity, old_state, new_state):\n                 self._unit_of_measurement = new_state.attributes.get(\n                     ATTR_UNIT_OF_MEASUREMENT)\n \n-            self.async_schedule_update_ha_state()\n+            if update_ha:\n+                self.async_schedule_update_ha_state()\n+\n+        if 'recorder' in self.hass.config.components:\n+            history_list = []\n+            for filt in self._filters:\n+                if isinstance(filt.window_size, int):\n+                    filter_history = await self.hass.async_add_job(partial(\n+                        history.get_last_state_changes, self.hass,\n+                        filt.window_size, entity_id=self._entity))\n+                elif isinstance(filt.window_size, datetime.timedelta):\n+                    start = dt_util.utcnow() - filt.window_size\n+                    filter_history = await self.hass.async_add_job(partial(\n+                        history.state_changes_during_period, self.hass,\n+                        start, entity_id=self._entity))\n+                for _, states in filter_history.items():\n+                    history_list.extend(\n+                        [st for st in states if st not in history_list])","path":"homeassistant/components/sensor/filter.py","position":77,"original_position":77,"commit_id":"7421475c80d56d1b7a96095654ada0cd45c30f7f","original_commit_id":"7421475c80d56d1b7a96095654ada0cd45c30f7f","user":{"login":"balloob","id":1444314,"avatar_url":"https://avatars3.githubusercontent.com/u/1444314?v=4","gravatar_id":"","url":"https://api.github.com/users/balloob","html_url":"https://github.com/balloob","followers_url":"https://api.github.com/users/balloob/followers","following_url":"https://api.github.com/users/balloob/following{/other_user}","gists_url":"https://api.github.com/users/balloob/gists{/gist_id}","starred_url":"https://api.github.com/users/balloob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/balloob/subscriptions","organizations_url":"https://api.github.com/users/balloob/orgs","repos_url":"https://api.github.com/users/balloob/repos","events_url":"https://api.github.com/users/balloob/events{/privacy}","received_events_url":"https://api.github.com/users/balloob/received_events","type":"User","site_admin":false},"body":"This is very very inefficient. We should not do a query per filter. We should be able to find the size of what to query _before_ we do a SQL query.\r\n\r\nI am also a bit confused that we need to do instance checks on `window_size`. Why does a variable have 2 different meanings?","created_at":"2018-03-22T03:41:55Z","updated_at":"2018-03-22T03:41:55Z","html_url":"https://github.com/home-assistant/home-assistant/pull/13075#discussion_r176301441","pull_request_url":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/home-assistant/home-assistant/pulls/comments/176301441"},"html":{"href":"https://github.com/home-assistant/home-assistant/pull/13075#discussion_r176301441"},"pull_request":{"href":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075"}}},"pull_request":{"url":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075","id":174196122,"html_url":"https://github.com/home-assistant/home-assistant/pull/13075","diff_url":"https://github.com/home-assistant/home-assistant/pull/13075.diff","patch_url":"https://github.com/home-assistant/home-assistant/pull/13075.patch","issue_url":"https://api.github.com/repos/home-assistant/home-assistant/issues/13075","number":13075,"state":"open","locked":false,"title":"Initialise filter_sensor with historical values","user":{"login":"dgomes","id":137684,"avatar_url":"https://avatars2.githubusercontent.com/u/137684?v=4","gravatar_id":"","url":"https://api.github.com/users/dgomes","html_url":"https://github.com/dgomes","followers_url":"https://api.github.com/users/dgomes/followers","following_url":"https://api.github.com/users/dgomes/following{/other_user}","gists_url":"https://api.github.com/users/dgomes/gists{/gist_id}","starred_url":"https://api.github.com/users/dgomes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgomes/subscriptions","organizations_url":"https://api.github.com/users/dgomes/orgs","repos_url":"https://api.github.com/users/dgomes/repos","events_url":"https://api.github.com/users/dgomes/events{/privacy}","received_events_url":"https://api.github.com/users/dgomes/received_events","type":"User","site_admin":false},"body":"## Description:\r\n\r\nInitialise filter sensor with historical values, therefore limiting the impact of restarts.\r\n\r\nWhen HA is restarted the filter looses its current state (window), requiring a new running period until the filter becomes stable again. \r\n\r\nThis PR loads historical values that enable the filters to reach previous state upon initiation.\r\n\r\n**Pull request in [home-assistant.github.io](https://github.com/home-assistant/home-assistant.github.io) with documentation (if applicable):** home-assistant/home-assistant.github.io#4883\r\n\r\n## Example entry for `configuration.yaml` (if applicable):\r\n```yaml\r\n  - platform: filter\r\n    name: \"events\"\r\n    entity_id: sensor.power_smart_meter\r\n    history_period: 00:15 \r\n    filters:\r\n      - filter: throttle\r\n        window_size: 1\r\n        precision: 2\r\n```\r\n\r\n## Checklist:\r\n  - [x] The code change is tested and works locally.\r\n  - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass**\r\n  - [x] Documentation added/updated in [home-assistant.github.io](https://github.com/home-assistant/home-assistant.github.io)\r\n  - [x] Tests have been added to verify that the new code works.\r\n","created_at":"2018-03-10T23:57:41Z","updated_at":"2018-03-22T03:41:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7b67883f02f66b9c6060ad4b1f024dec85965f73","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":527871105,"url":"https://api.github.com/repos/home-assistant/home-assistant/labels/cla-signed","name":"cla-signed","color":"008000","default":false},{"id":840963745,"url":"https://api.github.com/repos/home-assistant/home-assistant/labels/platform:%20sensor.filter","name":"platform: sensor.filter","color":"ededed","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075/commits","review_comments_url":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075/comments","review_comment_url":"https://api.github.com/repos/home-assistant/home-assistant/pulls/comments{/number}","comments_url":"https://api.github.com/repos/home-assistant/home-assistant/issues/13075/comments","statuses_url":"https://api.github.com/repos/home-assistant/home-assistant/statuses/7421475c80d56d1b7a96095654ada0cd45c30f7f","head":{"label":"dgomes:filter_history","ref":"filter_history","sha":"7421475c80d56d1b7a96095654ada0cd45c30f7f","user":{"login":"dgomes","id":137684,"avatar_url":"https://avatars2.githubusercontent.com/u/137684?v=4","gravatar_id":"","url":"https://api.github.com/users/dgomes","html_url":"https://github.com/dgomes","followers_url":"https://api.github.com/users/dgomes/followers","following_url":"https://api.github.com/users/dgomes/following{/other_user}","gists_url":"https://api.github.com/users/dgomes/gists{/gist_id}","starred_url":"https://api.github.com/users/dgomes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgomes/subscriptions","organizations_url":"https://api.github.com/users/dgomes/orgs","repos_url":"https://api.github.com/users/dgomes/repos","events_url":"https://api.github.com/users/dgomes/events{/privacy}","received_events_url":"https://api.github.com/users/dgomes/received_events","type":"User","site_admin":false},"repo":{"id":94482740,"name":"home-assistant","full_name":"dgomes/home-assistant","owner":{"login":"dgomes","id":137684,"avatar_url":"https://avatars2.githubusercontent.com/u/137684?v=4","gravatar_id":"","url":"https://api.github.com/users/dgomes","html_url":"https://github.com/dgomes","followers_url":"https://api.github.com/users/dgomes/followers","following_url":"https://api.github.com/users/dgomes/following{/other_user}","gists_url":"https://api.github.com/users/dgomes/gists{/gist_id}","starred_url":"https://api.github.com/users/dgomes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgomes/subscriptions","organizations_url":"https://api.github.com/users/dgomes/orgs","repos_url":"https://api.github.com/users/dgomes/repos","events_url":"https://api.github.com/users/dgomes/events{/privacy}","received_events_url":"https://api.github.com/users/dgomes/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dgomes/home-assistant","description":":house_with_garden: Open-source home automation platform running on Python 3","fork":true,"url":"https://api.github.com/repos/dgomes/home-assistant","forks_url":"https://api.github.com/repos/dgomes/home-assistant/forks","keys_url":"https://api.github.com/repos/dgomes/home-assistant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dgomes/home-assistant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dgomes/home-assistant/teams","hooks_url":"https://api.github.com/repos/dgomes/home-assistant/hooks","issue_events_url":"https://api.github.com/repos/dgomes/home-assistant/issues/events{/number}","events_url":"https://api.github.com/repos/dgomes/home-assistant/events","assignees_url":"https://api.github.com/repos/dgomes/home-assistant/assignees{/user}","branches_url":"https://api.github.com/repos/dgomes/home-assistant/branches{/branch}","tags_url":"https://api.github.com/repos/dgomes/home-assistant/tags","blobs_url":"https://api.github.com/repos/dgomes/home-assistant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dgomes/home-assistant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dgomes/home-assistant/git/refs{/sha}","trees_url":"https://api.github.com/repos/dgomes/home-assistant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dgomes/home-assistant/statuses/{sha}","languages_url":"https://api.github.com/repos/dgomes/home-assistant/languages","stargazers_url":"https://api.github.com/repos/dgomes/home-assistant/stargazers","contributors_url":"https://api.github.com/repos/dgomes/home-assistant/contributors","subscribers_url":"https://api.github.com/repos/dgomes/home-assistant/subscribers","subscription_url":"https://api.github.com/repos/dgomes/home-assistant/subscription","commits_url":"https://api.github.com/repos/dgomes/home-assistant/commits{/sha}","git_commits_url":"https://api.github.com/repos/dgomes/home-assistant/git/commits{/sha}","comments_url":"https://api.github.com/repos/dgomes/home-assistant/comments{/number}","issue_comment_url":"https://api.github.com/repos/dgomes/home-assistant/issues/comments{/number}","contents_url":"https://api.github.com/repos/dgomes/home-assistant/contents/{+path}","compare_url":"https://api.github.com/repos/dgomes/home-assistant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dgomes/home-assistant/merges","archive_url":"https://api.github.com/repos/dgomes/home-assistant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dgomes/home-assistant/downloads","issues_url":"https://api.github.com/repos/dgomes/home-assistant/issues{/number}","pulls_url":"https://api.github.com/repos/dgomes/home-assistant/pulls{/number}","milestones_url":"https://api.github.com/repos/dgomes/home-assistant/milestones{/number}","notifications_url":"https://api.github.com/repos/dgomes/home-assistant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dgomes/home-assistant/labels{/name}","releases_url":"https://api.github.com/repos/dgomes/home-assistant/releases{/id}","deployments_url":"https://api.github.com/repos/dgomes/home-assistant/deployments","created_at":"2017-06-15T22:12:01Z","updated_at":"2018-03-18T19:49:12Z","pushed_at":"2018-03-21T23:03:14Z","git_url":"git://github.com/dgomes/home-assistant.git","ssh_url":"git@github.com:dgomes/home-assistant.git","clone_url":"https://github.com/dgomes/home-assistant.git","svn_url":"https://github.com/dgomes/home-assistant","homepage":"https://home-assistant.io","size":80078,"stargazers_count":2,"watchers_count":2,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":2,"default_branch":"dev"}},"base":{"label":"home-assistant:dev","ref":"dev","sha":"89c7c80e42c769857389a3edda2a874b63f974f5","user":{"login":"home-assistant","id":13844975,"avatar_url":"https://avatars3.githubusercontent.com/u/13844975?v=4","gravatar_id":"","url":"https://api.github.com/users/home-assistant","html_url":"https://github.com/home-assistant","followers_url":"https://api.github.com/users/home-assistant/followers","following_url":"https://api.github.com/users/home-assistant/following{/other_user}","gists_url":"https://api.github.com/users/home-assistant/gists{/gist_id}","starred_url":"https://api.github.com/users/home-assistant/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/home-assistant/subscriptions","organizations_url":"https://api.github.com/users/home-assistant/orgs","repos_url":"https://api.github.com/users/home-assistant/repos","events_url":"https://api.github.com/users/home-assistant/events{/privacy}","received_events_url":"https://api.github.com/users/home-assistant/received_events","type":"Organization","site_admin":false},"repo":{"id":12888993,"name":"home-assistant","full_name":"home-assistant/home-assistant","owner":{"login":"home-assistant","id":13844975,"avatar_url":"https://avatars3.githubusercontent.com/u/13844975?v=4","gravatar_id":"","url":"https://api.github.com/users/home-assistant","html_url":"https://github.com/home-assistant","followers_url":"https://api.github.com/users/home-assistant/followers","following_url":"https://api.github.com/users/home-assistant/following{/other_user}","gists_url":"https://api.github.com/users/home-assistant/gists{/gist_id}","starred_url":"https://api.github.com/users/home-assistant/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/home-assistant/subscriptions","organizations_url":"https://api.github.com/users/home-assistant/orgs","repos_url":"https://api.github.com/users/home-assistant/repos","events_url":"https://api.github.com/users/home-assistant/events{/privacy}","received_events_url":"https://api.github.com/users/home-assistant/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/home-assistant/home-assistant","description":":house_with_garden: Open-source home automation platform running on Python 3","fork":false,"url":"https://api.github.com/repos/home-assistant/home-assistant","forks_url":"https://api.github.com/repos/home-assistant/home-assistant/forks","keys_url":"https://api.github.com/repos/home-assistant/home-assistant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/home-assistant/home-assistant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/home-assistant/home-assistant/teams","hooks_url":"https://api.github.com/repos/home-assistant/home-assistant/hooks","issue_events_url":"https://api.github.com/repos/home-assistant/home-assistant/issues/events{/number}","events_url":"https://api.github.com/repos/home-assistant/home-assistant/events","assignees_url":"https://api.github.com/repos/home-assistant/home-assistant/assignees{/user}","branches_url":"https://api.github.com/repos/home-assistant/home-assistant/branches{/branch}","tags_url":"https://api.github.com/repos/home-assistant/home-assistant/tags","blobs_url":"https://api.github.com/repos/home-assistant/home-assistant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/home-assistant/home-assistant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/home-assistant/home-assistant/git/refs{/sha}","trees_url":"https://api.github.com/repos/home-assistant/home-assistant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/home-assistant/home-assistant/statuses/{sha}","languages_url":"https://api.github.com/repos/home-assistant/home-assistant/languages","stargazers_url":"https://api.github.com/repos/home-assistant/home-assistant/stargazers","contributors_url":"https://api.github.com/repos/home-assistant/home-assistant/contributors","subscribers_url":"https://api.github.com/repos/home-assistant/home-assistant/subscribers","subscription_url":"https://api.github.com/repos/home-assistant/home-assistant/subscription","commits_url":"https://api.github.com/repos/home-assistant/home-assistant/commits{/sha}","git_commits_url":"https://api.github.com/repos/home-assistant/home-assistant/git/commits{/sha}","comments_url":"https://api.github.com/repos/home-assistant/home-assistant/comments{/number}","issue_comment_url":"https://api.github.com/repos/home-assistant/home-assistant/issues/comments{/number}","contents_url":"https://api.github.com/repos/home-assistant/home-assistant/contents/{+path}","compare_url":"https://api.github.com/repos/home-assistant/home-assistant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/home-assistant/home-assistant/merges","archive_url":"https://api.github.com/repos/home-assistant/home-assistant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/home-assistant/home-assistant/downloads","issues_url":"https://api.github.com/repos/home-assistant/home-assistant/issues{/number}","pulls_url":"https://api.github.com/repos/home-assistant/home-assistant/pulls{/number}","milestones_url":"https://api.github.com/repos/home-assistant/home-assistant/milestones{/number}","notifications_url":"https://api.github.com/repos/home-assistant/home-assistant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/home-assistant/home-assistant/labels{/name}","releases_url":"https://api.github.com/repos/home-assistant/home-assistant/releases{/id}","deployments_url":"https://api.github.com/repos/home-assistant/home-assistant/deployments","created_at":"2013-09-17T07:29:48Z","updated_at":"2018-03-22T01:40:24Z","pushed_at":"2018-03-22T03:13:08Z","git_url":"git://github.com/home-assistant/home-assistant.git","ssh_url":"git@github.com:home-assistant/home-assistant.git","clone_url":"https://github.com/home-assistant/home-assistant.git","svn_url":"https://github.com/home-assistant/home-assistant","homepage":"https://home-assistant.io","size":80078,"stargazers_count":13329,"watchers_count":13329,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3870,"mirror_url":null,"archived":false,"open_issues_count":821,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":3870,"open_issues":821,"watchers":13329,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075"},"html":{"href":"https://github.com/home-assistant/home-assistant/pull/13075"},"issue":{"href":"https://api.github.com/repos/home-assistant/home-assistant/issues/13075"},"comments":{"href":"https://api.github.com/repos/home-assistant/home-assistant/issues/13075/comments"},"review_comments":{"href":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075/comments"},"review_comment":{"href":"https://api.github.com/repos/home-assistant/home-assistant/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/home-assistant/home-assistant/pulls/13075/commits"},"statuses":{"href":"https://api.github.com/repos/home-assistant/home-assistant/statuses/7421475c80d56d1b7a96095654ada0cd45c30f7f"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"balloob"}}