Have Home Assistant Do Something When Phone Rings (Android)

Let’s say I am listening to music or watching live TV in Kodi. I get a phone call in my Android smartphone and I want Home Assistant to do something, such as pausing music or live TV, flash a couple of lights, and send a voice notification saying that I have an incoming phone call.

I’ve been thinking – that to pull this off, I’ll need to use REST API and Tasker. My theory is this: when a phone rings, Tasker will send a message through cURL to Home Assistant. Once Home Assistant senses that a phone is ringing, Home Assistant can do the rest via automation.

Although I do have experience with Asterisk, I don’t think setting up Simonics SIP Gateway Trunk and configuring Asterisk to connect to that trunk will work since Project Fi will no longer work with Google Voice as I have my phone number ported to Fi By “experience with Asterisk,” I meant modifying sip.conf and extensions.conf config files by hand, although my memory has gotten a bit rusty.

So with that in mind, is this possible for turning my theory into practice with just Tasker?

Just because Fi makes GV no longer usable doesn’t mean you can’t have GV ring another number. You can set up Google Voice / Fi ring another number in addition to your Fi number. There are free or very inexpensive SIP numbers you can use. Callcentric has some free New York numbers (free inbound maybe). You can set up a SIP client on Android to accept the call. and then do something with Tasker. Perhaps something with Asterisk would be easy enough as well.

I’ve used CallCentric in the past. Will try to get a New York number and get Google Voice forwarded to that number and put to ti use in Asterisk. Will try it tomorrow.

I have it running with Tasker: I added an input_boolean to hass, to track if I’m in call or not, and use this toggle for automation (pausing/resuming music, mute AVR…). I’ll assume you already have two tasks named “HA: enable call” and “HA: disable call” toggling this input_boolean.

I added three profiles (I’m not sure of the exact name, my Tasker is in french):

  1. Events -> Phone ringing, calling HA: enable call
  2. Events -> Phone answered, calling HA: enable call
  3. Events -> Phone idle, calling HA: disable call

With those three events tracked, I never noticed any incoherent state.

4 Likes

Okay. I’ve been making progress but haven’t gotten into Tasker yet.

input_boolean.yaml:

phone_in_progress: name: "Phone Call In Progress" initial: off

configuration.yaml:

[code]automation:

  • alias: “Incoming Phone Call”
    trigger:
    platform: state
    entity_id: input_boolean.phone_in_progress
    state: “on”
    action:
    service: script.turn_on
    entity_id: script.phone_in_progress
  • alias: “Phone Call Complete”
    trigger:
    platform: state
    entity_id: input_boolean.phone_in_progress
    state: “off”
    action:
    service: script.turn_on
    entity_id: script.phone_progress_complete[/code]

Now here’s the fun part:

phone_incoming: alias: "Incoming Phone Call" sequence: - service: input_boolean.turn_on data: entity_id: input_boolean.phone_in_progress - service: light.turn_on data: entity_id: group.all_lights brightness: 255 - delay: seconds: 2 - service: light.turn_on data: entity_id: group.all_lights brightness: 32 - delay: seconds: 1 - service: light.turn_on data: entity_id: group.all_lights brightness: 192 - delay: seconds: 2 - service: light.turn_on data: entity_id: group.all_lights brightness: 32 - delay: seconds: 1 # repeating... phone_answered: alias: "Phone answered" sequence: - service: script.turn_off data: entity_id: script.phone_incoming - service: light.turn_on data: entity_id: group.all_lights brightness: 128 phone_in_progress: alias: "Phone Call In Progress" sequence: - service: media_player.volume_mute data: entity_id: media_player.Living_Room_Receiver is_volume_muted: "true" phone_progress_complete: alias: "Phone Progress Complete" sequence: - service: script.turn_off data: entity_id: script.phone_incoming - service: media_player.volume_mute data: entity_id: media_player.Living_Room_Receiver is_volume_muted: "false"

For testing, I have activated “Incoming Phone Call” from the frontend. This turns on the input boolean’s “Phone in Progress” which then activates the associated script, thereby muting my home theater receiver. This begins the ringing mode until I activate the “phone answered” script, which stops the “Incoming Phone Call” script and the lights are set to a preset level Once done, turning off the “Phone Call In Progress” activates the “Phone Call Complete” script.

Wow. Can’t believe I’m implementing the smarts! LOL!

Well, I could probably figure things out from here with Tasker, although I’m unsure if Tasker supports cURL, but the Tasker forum would be more suitable than here, as I’ll need to figure out how to get tasks executed so long as I’m in my own Wi-Fi Network.

Thanks.

Update as of 3:54 PM EST: After I execute the “Phone Ringing” task,Tasker will immediately follow by “Phone idle;” thus, immediately triggering the “Phone Call Complete” script but it seems the input boolean for “phone call in progress” did not turn off in time. I’ve had to add a delay right before media_player.volume_mute and that’s how I know – that when I watch the frontend, “Phone Progress Complete” switches to the “on” position for a number of seconds I’ve set and then unmutes my receiver.

I think it’s Tasker that does this kind of behavior. The only thing I can rule out is this:

[code]curl -X POST -d ‘{“entity_id”: “script.phone_incoming” }’ Http://localhost:8123/api/s
ervices/script/turn_on

curl -X POST -d ‘{“entity_id”: “script.phone_answered” }’ Http://localhost:8123/api/s
ervices/script/turn_on

curl -X POST -d ‘{“entity_id”: “input_boolean.phone_in_progress” }’ Http://localhost:8123/api/services/input_boolean/turn_off[/code]

The three commands are executed as expected. If this were for real (I don’t have another phone number to test), Tasker will probably not execute the “Phone idle” immediately after “phone ringing.”

Tasker does handle POST request out of the box. In a task, add an action: in the Networking category, you’ll find a HTTP Post action. Simply fill in the different options needed (Server:Port, Path and Data. This last part would be the JSON body you used with cURL).

Thanks, but I was in the process of debugging my script and I have solved the problem from the backend side and in Tasker as well.

Task: Phone Ringing
Service: script.turn_on
Data: {“entity_id”: “script.phone_incoming”}

Task: Phone Answered
Service: input_boolean.turn_on
Data: {“entity_id”: “input_boolean.phone_in_progress”}

Task: Phone Idle
Service: input_boolean.turn_off
Data: {“entity_id”: “input_boolean.phone_in_progress”}

Both three of the tasks have a Content-Type of application/json.

Script.yaml:

# Scripts for Interrupting playback in Kodi/Resuming playback of Kodi go here... phone_incoming: alias: "Incoming Phone Call" sequence: - service: script.turn_on data: entity_id: script.phone_in_progress - service: script.turn_on data: entity_id: script.phone_incoming_kodi - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 255 - delay: seconds: 2 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 32 - delay: seconds: 1 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 192 - delay: seconds: 2 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 32 - delay: seconds: 1 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 192 - delay: seconds: 2 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 32 - delay: seconds: 1 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 255 - delay: seconds: 2 - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 32 - delay: seconds: 1 # repeat the bright-to-dim-to-bright lighting sequences for few more times. - service: script.turn_on data: entity_id: script.phone_progress_complete phone_answered: alias: "Phone answered" sequence: - service: script.turn_on data: entity_id: script.phone_in_progress - service: script.turn_off data: entity_id: script.phone_incoming - service: light.turn_on data: entity_id: light.lgRingerGroup brightness: 128 phone_in_progress: alias: "Phone Call In Progress" sequence: - service: media_player.volume_mute data: entity_id: media_player.Living_Room_Receiver is_volume_muted: "true" phone_progress_complete: alias: "Phone Progress Complete" sequence: - service: script.turn_off data: entity_id: script.phone_incoming - delay: seconds: 3 - service: media_player.volume_mute data: entity_id: media_player.Living_Room_Receiver is_volume_muted: "false" - service: script.turn_on data: entity_id: script.phone_progress_complete_kodi

Automation in configuration.yaml:

[code] - alias: “Incoming Phone Call”
trigger:
platform: state
entity_id: input_boolean.phone_in_progress
state: “on”
action:
service: script.turn_on
entity_id: script.phone_answered

  • alias: “Phone Call Complete”
    trigger:
    platform: state
    entity_id: input_boolean.phone_in_progress
    state: “off”
    action:
    service: script.turn_on
    entity_id: script.phone_progress_complete[/code]

Now to test it out…

Event: Phone Ringing -> Task: Phone Ringing
Event: Phone Offhook -> Task: Phone Answered (not sure if “off hook” is the same as “answered…”
Event: Phone Idle -> Task: Phone Idle

Okay. I test using my mom’s phone to call my phone. The way I see it, it seems “Phone Idle” gets called immediately after “Phone Ringing.” I can tell because the “Phone Incoming” script still runs and “Phone Progress Complete” script gets triggered during the phone ringing. The “Phone Ringing” event isn’t really a state that knows when the ringer is still active.

So yeah. Back to square one, I suppose…

And if anyone want the two Kodi scripts, here it is, although I don’t think this will do anyone any good.

phone_incoming_kodi: alias: "Phone ringing - interrupt Kodi playback." sequence: - condition: state entity_id: media_player.kodi state: "playing" - service: media_player.media_pause data: entity_id: media_player.kodi phone_progress_complete_kodi: alias: "Resume playback once call is cimpleted." sequence: - condition: state entity_id: media_player.kodi state: "paused" - service: media_player.media_play data: entity_id: media_player.kodi

And I’ll add an additional boolean input for controlling “phone_incoming” script, since turning off the ringing script won’t trigger the “phone progress complete” script.

So yeah, the scripts are a non-issue and I’m getting there, but not quite. The Tasker is the major issue for me and I’m wondering how I could keep the ringer state active until I answer, decline, or miss the call.