Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History Stats doesn't work #71593

Closed
MikaelHoogen opened this issue May 9, 2022 · 21 comments · Fixed by #71704 or #73040
Closed

History Stats doesn't work #71593

MikaelHoogen opened this issue May 9, 2022 · 21 comments · Fixed by #71704 or #73040

Comments

@MikaelHoogen
Copy link

The problem

Since the update to 2022.5 the history stats integration doesn't work properly. The first day it counted up time constantly regardless of the state of the entity_id. After midnight it stopped counting completely. When I restart HA it is updated the first minutes. Then it stops again.

What version of Home Assistant Core has the issue?

2022.5.3

What was the last working version of Home Assistant Core?

2022.4.x

What type of installation are you running?

Home Assistant OS

Integration causing the issue

History Stats

Link to integration documentation on our website

https://www.home-assistant.io/integrations/history_stats/

Diagnostics information

No response

Example YAML snippet

- platform: history_stats
    name: heatpump_compressor_today
    entity_id: binary_sensor.heatpump_compressor_state
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
    end: "{{ now().replace(microsecond=0) }}"

Anything in the logs that might be useful for us?

Nothing in the logs

Additional information

No response

@probot-home-assistant
Copy link

history_stats documentation
history_stats source
(message by IssueLinks)

@bdraco bdraco self-assigned this May 9, 2022
@bdraco
Copy link
Member

bdraco commented May 9, 2022

Does it work if you change end: "{{ now().replace(microsecond=0) }}" to a duration: 24:00:00

@gideonyong
Copy link

I have the same issue.
my yaml looks like this:

  • platform: history_stats
    name: toggle_dryingmachine
    entity_id: binary_sensor.dryingmachine_status
    state: 'on'
    type: time
    start: '{{ as_timestamp(states.binary_sensor.dryingmachine_status.last_changed) }}'
    end: '{{ as_timestamp(now()) }}'

@chupacabra71
Copy link

Current Version: 2022.5.3
Last Working Version: Unknown
Install Type: Home Assistant OS
Integration: History Stats
Integration Link: https://www.home-assistant.io/integrations/history_stats/
YAML snippet

  - platform: history_stats
    name: Thermostat Heating Today Upstairs
    entity_id: sensor.hvac_activity_upstairs
    state: 'heating'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

Additional Info: I can see the sensor data in the history, so recorder is working.

@tomlut
Copy link

tomlut commented May 10, 2022

The post by chpacabra71 was solved as an issue with the sensor.hvac_activity_upstairs sensor state.

@bdraco
Copy link
Member

bdraco commented May 10, 2022

I've spent a few hours with no luck replicating this. All of mine are working no matter how hard I try to break them. I'm not discounting that there is an issue here, I've just been unable to replicate it.

@bdraco
Copy link
Member

bdraco commented May 10, 2022

In case it was my dev install, I also just tried it on my production instance.

Screen Shot 2022-05-10 at 08 26 46

Screen Shot 2022-05-10 at 08 26 43

@MikaelHoogen Can you post a dump of the binary_sensor.heatpump_compressor_state state from the developer tools?

@MikaelHoogen
Copy link
Author

Does it work if you change end: "{{ now().replace(microsecond=0) }}" to a duration: 24:00:00

Hi, with this change it seem to work. Kinda strange

@bdraco
Copy link
Member

bdraco commented May 10, 2022

Which time zone is your instance running in?

@bdraco
Copy link
Member

bdraco commented May 10, 2022

Screen Shot 2022-05-10 at 09 05 00

Screen Shot 2022-05-10 at 09 04 48

Here's what mine looks like when I paste your templates into developer tools

@MikaelHoogen
Copy link
Author

MikaelHoogen commented May 10, 2022

In case it was my dev install, I also just tried it on my production instance.

Screen Shot 2022-05-10 at 08 26 46 Screen Shot 2022-05-10 at 08 26 43

@MikaelHoogen Can you post a dump of the binary_sensor.heatpump_compressor_state state from the developer tools?

Around 13:30 I did the change, end: "{{ now().replace(microsecond=0) }}" to a duration: 24:00:00
Screenshot 2022-05-10 at 16 05 01

Screenshot 2022-05-10 at 16 04 03

png">

@MikaelHoogen
Copy link
Author

{{ now().replace(microsecond=0) }}

Screenshot 2022-05-10 at 16 09 11

@bdraco
Copy link
Member

bdraco commented May 10, 2022

I think we have a race condition here

        now_timestamp = floored_timestamp(datetime.datetime.now())

        if now_timestamp < current_period_start_timestamp:
            # History cannot tell the future
            self._history_current_period = []
            self._previous_run_before_start = True

@MikaelHoogen
Copy link
Author

I think we have a race condition here

        now_timestamp = floored_timestamp(datetime.datetime.now())

        if now_timestamp < current_period_start_timestamp:
            # History cannot tell the future
            self._history_current_period = []
            self._previous_run_before_start = True

Hm I don't understand, can you elaborate?

@bdraco
Copy link
Member

bdraco commented May 10, 2022

Can you try this change?

diff --git a/homeassistant/components/history_stats/data.py b/homeassistant/components/history_stats/data.py
index 3f22f4cc32..6a10345df1 100644
--- a/homeassistant/components/history_stats/data.py
+++ b/homeassistant/components/history_stats/data.py
@@ -67,9 +67,10 @@ class HistoryStats:
         current_period_end_timestamp = floored_timestamp(current_period_end)
         previous_period_start_timestamp = floored_timestamp(previous_period_start)
         previous_period_end_timestamp = floored_timestamp(previous_period_end)
-        now_timestamp = floored_timestamp(datetime.datetime.now())
+        utc_now = dt_util.utcnow()
+        now_timestamp = floored_timestamp(utc_now)
 
-        if now_timestamp < current_period_start_timestamp:
+        if current_period_start > utc_now:
             # History cannot tell the future
             self._history_current_period = []
             self._previous_run_before_start = True

@bdraco
Copy link
Member

bdraco commented May 10, 2022

When you had the issue was the state 0 or unknown?

bdraco added a commit to bdraco/home-assistant that referenced this issue May 11, 2022
- history_stats ignores microseconds but it was not doing
  it for state_changed events

Fixes home-assistant#71593
@atjshop
Copy link

atjshop commented May 12, 2022

Can you try this change?

diff --git a/homeassistant/components/history_stats/data.py b/homeassistant/components/history_stats/data.py
index 3f22f4cc32..6a10345df1 100644
--- a/homeassistant/components/history_stats/data.py
+++ b/homeassistant/components/history_stats/data.py
@@ -67,9 +67,10 @@ class HistoryStats:
         current_period_end_timestamp = floored_timestamp(current_period_end)
         previous_period_start_timestamp = floored_timestamp(previous_period_start)
         previous_period_end_timestamp = floored_timestamp(previous_period_end)
-        now_timestamp = floored_timestamp(datetime.datetime.now())
+        utc_now = dt_util.utcnow()
+        now_timestamp = floored_timestamp(utc_now)
 
-        if now_timestamp < current_period_start_timestamp:
+        if current_period_start > utc_now:
             # History cannot tell the future
             self._history_current_period = []
             self._previous_run_before_start = True

I'm using image homeassistant/home-assistant:stable, is there a way for me to make this change manually? I am able to login into the container by "docker exec" command but don't know where this file located.

@bdraco
Copy link
Member

bdraco commented May 12, 2022

@atjshop Can you post a copy of your yaml config ?

@atjshop
Copy link

atjshop commented May 12, 2022

+        if current_period_start > utc_now:

I found it, after login into docker container, the file location is at usr/src/homeassistant/homeassistant/components/history_stats/

thanks.

@unwasted
Copy link

unwasted commented Jun 4, 2022

Hi!

This fix is still not in dev:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/history_stats/data.py

This fixed the problem ...

Can you try this change?

diff --git a/homeassistant/components/history_stats/data.py b/homeassistant/components/history_stats/data.py
index 3f22f4cc32..6a10345df1 100644
--- a/homeassistant/components/history_stats/data.py
+++ b/homeassistant/components/history_stats/data.py
@@ -67,9 +67,10 @@ class HistoryStats:
         current_period_end_timestamp = floored_timestamp(current_period_end)
         previous_period_start_timestamp = floored_timestamp(previous_period_start)
         previous_period_end_timestamp = floored_timestamp(previous_period_end)
-        now_timestamp = floored_timestamp(datetime.datetime.now())
+        utc_now = dt_util.utcnow()
+        now_timestamp = floored_timestamp(utc_now)
 
-        if now_timestamp < current_period_start_timestamp:
+        if current_period_start > utc_now:
             # History cannot tell the future
             self._history_current_period = []
             self._previous_run_before_start = True

I'm using image homeassistant/home-assistant:stable, is there a way for me to make this change manually? I am able to login into the container by "docker exec" command but don't know where this file located.

@bdraco
Copy link
Member

bdraco commented Jun 4, 2022

@unwasted Its in 2022.6.2 via #73040

@github-actions github-actions bot locked and limited conversation to collaborators Jul 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.