Android Intents - Sending & Receiving List (Starting Activities too!)

Hey Everyone,

We have added a few features that are currently available in the latest Beta version of the app, if you don’t see some of the below features check that you have the latest beta per GitHub. As of this post you want to be on at least beta-573. There are a couple of features that are currently in beta and also been released to help you further integrate other apps from your android phone.

For those who are new an Intent is something that Android uses to allow the system and other apps to communicate to each other. Not all apps supports these features let alone have them documented. The purpose of this post to help consolidate a list of those so users can see all that is possible.

I chose a forum post so we can keep a discussion and so I can update the list of links to help everyone out. Just remember there is no easy answer as to what to do for an app, we have to get the proper data to build out the command. The docs provide a real world example for you to use and get familiar with.

So to get started you will need to familiarize yourself with the documentation around this. To send an intent from HA to another app you will need to follow command_broadcast_intent.

https://companion.home-assistant.io/docs/notifications/notification-commands#broadcast-intent

To add on to this you can also launch an activity using command_activity

https://companion.home-assistant.io/docs/notifications/notification-commands#activity

To receive an intent for another app you will need to make use of the Last Update Sensor as it has settings to allow you to register for any intent. Long story short go to App Configuration > Manage Sensors > Last Update Sensor > Toggle add intent (wait a few seconds for the page to update and the toggle to turn off) > Select the new item that just appeared and clear out the field, then add in the intent string you wish to register for > Repeat last 2 steps for each and every intent you wish to register for. Once you are done adding intents, force stop the app and open it again so the app can register for them.

https://companion.home-assistant.io/docs/core/sensors#last-update-trigger-sensor

Once you register for the intent and restarted the app you will then need to listen for the android.intent_received event

https://companion.home-assistant.io/docs/integrations/app-events

To get you started here is an example of how to listen for events in an automation and notify you when its been received. The below will send a notification with the person who fired the event and the intent that was sent with the event data.

- alias: Intent received
  trigger:
  - platform: event
    event_type: android.intent_received
  action:
  - service: notify.mobile_app_dannys_pixel_4_xl
    data_template:
      message: "Intent received by user {% set person = states.person|selectattr('attributes.user_id', 'eq', trigger.event.context.user_id)|first %}{{ person.attributes.friendly_name }} with action {{ trigger.event.data.intent }}"
      data:
        ttl: 0
        priority: high

Now here is the consolidated list:

As you can see a lot of this is going to be trial and error. Again the docs have some good examples to show you how to translate things from what you find online (refer to the example and provided links to make the connection). If you are stuck on a command make sure to give us the full example you tried so we can help out :slight_smile: Don’t be afraid to reach out to your favorite app developers asking if they support these things or if they can add them, you will be surprised how many already do or will add it for you.

Looking forward to all of the use cases everyone is going to come up with!

Personally I am doing a few things thus far:

  • Mi Band and Fitness to send intents to track steps, heart rate, falling asleep and waking up (there is more too just peep the link up above). Just started using these so need to find a good use case but cool nonetheless!
  • Sleep as Android, starting sleep tracking when scene.goodnight is ran. Reacting to sleep tracking turning off. I use the start and stop sleep tracking events to automate armed_night as it makes life easier. Also the stop tracking event is useful if you like to have a script ran after you wake up.
14 Likes

I can’t get this to work.
Long story short, this thread made me think about the intent listener.
So I added android.bluetooth.device.action.BATTERY_LEVEL_CHANGED to listen for and added the automation as above.

I see this very briefly in states:

And the app has this setting:

And automation is this:

alias: New Automation
description: ''
trigger:
  - platform: event
    event_type: android.intent_received
    context:
      user_id:
        - 1ebf075a6b72488494cbf1bc09bfadc6
        - afc9fe441ceb4b468205fd29fdaa42ac
condition: []
action:
  - service: notify.mobile_app_andreas
    data:
      message: >-
        Intent received by user {% set person =
        states.person|selectattr('attributes.user_id', 'eq',
        trigger.event.context.user_id)|first %}{{
        person.attributes.friendly_name }} with action {{
        trigger.event.data.intent }}
      data:
        ttl: 0
        priority: high
mode: single

And it never fires…

I have force closed the app and restarted it.
I have also tried without the context of trigger, which I assume means trigger on all events?
Node red does not either notice any events.

Try listening for the event manually in dev tools, is it even firing?

No…
I tried to listen for the event.

Nothing happened. Then I fired the event from the dev tools and it got picked up both in dev tools and in node red.
So apparently the app doesn’t send the events… ?

check logcat, see if your device is even sending the intent.

You should also see an attempt to send the event or a message saying it failed

I have some custom intents for my mi band that work well

I don’t have any “event successfully…” but I have a few like this:

02-28 18:11:54.002 25994 26039 E SensorReceiver: Unable to send event data to Home Assistant

I tried to use the mobile app share, and the event was sent using that method from waze.

What’s the full error

I believe that was the full line.
I’ll have a look in a few minutes, putting the kids in bed at the moment

we print an exception directly below it, it contains exactly what the error is. Honestly if its failing here it usually suggests a connection error to HA. The HA error logs may be of help too. All this call does is send over the data to HA.

02-28 19:24:36.385 16880 17035 E SensorReceiver: Unable to send event data to Home Assistant
02-28 19:24:36.385 16880 17035 E SensorReceiver: io.homeassistant.companion.android.common.data.integration.IntegrationException
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at io.homeassistant.companion.android.common.data.integration.impl.IntegrationRepositoryImpl.fireEvent(IntegrationRepositoryImpl.kt:263)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at io.homeassistant.companion.android.common.data.integration.impl.IntegrationRepositoryImpl$fireEvent$1.invokeSuspend(Unknown Source:12)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
02-28 19:24:36.385 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
02-28 19:24:36.388 16880 17035 E SensorReceiver: Unable to send event data to Home Assistant
02-28 19:24:36.388 16880 17035 E SensorReceiver: io.homeassistant.companion.android.common.data.integration.IntegrationException
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at io.homeassistant.companion.android.common.data.integration.impl.IntegrationRepositoryImpl.fireEvent(IntegrationRepositoryImpl.kt:263)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at io.homeassistant.companion.android.common.data.integration.impl.IntegrationRepositoryImpl$fireEvent$1.invokeSuspend(Unknown Source:12)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
02-28 19:24:36.388 16880 17035 E SensorReceiver: 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
02-28 19:24:36.462 16880 16920 D LightSensor: Light sensor listener registered
02-28 19:24:36.465 16880 16880 D LightSensor: Light sensor listener unregistered

I can’t see anything in HA logs that seems relevant.
This is the same-ish time in HA logs.

2021-02-25 09:23:27 WARNING (MainThread) [homeassistant.components.http.ban] Login attempt or request with invalid authentication from localhost (127.0.0.1) (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68)
2021-02-25 09:23:35 WARNING (MainThread) [homeassistant.components.upnp] Wanted UPnP/IGD device with UDN/ST "uuid:61c9066f-7d3a-4992-8630-2d0e19d4344d"/"urn:schemas-upnp-org:device:InternetGatewayDevice:2" not found, aborting
2021-02-25 09:23:35 WARNING (MainThread) [homeassistant.config_entries] Config entry for upnp not ready yet. Retrying in 80 seconds
2021-02-25 09:23:35 WARNING (MainThread) [homeassistant.components.upnp] Wanted UPnP/IGD device with UDN/ST "uuid:1ec61488-f33f-a454-3310-782650e8b914"/"urn:schemas-upnp-org:device:InternetGatewayDevice:1" not found, aborting
2021-02-25 09:23:35 WARNING (MainThread) [homeassistant.config_entries] Config entry for upnp not ready yet. Retrying in 80 seconds
2021-02-25 09:24:35 ERROR (SyncWorker_13) [custom_components.ical.sensor] RRULE UNTIL values must be specified in UTC when DTSTART is timezone-aware
2021-02-25 09:24:35 ERROR (SyncWorker_13) [custom_components.ical.sensor] RRULE UNTIL values must be specified in UTC when DTSTART is timezone-aware
2021-02-25 09:25:05 WARNING (MainThread) [homeassistant.components.upnp] Wanted UPnP/IGD device with UDN/ST "uuid:61c9066f-7d3a-4992-8630-2d0e19d4344d"/"urn:schemas-upnp-org:device:InternetGatewayDevice:2" not found, aborting
2021-02-25 09:25:05 WARNING (MainThread) [homeassistant.config_entries] Config entry for upnp not ready yet. Retrying in 80 seconds
2021-02-25 09:25:05 WARNING (MainThread) [homeassistant.components.upnp] Wanted UPnP/IGD device with UDN/ST "uuid:1ec61488-f33f-a454-3310-782650e8b914"/"urn:schemas-upnp-org:device:InternetGatewayDevice:1" not found, aborting
2021-02-25 09:25:05 WARNING (MainThread) [homeassistant.config_entries] Config entry for upnp not ready yet. Retrying in 80 seconds
2021-02-25 09:26:35 WARNING (MainThread) [homeassistant.components.upnp] Wanted UPnP/IGD device with UDN/ST "uuid:61c9066f-7d3a-4992-8630-2d0e19d4344d"/"urn:schemas-upnp-org:device:InternetGatewayDevice:2" not found, aborting

Can you try another intent? I just checked and mine are still working for my mi band. May need to add some debug logging in the app to see what’s up with this intent in particular.

I tried with screen on and off. And I can listen for them manually, but I didn’t get the notification from the automation.

But the automation is triggered.
image

Alarm change also worked fine.
The issue with the automation seems to be the person = part.
But that isn’t very important to me, I can live without this automation.

I tried with
android.bluetooth.device.action.ACL_CONNECTED
Since that is the intent that is used to lock and unlock our door.
But it didn’t work either.

Found the issue, need to send all the extra data as a string to properly process it. Will submit an update for this as well as some debug logging to troubleshoot in the future.

PR to correct the issue: https://github.com/home-assistant/android/pull/1386

1 Like

Having MQTT for Sleep as Android is epic. I can’t wait to implement some handy automations based on this. Reading the app docs it looks pretty powerful.

@dshokouhi would you mind sharing how to receive the MQTT sleep_tracking_started from the app output? MQTT isn’t something I’ve had much experience with. Do I just create a MQTT switch entity? Or is there a nicer way? As I’d also like to integrate the sleep_tracking_stopped event.

for MQTT it will all come in under the same topic that you define in the sleep as android app

For our app you can register with the intents directly if you want. Personally I just use their own webhook support as my MQTT server is local only.

I was reading that page to see what the app can output. My question was more for the HA side as to how I receive those MQTT messages (how to write the YAML for a sensor/ switch). Not really on the topic of this thread, I apologise.

A lil bit off topic but all good, I think this may be right up your alley https://github.com/IATkachenko/HA-SleepAsAndroid

1 Like

Hi,

I am having difficulty getting the service notify_mobile_app_ to launch any android app on my phone.
I have tied both command_broadcast_intent and comand_activity with no success, the closest I get is with command_activity where the HA app displays a fleeting error “unable to send activity intent, pleas check command format”

I specifically want to launch Spotifyon its home page and possibly Alexa, again on the home page.

I think the intent for Spotify is com.spotify.music and the activity is MainActivity
But I have had no success. This is the closest I’ve got.

service: notify.mobile_app_<mymobile>
data:
  message: command_activity
  data:
    channel: com.spotify.music.MainActivity
    tag: andriod.intent.VIEW

Can anyone help me? What am I doing wrong?

Thanks

your tag is incorrect and also misspelled

should be tag: "android.intent.action.VIEW"

also channel is meant to be the package itself and not an activity as you have it so remove the .MainActivity part

check the main post for the spotify link to make things a little easier to decipher, you may also want to refer to the docs linked in the first post too.