I have a python script
#!/usr/bin/pythonw
import requests as req
import sys
import subprocess
from googleapiclient.errors import HttpError
from googleapiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
service, flags = sample_tools.init(
"", 'analytics', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/analytics.readonly')
realtime = service.data().realtime().get(ids="ga:XXX",metrics="rt:activeUsers").execute()
print realtime
that gives back my Google Analytics Real Time Data, the result looks like that:
{u'kind': u'analytics#realtimeData', u'totalResults': 0, u'totalsForAllResults': {u'rt:activeUsers': u'0'}, u'columnHeaders': [{u'dataType': u'INTEGER', u'columnType': u'METRIC', u'name': u'rt:activeUsers'}], u'query': {u'metrics': [u'rt:activeUsers'], u'max-results': 1000, u'ids': u'ga:XXX'}, u'profileInfo': {u'webPropertyId': u'UA-XXX', u'internalWebPropertyId': u'XXX', u'tableId': u'realtime:XXX', u'profileId': u'XXX', u'profileName': u'Alle Websitedaten', u'accountId': u'XXX'}, u'id': u'https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:XXX&metrics=rt:activeUsers', u'selfLink': u'https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:XXX&metrics=rt:activeUsers'}
Now I want to filter from this the value of u'totalResults':
and send it to my Home Assistant server as a sensor
I tried
headers = {'x-ha-access': 'XXX', 'content-type': 'application/json'}
url = 'http://MYHASSIO:8123/api/states/sensor.google_real_time'
data = '{"state": "realtime"}'
response = req.post(url, headers=headers, data=data)
but the sensor value is just ‘realtime’
What do I have to add in the python file that the totalResults value send to sensor.google_real_time?