Asterisk and phone ringing based on presence with node-red

Hello everyone!

Since someone really helped me on /r/, I’d like to post here so anyone else can find it useful.
What we can do with this, is actually make the phone ring or go straight to voicemail depending on our presence. Actually you can link it to whatever you want (eg go to voicemail at night for a do-not-disturb mode) with node-red.

Info:
What this actually does is on Home Assistant we write 1 or 0 to a table on the database of the Asterisk. Then we create a route from Asterisk that checks this table when we have an incoming line and routes the line accordingly. In our case, when HA writes 1 we are home so Asterisk will ring the phone and when we’re away it will write 0 so it will go straight to voicemail which is configured to send an e-mail. Handy stuff.

Disclaimer:
With this particular setup, when you’re away the phone will never answer. This is a probable security risk for a malicious person, so you’re on your own with that. I’m just showing what can be done with this setup.

Prerequisites:
An Asterisk solution (Freepbx, Issabel etc)
A bit of knowledge with SSH since we will need to create a database.
Node-Red
Node-Red mysql node (you can install it under manage pallete on node-red)

So first we connect with Putty (or any other ssh method you like) to asterisk (username should be root and password the password you login to Asterisk) and:

mysql -p

It will ask for a password and connect. Then we create the DB with

CREATE DATABASE test;
exit;

That’s it for the SSH part.
Moving on to the GUI of Asterisk, we go to Dynamic Routing, click Add routing and fill the blanks. Source type is MySQL, Host will be local so 127.0.0.1, database is test (or what name you gave before), username will be root (unless you have knowledge and already created another one for your database), password would be the password you entered earlier and Query will be

SELECT occupancy from hassio

Below that would be the match section. The first is the default. The second you write 1 on the first box and select extensions -> your extension and on the third you write 0 and select voicemail -> your voicemail.
Click save and apply config. Now go to your inbound routes and select the Dynamic route as destination. That’s it from asterisk.

Ok moving on to home assistant. Go to Node-red and add a function(you can search it on the left). Inside the fuction add:

msg.topic = "DELETE FROM hassio";
msg.payload = [ ""];
return msg;

Note: hassio is the table name we also use in asterisk. So if you named it something else (eg. whatever, you would write DELETE FROM whatever)
Now add a mysql node and link it after the function.
Then we create another function where we write:

msg.topic = "INSERT INTO hassio (occupancy) VALUES (?)";
msg.payload = [ "1"];
return msg;

And link it to the end of the mysql node.
Again, hassio and occupancy are the names we also use in Asterisk, so work accordingly.
msg.payload which is 1 will be the checkpoint. If you send 1 then we are home and will ring, if you end 0 it will go to voicemail.
Last, we add the final mysql node and link it on the end of the function.
You see that first I delete the table and then write the checkpoint. The reason is that since it’s a table, anything you send will fill up lines instead of replacing the current value, and the Asterisk only checks the first one ever sent, so it’s a quick n dirty method that worked.
That’s it. From there on, the sky is the limit. You could do a number of things with this method apart from what I posted.

If you have any problems with accessing mysql db or ssh, you should read a bit more on google. I did the same and worked out fine.
Hope I helped!