core icon indicating copy to clipboard operation
core copied to clipboard

state_class missing for Android battery sensors after Home Assistant core update or restart

Open J3173 opened this issue 1 year ago • 25 comments

The problem

The state_class attribute for the battery_level, battery_temperature and battery_power sensors of my Android phone has disappeared since Home Assistant 2022.12.

This causes the sensor data to be now longer recorded into the long term statistics.

afbeelding

I have confirmed that the state_class attribute reappears when downgrading Home Assistant to 2022.11.5.

What version of Home Assistant Core has the issue?

core-2022.12.1

What was the last working version of Home Assistant Core?

core-2022.11.5

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Mobile App

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

J3173 avatar Dec 10 '22 14:12 J3173

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (mobile_app) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mobile_app can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign mobile_app Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


mobile_app documentation mobile_app source (message by IssueLinks)

home-assistant[bot] avatar Dec 10 '22 14:12 home-assistant[bot]

Sounds like a duplicate of https://github.com/home-assistant/core/issues/71065

Did you wait up to 4 hours after the upgrade? What we discovered in the other bug is that upon a HA core update the state class goes missing until the app re-registers the sensors which happens about 4 hours after the update. It can happen anytime between the update and 4 hours so its possible you didnt wait long enough for that to happen.

In order to continue here we will need the logs as mentioned in the other issue.

https://github.com/home-assistant/core/issues/71065#issuecomment-1113657883

dshokouhi avatar Dec 10 '22 17:12 dshokouhi

I didn't wait. I checked now and about half an hour after the upgrade the state class attribute appeared again.

I have enabled the debug logging. If it happens again, I'll upload the logging.

J3173 avatar Dec 10 '22 20:12 J3173

See plenty of those errors too, not limited to battery entities only, see https://github.com/home-assistant/android/issues/2847#issuecomment-1345649375.

Still on HA Core 2022.9.7. Watching this issue now as all previous ones have been closed as solved...

bcutter avatar Dec 12 '22 17:12 bcutter

Yesterday I have updated my Home Assistant to 2022.12.6 and later did a restart. It looks like the state class also disappeared after a normal restart, not only after an update. I have attached the logging and the states of the level, power and temperature entities (query: select s.last_updated, s.entity_id, s.state, sa.shared_attrs from states s join state_attributes sa on s.attributes_id = sa.attributes_id where s.entity_id = 'sensor.pixel_4a_battery_level';)

The state_class seems to disappear when the first update_sensor_states is received after an update/restart and appears again when register_sensor is received.

I suspect that self._config.get(ATTR_SENSOR_STATE_CLASS) of the sensors is not correctly restored after a restart as it is not mentioned in https://github.com/home-assistant/core/blob/cc40c6d16a09978aba5fbdcb3ded048a72a4c8c8/homeassistant/components/mobile_app/sensor.py#L48-L58

J3173 avatar Dec 18 '22 12:12 J3173

Can this please get fixed?

Suffering from this for months, being redirected from https://github.com/home-assistant/android/issues/2847#issuecomment-1346869360.

Now that the entity popup only shows statistics and not the raw data anymore, thanks to this bug NOTHING is shown for affected entities (in short: things got worse):

image

image

bcutter avatar Jan 14 '23 12:01 bcutter

I believe this is now fixed in the latest HA core release? May also need latest android app version too

dshokouhi avatar Feb 15 '23 02:02 dshokouhi

@dshokouhi this is not fixed on the latest HA core release for me

DAVe3283 avatar Feb 17 '23 18:02 DAVe3283

@dshokouhi this is not fixed on the latest HA core release for me

I think you first need to wait for the app to re-register the sensors after updating HA core and then on the next restart it should work.

dshokouhi avatar Feb 17 '23 18:02 dshokouhi

I think you first need to wait for the app to re-register the sensors after updating HA core and then on the next restart it should work.

I will try waiting for a re-register (4 hours?) then restart. But what do I restart, HA core or the android app/phone?

DAVe3283 avatar Feb 17 '23 18:02 DAVe3283

I will try waiting for a re-register (4 hours?) then restart.

might be more frequent than that, when it re-registers the sensors the error will go away in Statistics.

But what do I restart, HA core or the android app/phone?

restart HA core like this issue states :)

dshokouhi avatar Feb 17 '23 18:02 dshokouhi

Just came back for me after the latest core update - 2023.4.6.

bschatzow avatar Apr 24 '23 20:04 bschatzow

It never was gone completely. Or when should it be fixed? Still see this for few devices. E. g. for one entity:

image

image

bcutter avatar Apr 24 '23 20:04 bcutter

For a long time, including the last version I am getting the following warnings in the logs:

2023-04-24 14:52:11.795 WARNING (MainThread) [homeassistant.components.sensor] Sensor sensor.mobile_battery_state has device class battery, state class None and unit None thus indicating it has a numeric value; however, it has the non-numeric value: full (<class 'str'>); Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+mobile_app%22

I am using:

  • Home Assistant Core 2023.4.6
  • Frontend 20230411.1 - latest
  • Home assistant companion app 2023.3.0-full on Android 13

nicolaasuni avatar Apr 26 '23 14:04 nicolaasuni

Cave back on 2023.6 for me.

bschatzow avatar Jun 08 '23 16:06 bschatzow

Seeing same issue again.

Last date for getting state was just prior update: https://ss.codeone.pl/ss-2023-06-13-22-30-49-1686688249-aTMenRla.png And after that: https://ss.codeone.pl/ss-2023-06-13-22-31-26-1686688286-EfruXX0W.png

Same for battery temperature as well as other sensors from phone (OnePlus LE2123)

fliespl avatar Jun 13 '23 20:06 fliespl

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Not fixed, still exists.

bcutter avatar Sep 11 '23 22:09 bcutter

Happened to me as well. The fix was to disable/re-enable the sensor in the companion app, set the refresh time to always, and then set the refresh time to normal.

bryanyork avatar Sep 12 '23 17:09 bryanyork

Happened to me as well. The fix was to disable/re-enable the sensor in the companion app, set the refresh time to always, and then set the refresh time to normal.

I doubt it. Have you restarted HA and/or the devices meanwhile? It will come back...

bcutter avatar Sep 12 '23 18:09 bcutter

The same thing happens to me currently, both with the battery sensors of the phones and the battery and temperature sensors of the Xiaomi door sensors.

emilianogetino avatar Nov 15 '23 19:11 emilianogetino

Still happening. Noticed it just after a server reboot. Is there a workaround? Manually adding a 'state class' in? I can't find any documentation for state classes (and I'm not a HA developer).

grimreaper38uk avatar Nov 18 '23 13:11 grimreaper38uk

This has been going on for months with me. In fact, this may never have worked since I installed Home Assistant about six months ago. I'm using the latest version of Home Assistant and the Android app. Restarting core or the app does nothing.

image

dziban303 avatar Jan 10 '24 17:01 dziban303

Same issue here with some sensors of my phone and also some of my Pixel Watch 2.

endyb66 avatar Feb 01 '24 18:02 endyb66

For me it helped to set the state_class manually in the configuration.yaml. It took some time but now i have statistics for my android devices:

homeassistant:
  customize:
    sensor.redmi10_battery_level:
      state_class: measurement

83716-statistics-for-android

kmoser77 avatar Feb 06 '24 18:02 kmoser77

I have the same issue for my Lenovo tablet.

robinhood-code avatar Feb 12 '24 11:02 robinhood-code

I'm having this issue for all of my mobile devices. Clicking "Fix Issue" in Statistics clears the issue there but then I am not able to graph the battery level of my devices. The entity simply doesn't exist when I try to create a Statistics Graph for it.

I have no idea if this is related to any upgrades on my part. My last upgrade was weeks ago so any of this "wait 4 hours" suggestion is not the case here.

How do I fix all of these broken statistics without completely deleting them so that they are not even graphable any more?

brianjmurrell avatar Feb 23 '24 14:02 brianjmurrell

I just added a new mobile device and it has immediately gotten into this state:

image

No upgrade of HA, etc. Just adding a new mobile device. I don't dare click the DELETE button or else I will never again be able to add it to a statistics graph as the entity just never shows up again for that card despite the device showing data for that entity.

I did enable debug logging for homeassistant.components.mobile_app before adding this new device. What can I extract from the log to debug this issue? Is it this:

2024-02-23 10:08:47.507 DEBUG (MainThread) [homeassistant.components.mobile_app.webhook] Received webhook payload from Redmi Note 4 for type register_sensor: {'unique_id': 'battery_level', 'state': 100, 'type': 'sensor', 'icon': 'mdi:battery-charging', 'attributes': {}, 'name': 'Battery level', 'device_class': 'battery', 'unit_of_measurement': '%', 'state_class': 'measurement', 'entity_category': 'diagnostic', 'disabled': False}

I'm running: Core: 2024.1.6 Frontend: 20240104.0

brianjmurrell avatar Feb 23 '24 15:02 brianjmurrell

@brianjmurrell what app version are you running?

dshokouhi avatar Feb 23 '24 16:02 dshokouhi

@dshokouhi It should be the latest. I always keep all of my apps up-to-date. It says it's 2024.1.5-full (12102)

brianjmurrell avatar Feb 23 '24 16:02 brianjmurrell