Google Fit Support

Trying to get this to work but get the 401 error posted above despite correct Client ID/Secret. Just upgraded to 90.2. Any suggestions?

Just delete the .google_fit.token file and restart home assistant, it will ask you to authorize the app and get the new access token.

Missed something here. Where should this file be? I have only added the Client ID/Secret. I also have authorized for google assistant previously.

The file should be in your Config folder.

I am using Mi Band 1s and regarding the sleep i get only one sensor called sensor.google_fit_sleep. Is this a limitation due to the Mi Band or what?

Remaining attributes are stored as device state attributes and you can access them in template sensor.
Please refer to the template sensor definition here

- platform: template sensors: hemant_sleep_bedtime: friendly_name: "Hemant bedtime" icon_template: >- mdi:hotel value_template: >- {%- if states.sensor.hemant_sleep.attributes.bed_time %} {{ states.sensor.hemant_sleep.attributes.bed_time }} {% else %} {{ states.sensor.hemant_sleep.attributes.bed_time }} {%- endif %}

1 Like

How can I change this time/date from 2019-03-31 10:17:00 to 10:17:00 31-03-2019 (time first and the date in another format)?
Thanks.

Also, do you have any plans for creating custom card for Google Fit? Something like this https://github.com/ljmerza/fitbit-card

If anyone is looking for a manifest.json file to make this custom component compatible with 0.92, I created this one. Hope I got it all correct, but no errors now when I run Check Config.

{
    "domain": "https://github.com/hemantkamalakar/haconfigs/tree/master/custom_components/google_fit",
    "name": "Google Fit",
    "documentation": "https://github.com/hemantkamalakar/haconfigs/tree/master/custom_components/google_fit",
    "dependencies": [],
    "codeowners": ["@hemantkamalakar"],
    "requirements": []
  }

Wish you did this before I went through the hacky way of getting them through tasker. Do you find there is a huge difference between steps from mi fit app compared to those on Google fit?

Like this? image

I used it with a combo of Google fit and mi band but you could easily adapt it to this component. If there is enough interest I could do it?

3 Likes

@hemantkamalakar I get the following error with this component using 0.91.4

google_fit: Error on device update!
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 261, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 379, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/util/__init__.py", line 224, in wrapper
    result = method(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/util/__init__.py", line 224, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/google_fit/sensor.py", line 615, in update
    bed_time = datetime.fromtimestamp(round(min(starts) / 1000))
ValueError: min() arg is an empty sequence

I get the same error when I used the code from @hemantkamalakar, so instead I use the other version from @Michal_Ciemiega and works fine (but no sleep data). However I think the steps sensor is updating haphazardly. During the day, it goes +4 hours without update. Is that normal?

me to :neutral_face:

Same! It doesn’t work anymore.

How come this post doesn’t get any update? It needs to be fixed :frowning:

Regret the delay in reply. I have now pushed the fix. It now supports multiple Google accounts. Google_fit custom component
However I have noticed that MI fit app is only sending sleep start and end time. Earlier it was sending all sleep sessions and using those sessions we were able to calculate Deep and light sleep. This is what I have received for today.

{
   "id": "Sleep1563730380000",
   "name": "Sleep",
   "description": "",
   "startTimeMillis": "1563730380000",
   "endTimeMillis": "1563754980000",
   "modifiedTimeMillis": "1563797759537",
   "application": {
    "packageName": "com.xiaomi.hm.health"
   },
   "activityType": 72
  }

Now we are only able to get Bed time and wakeup time.

2 Likes

Thanks dude, I really appreciate.

But I can’t get it to work. The sensors doesn’t appear.

Same for me. There are a few errors in the logs:

google_fit: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 261, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 380, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 224, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 224, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/google_fit/sensor.py", line 612, in update
    sleep_dataset =  self._client.users().sessions().list(userId='me',fields='session',startTime=starttime,endTime=endtime).execute()
AttributeError: 'NoneType' object has no attribute 'users'
Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 365, in _async_add_entity
    await entity.async_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 226, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 256, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/config/custom_components/google_fit/sensor.py", line 286, in state_attributes
    ATTR_LAST_UPDATED: self.last_updated,
  File "/config/custom_components/google_fit/sensor.py", line 263, in last_updated
    return utc_from_timestamp(self._last_updated)
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 99, in utc_from_timestamp
    return UTC.localize(dt.datetime.utcfromtimestamp(timestamp))
TypeError: an integer is required (got type str)

By creating an empty init.py, it seems to go further but I now get a: Authentication expired, please reboot Home Assistant

I’m able to authenticate OK - I got the notification with the key to copy and paste into Google. When I reboot after that, I get the messages seen above.

Have you tried deleting the token file (mentioned above)?