Setting user presence state via external application

Tags: #<Tag:0x00007f702a714660>

(Mitch Mitchell) #1

I have a home automation system that tracks when we are at home and away. I’m looking for examples of setting users presence state from an external program – googling the questions I came up with mostly pointed at how to use presence but nothing I could find about controlling it externally.

I’m wanting to have the HA system set presence state for my wife and I so that FreePBX knows if we are home or not and can route calls accordingly.



You could use the Asterisk AMI Action DBPut to set or unset a Call Flow Control.
or do the same thing from the CLI e.g.
asterisk -rx 'database put DAYNIGHT C0 NIGHT'
which could be easily run via ssh from your HA system.

But unless you often travel separately, I don’t see how it would help much.

Calls to my number, if not answered quickly at the desk phone, start ringing other home phones and my mobile. If we’re not home, I don’t care whether the home phones ring or not. I suppose it would be better to avoid the five second delay; in rare cases when the mobile is not answered fast enough, the caller will abandon the call. That’s usually not a disaster as I just call back.

But when one person is in and one out, you could prevent calls for the one out from disturbing the one at home.

(Lorne Gaetz) #3

I have the start of a script for working with the presence state module, but ran into what I believe to be a bug:

There are also REST API methods for both the presencestate and daynight modules, tho I’m not aware of working examples to follow.

(Mitch Mitchell) #4

I cloned and fixed the bug so I could try your script. It seems to work but I’m clearly not understanding presence state and all its implications. I’ll try more tests tofay.


There is a good description of presence states: .

But IMO, you will want finer controls than presence offers. Examples:

Setting the built-in DND means your phone doesn’t ring at all. But you probably still want calls from the alarm company or fraud alerts to ring. If your kid calls, maybe an IVR “If this is urgent, press 1 to ring through, or stay on the line to leave a message.”

Likewise, turning follow-me on or off is pretty coarse. If you’re home, you may want the kitchen phone to ring if you don’t quickly answer the desk phone.

Possibly, you might want different rules for combinations of states:

  1. We’re both home.
  2. I’m home, she’s away.
  3. I’m away, she’s home.
  4. We’re both away, together.
  5. We’re both away, traveling separately.

(Mitch Mitchell) #6

My home automation system has three variables:

Me at home - true or false
Her at home - true or false
House occupied - true if one or both of us are home, false if neither of us are home.

I already have the allowlist module set up to screen calls.

Non-allowlisted callers get sent to an ant-spam IVR where they have to choose who they are calling. If they make the correct selection then the call is routed to either mine or my wife’s destination which rings all the phones in the house but when executing follow me goes to either mine or her cell phone.

What I want the system to do is recognize the status of her or my at home flags and skip ringing the house phones and go straight to follow me which will ring one of our cell phones.

The home automation system takes care of the state management but I am looking for a way of making FreePBX take it into account.

(Mitch Mitchell) #7

Trying out Lorne’s script, the device presence state changes according to the script when it reads it back but the UCP doesn’t show that, and calls still go right through rather than off to follow-me. More research needed…

I take it back – the UCP is seeing the state changes. I wonder why follow-me doesn’t pay attention even after I set the action in UCP for away to be Findme/Follow Me.

(Lorne Gaetz) #8

You might get better results by forgetting about presence and using Dynamic Routes to query the occupancy states(s) and route calls accordingly.

(Mitch Mitchell) #9

I will punt to that if I can’t get presence figured out – does presence not work correctly (other than the issue you found of course)?

(Mitch Mitchell) #10

Well well - it looks like presence states work fine – its my findme/followme I need to work on – I need to find a setting that does not ring the primary extension.

(Mitch Mitchell) #11

I finally punted to dynamic routing – Presence really seems like a weird animal and maybe a bit of a step child? I could only set the actions (e.g. the meaning) of the various states on the UCP. Seems like at least defaults should be available in the presence module.

Follow me did not work as I expected either – it didn’t seem at all sensitive to state of Away though it did DND did seem to prevent any phones from ringing.