Announce incoming calls

I’ve got a setup working such that if I get a call on my landline, the incoming number is sent by MQTT message to my HA instance.

Anyone know how I’d be able to check the incoming number against a table of known contacts and perform different actions depending on who called?

I know I could do this with about 20 different automations, but is there a smarter way?

Any thoughts on this one? It’s a bit of a weird one, maybe?!

What does the sensor that receives the phone number look like?

I have something similar for checking if an imcoming call is spam.

I have a json file which contains phone numbers with the corresponding name. When I get an imcoming call on the landline a shell_command is triggered.

shell_command:
  werbeanruf_suchen: "python3 /home/homeassistant/.homeassistant/python_scripts/werbeanrufe.py {{ rufnummer }}"

The python script searches the entries in the json file and creates a sensor if the number is found.

http://192.168.2.75:8123/api/states/binary_sensor.werbeanruf'
    header = {'content-type': 'application/json'}
    payload = {'state': 'off', 'attributes': {'friendly_name': 'Werbeanruf'}}

    if len(sys.argv) == 1:   
        post(url, headers=header, data=json.dumps(payload))     
        return

    number = sys.argv[1]

    database = '/home/homeassistant/.homeassistant/www/werbeanrufe/liste.json'
    data = json.loads(open(database).read())

    for i in data:
        if i['Nummer'] == number:
            varName = i['Anrufname']
            varNummer = i['Nummer']
            varTyp = i['Anruftyp']
            varScore = i['Score']   

            payload = {'state': 'on', 'attributes': {'friendly_name': 'Werbeanruf', 'name': varName, 'number': varNummer, 'type': varTyp, 'score': varScore}}        
            break   
    
    post(url, headers=header, data=json.dumps(payload))

It’s not very nice but it works for me.

Cool, @dennis84de , thank you, this looks like it might be the answer! Would you mind posting a (scrubbed/obfuscated) json example file? I’m not very good with json beyond the basics. Unless it would just look like the below?

[
    {
         "Name": "Tom Miller",
         
         "phones": [
             {"fax": 2015551212}
             
        ]
    },
    {
         "Name": "Bud Light",
         
         "phones": [
             {"fax": 2015551212}

        ]
    }
]

@finity at the moment, the sensorl looks like this. It’s sent by MQTT from FreePBX
dsadsa

The json file looks like this:

[
  {
    "Nummer": "03036428369",
    "Score": 8,
    "Bewertungen": 482,
    "Land": 49,
    "Vorwahl": 30,
    "Suchanfragen": 1463741,
    "Anruftyp": "Aggressive Werbung",
    "Anrufname": "DEV",
    "Vorwahlname": "Berlin",
    "Deeplink": "https://www.tellows.de/num/03036428369",
    "CallerTypeId": 5
  },
  {
    "Nummer": "02148334499",
    "Score": 6,
    "Bewertungen": 70,
    "Land": 49,
    "Vorwahl": 214,
    "Suchanfragen": 907560,
    "Anruftyp": "Meinungsforschung",
    "Anrufname": "Leyhausen",
    "Vorwahlname": "Leverkusen",
    "Deeplink": "https://www.tellows.de/num/02148334499",
    "CallerTypeId": 6
  }
]

In my case the file contains 8000 entries. So it takes a moment to search for an entry.

Maybe I could create a custom_component for it. But for now I have no time for that.

1 Like

well, i was more interested in what it looked like in the states page to see how the sensor results were formatted there.

But it looks like you might be on the right track with @dennis84de’s solution


aha! Sorry @finity, please find above

(not had a call on the Sipgate line since last reboot)

Could you just use the state of the sensor and do a comparison to a known number and then trigger an action cased on that?

something like:

trigger:
  platform: template
  value_template: "{{ is_state('sensor.last_caller_landline', '0123462333') }}"
action:
  ...

Do you actually need to pull information out of the json to do something with it?

ahhhh, I see, yes I see. Yes I could do that. I guess I’d need to make sure that the trigger only triggered once, but I can probably figure that out.

I guess I just wondered if there was a better way to do it without having lots of automations. But this seems fine!! :smiley:

Thanks

it all depends on the actions…

If each action is different depending on the caller then it might just be easier to do it with individual automations.

If each action is similar then you might be able to template it to make things cleaner.

hi there Dennis (think you might be born the same year as me!),

I’m finally getting around to trying to implement this, so far I’ve got this problem in my logs when I try to execute the script from an automation.

I wonder if it’s because my HA is HTTPS? I did modify the link in the script to https, but when I try to test it from my computer
https://192.168.1.115:8123/api/states/sensor.last_caller
I get a 401 unauthorised message so I don’t know if the link actually works. I figure if it’s the HA instance itself trying to access that link presumably it works?!

2020-04-29 22:32:38 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `python3 /home/homeassistant/.homeassistant/python_scripts/werbeanrufe.py {{ rufnummer }}`, return code: 2
NoneType: None

Finally I’m not sure what your script does with the result? It posts it, but where? For me the ideal result would be to adjust another sensor, is this possible?

THANKS! :slight_smile:
(Hope you’re well)

EDIT I think I might have figured out what’s going on…I’m running Hass and shell_command won’t work?
EDIT2 Don’t worry - I’ve done the same thing in NodeRed. Thank you though!! :smiley: :smiley:

@daneboom I am in the middle of migrating from Smartthings / Webcore where I had incoming caller ID that announces calls to my Google home device sourced from Sipgate.

With Webcore, I created a REST endpoint that the incoming Sipgate call sent the caller ID as a payload. How have you managed this with HA using MQTT?