Running custom routines on companion app

HA is one of those few self hostable apps that is always synced across all devices.

Is it possible to make it run some custom code/routines on a specific device?

My use case is to toggle whether my android phone charges its battery to 70% or 100%. The ACCA app takes care of this, I just need HA to run some custom bash commands.

I do this to maximize life span its lithium battery.

if you can hook it into tasker or using the android intent system this might be achievable…youll need to do some research to see what is possible with that app.

I use macrodroid to recieve notifications from HA and perform actions based on that. Ie turn on/off bluetooth on mine or wifes phones.

You can send notification via service to any phone device that has HA app and then use content of notification as trigger for macro.

Instead of relying on a notification you can also use an intent to send more custom data.

What I’m asking in my OP is, can companion app send out a signal?

So now I know it can send out a notification. How about intent?

Ok, can a notification be sent to only 1 companion app? Or it will show up on all devices?

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

Yes. You will have a notify service for each device with mobile app on. Ie

service: notify.mobile_app_sm_s901b
data:
  message: Turn off bluetooth

And then a macrodroid macro

EDIT: btw i used this method for bluetooth because the HA app cannot control BT on android 13 and avove but Macrodroid can. Agree with others you can also use intents for your use case.

@msp1974 thanks, stuff got in the way. But I eventually managed to setup your method.

@dshokouhi
Is there a way to run an su command without tasker?

pretty sure you will need an automation tool like tasker to do that

@dshokouhi

I was looking at RUN_COMMAND Intent · termux/termux-app Wiki · GitHub

Would you know how to interpret that example into HA?

its pretty straight forward, look at each variable like Class Name, Action and Extras and translate them to the examples from https://companion.home-assistant.io/docs/notifications/notification-commands#activity

Since finding the links, I have read and tried, but still could not bridge them

mobile_action:
  friendly_name: "Mobile Action"
  turn_on:
    service: notify.mobile_app_XXXX
    data:
      message: "command_broadcast_intent"
      data:
        intent_package_name: "com.termux"
        intent_action: "com.termux.RUN_COMMAND"
        intent_extra: "su -c 'mycommand param1 param2'"

This command format does not match what you linked me to. The intent extra is not setup properly either its missing the variable name, all you have as of now is variable value. The only thing correct above is the package name and intent action.

this still doesn’t work

intent_package_name: "com.termux"
intent_action: "com.termux.RUN_COMMAND"
intent_extra: "com.termux.RUN_COMMAND_PATH:/data/data/com.termux/files/usr/bin/su,com.termux.RUN_COMMAND_ARGUMENTS:-c 'command param1 param2'"

now you need the missing class name intent_class_name: om.termux.app.RunCommandService

then check companion app logs if teh issue still occurs.

I did check the logs, but there doesn’t seem to be any errors or sufficient details
The closest I saw was MessagingService: Sending broadcast intent

I updated to this, but still no effect, it was as though the command never ran

mobile_action:
  friendly_name: "Mobile Action"
  turn_on:
    service: notify.mobile_app_XXXX
    data:
      message: "command_broadcast_intent"
      data:
        intent_package_name: "com.termux"
        intent_class_name: "com.termux.app.RunCommandService"
        intent_action: "com.termux.RUN_COMMAND"
        intent_extra: "com.termux.RUN_COMMAND_PATH:/data/data/com.termux/files/usr/bin/su,com.termux.RUN_COMMAND_ARGUMENTS:-c 'mycommand param1 param2'"

reading further into the page, looks like termux has their own custom permission they are expecting the app to request. Unfortunately this requires the app to hardcode an app specific permission and not a general android permission so the app will not have support for the custom permission.

Judging by teh above link you’ll probably need to link tasker to termux for this.

I still can’t find a way to run commands in tasker or termux. The integration found here GitHub - lone-faerie/taskerha: Tasker custom component for Home Assistant doesn’t quite cut it since it only works when the android device is on the same network as HA.

But I thought it might be useful for the companion app to add a custom sensor/switch, i.e. we can configure it to call (root) commands to retrieve values that are send back to HA as a sensor, and have a switch that runs a ON and OFF command.

Sounds very doable since there is already a launch app command Notification Commands | Home Assistant Companion Docs

Correct?