jason0x43 / hacs-hubitat

A Hubitat integration for Home Assistant
MIT License
198 stars 48 forks source link

Owntracks Hubitat devices do no update their state on HA automatically #252

Closed jiipee-blob closed 6 months ago

jiipee-blob commented 8 months ago

I'm running latest version of integration: image

I noticed same issue that I reported before with life360 presence sensors. Status or any information did not update automatically. If presence sensor's status, location, etc changed nothing happened until I manually went to HA, opened Hubitat integration and run/update integration again. All other devices like door sensor's and stuff worked normally and information updated automatically.

That was fixed with newer update of your integration. I never understood what was the issue and I'm guessing none of us did.

Now I'm having exact same issue with Owntracks presence sensors. Information can be updated if I update HA hubitat integration manually. Information doesn't update automatically and again I'm wondering what should I do. Last time I enabled integration logs and they did not reveal anything special. There was no log lines when device changes its location so for me it seems like these device changes do not update at all to HA side using this integration.

Here's attribute of "last location change time" image

And here is Hubitat side: image

And if I run this in HA: (yellow line and yeah.. language is Finnish.. ) image

and after that HA shows: image

Any ideas what to do?

jason0x43 commented 8 months ago

If you enable debug logging for the integration, the log will show when state updates are received from Hubitat, like:

2024-01-15 12:56:59.292 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'sunrise', 'value': 'true', 'displayName': 'sunrise', 'deviceId': None, 'descriptionText': None, 'unit': None, 'type': 'API', 'data': None}

If events are showing up in the log for the device but entities aren’t updating in HA, there’s an issue in the integration. If events for some devices are showing up in the log but not events for other devices, then Hubitat likely isn’t sending updates to the integration for those devices. If no events from Hubitat are showing up in the log, then there may be a configuration issue with the integration.

Reloading the integration will update device states because the integration requests the current state of all devices during startup. After startup, updates are pushed from Hubitat to HA. If the integration isn’t configured properly, or if the HA machine isn’t directly reachable by Hubitat, Hubitat won’t be able to push events to HA.

jiipee-blob commented 8 months ago

Hi,

I'm a little stupid with this log thing but maybe I found something. Or did I?

ValueError: Sensor sensor.owntracks_jani_battery has device class 'battery', state class 'measurement' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Battery 75%' (<class 'str'>)
2024-01-17 17:44:30.977 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'accuracy', 'value': '29', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:44:30.978 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating accuracy of 1851 to 29 (None)
2024-01-17 17:44:30.979 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback Hub._process_event({'content': {'data': None, 'descriptionText': None, 'deviceId': '1851', 'displayName': 'OwnTracks - Jani', ...}})
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 644, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: 'Battery 75%'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/config/custom_components/hubitat/hubitatmaker/hub.py", line 328, in _process_event
    listener(evt)
  File "/config/custom_components/hubitat/hub.py", line 475, in handle_event
    listener(event)
  File "/config/custom_components/hubitat/device.py", line 171, in handle_event
    self.update_state()
  File "/config/custom_components/hubitat/device.py", line 176, in update_state
    self.async_schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1181, in async_schedule_update_ha_state
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 945, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1066, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1003, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 951, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 646, in state
    raise ValueError(

This is what it logs and a lot.

jason0x43 commented 8 months ago

Thanks, that’s helpful! So it looks like that service is reporting its battery value in a weird way, but one that can be worked around.

Are other attributes for the device being updated? It looks like there’s an accuracy attribute that was handled properly.

jiipee-blob commented 8 months ago

Yes it is also reporting for example longitude:

ValueError: Sensor sensor.owntracks_jani_battery has device class 'battery', state class 'measurement' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Battery 76%' (<class 'str'>)
2024-01-17 17:09:52.662 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'lon', 'value': 'XX.XXXXXXXXXXXX', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:09:52.663 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating lon of 1851 to 24.6824346 (None)
2024-01-17 17:09:52.664 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback Hub._process_event({'content': {'data': None, 'descriptionText': None, 'deviceId': '1851', 'displayName': 'OwnTracks - Jani', ...}})

Also altitude:

ValueError: Sensor sensor.owntracks_jani_battery has device class 'battery', state class 'measurement' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Battery 76%' (<class 'str'>)
2024-01-17 17:09:52.832 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'altitude', 'value': '73', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:09:52.832 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating altitude of 1851 to 73 (None)
2024-01-17 17:09:52.834 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback Hub._process_event({'content': {'data': None, 'descriptionText': None, 'deviceId': '1851', 'displayName': 'OwnTracks - Jani', ...}})
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 644, in state
    numerical_value = float(value)  # type:ignore[arg-type]

And for example Distance from home:

ValueError: Sensor sensor.owntracks_jani_battery has device class 'battery', state class 'measurement' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Battery 76%' (<class 'str'>)
2024-01-17 17:09:53.231 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'distanceFromHome', 'value': '0.439', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:09:53.232 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating distanceFromHome of 1851 to 0.439 (None)
2024-01-17 17:09:53.233 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback Hub._process_event({'content': {'data': None, 'descriptionText': None, 'deviceId': '1851', 'displayName': 'OwnTracks - Jani', ...}})
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 644, in state
    numerical_value = float(value)  # type:ignore[arg-type]

So yes all attributes are trying to update but they all end up to errors.

jason0x43 commented 8 months ago

The attributes other than battery look like they're updating properly, like:

2024-01-17 17:09:52.662 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'lon', 'value': 'XX.XXXXXXXXXXXX', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:09:52.663 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating lon of 1851 to 24.6824346 (None)

The 'lon' event was received, and the lon attribute of the device was updated, so the lon entity for the device should be updating (if it's enabled).

jiipee-blob commented 8 months ago

Longitude is not enabled but for example location is:

ValueError: Sensor sensor.owntracks_jani_battery has device class 'battery', state class 'measurement' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Battery 76%' (<class 'str'>)
2024-01-17 17:15:01.940 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'location', 'value': '1.0 km from Home', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:15:01.943 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating location of 1851 to 1.0 km from Home (None)
2024-01-17 17:15:01.945 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback Hub._process_event({'content': {'data': None, 'descriptionText': None, 'deviceId': '1851', 'displayName': 'OwnTracks - Jani', ...}})
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 644, in state
    numerical_value = float(value)  # type:ignore[arg-type]

Here's the lasLocationtime attribute in the log:

2024-01-17 17:24:39.243 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Received event: {'name': 'lastLocationtime', 'value': 'Wed 5:24 PM 2024-01-17', 'displayName': 'OwnTracks - Jani', 'deviceId': '1851', 'descriptionText': None, 'unit': None, 'type': None, 'data': None}
2024-01-17 17:24:39.243 DEBUG (MainThread) [custom_components.hubitat.hubitatmaker.hub] Updating lastLocationtime of 1851 to Wed 5:24 PM 2024-01-17 (None)
2024-01-17 17:24:39.245 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback Hub._process_event({'content': {'data': None, 'descriptionText': None, 'deviceId': '1851', 'displayName': 'OwnTracks - Jani', ...}})

These are all the enabled entities at the moment and none of them are working :( image

jason0x43 commented 8 months ago

Hmmm...it looks like the location event was received and the state of the location attribute was updated in HA (the Updating location of 1851 to 1.0 km from Home (None) bit). Possibly the integration isn't notifying HA about the updated attribute, though.

jiipee-blob commented 8 months ago

Yep and this is what happened with life360 too. Then there was new update for the integration and that did something because everything started suddenly working. I'm guessing there wasn't anything special fixed for the life360 issue I was having. Update still somehow did something..

jason0x43 commented 8 months ago

Try updating to v0.9.21.

jiipee-blob commented 8 months ago

That did it...again. 🙄 Is this something that we should understand?

jason0x43 commented 8 months ago

Are things working now, then?

jiipee-blob commented 8 months ago

Yes. Everything is now working and all attributes are updating. I think we should remember this next time when someone adds presence sensors from Hubitat owntracks or life360 to HA.

By the way. Do you know where does this change happen? In Hubitat side attribute presence is either present or not present. image

In HA side it is connected or not connected. image

So normally presence is usually present or not present. This is how it works on hubitat. In HA life360 and owntracks presence sensors changes somehow to connected or not connected. It's weird to say that "Jani is not connected" when it means "Jani is not present" :)

jason0x43 commented 8 months ago

I think we should remember this next time when someone adds presence sensors from Hubitat owntracks or life360 to HA.

The issue was that the Life360 app on Hubitat doesn’t follow the Hubitat spec for battery reporting. A battery property is supposed to be a number (like 76), and that’s what the integration expects, but the Life360 app is reporting it as a string (like ”Battery 76%”). The integration has been updated with a workaround, so future users won’t run into this issue.

In HA side it is connected or not connected.

The integration is assigning the wrong device class to the sensor. If you want to post the device capabilities here I can take a look at what it’s working with.

In HA, a presence sensor is a type of binary sensor, so its value in HA is only ever on or off. Home Assistant decides how to present the sensor depending on the sensor’s device class. Now that I look at it, the integration is doing something a bit weird with presence sensors that will make some of them show up as connectivity sensors, so that needs an update. 🙂

jiipee-blob commented 8 months ago

Heres capabilities of the owntrack presence sensor.

[{"capabilities":["Actuator","PresenceSensor",{"attributes":[{"name":"presence","currentValue":"not present","dataType":"ENUM","values":["present","not present"]}]}]}]

And here is life360 presence sensor. [{"capabilities":["Actuator","PowerSource",{"attributes":[{"name":"powerSource","currentValue":"BTRY","dataType":"ENUM","values":["battery","dc","mains","unknown"]}]},"Refresh","Battery",{"attributes":[{"name":"battery","currentValue":"55","dataType":"NUMBER","values":null}]},"ContactSensor",{"attributes":[{"name":"contact","currentValue":"closed","dataType":"STRING","values":null}]},"AccelerationSensor",{"attributes":[{"name":"acceleration","currentValue":"active","dataType":"STRING","values":null}]},"Switch",{"attributes":[{"name":"switch","currentValue":"on","dataType":"ENUM","values":["on","off"]}]},"Sensor","PresenceSensor",{"attributes":[{"name":"presence","currentValue":"not present","dataType":"ENUM","values":["present","not present"]}]}]}]

jiipee-blob commented 8 months ago

@jason0x43 did you have a time to check if there is something that could be done with capabilities (Present is Connected in HA, and Not Present is Disconnected.

jason0x43 commented 8 months ago

Sorry, I lost track of this. From looking at the device capabilities, I think this should be straightforward enough to deal with. I’ll try to get an fix out this weekend.

jiipee-blob commented 8 months ago

Thanks, I really appreciate that!!

jason0x43 commented 8 months ago

This should be fixed in v0.9.22.

jiipee-blob commented 8 months ago

Cool. I'll update as soon as you publish. Great job and thanks already!! MVP!

jason0x43 commented 8 months ago

It's published

jiipee-blob commented 8 months ago

Confirm it's working. Shows "Home" when I'm home.. and let's see tomorrow if it's "Away" when I'm away ;)