Oliver Maller
All projects

Project · Telephony · Home Assistant

The house phones in Home Assistant

Who called, which room answered and what was missed, read straight from an NEC SL2100's call log over the network. No CTI licence, no extra box and no cloud.

Phone system
NEC SL2100
Reads
SMDR over TCP
Latest release
0.9.0
Cloud services
None
Contents

The phone system already knows

Our house runs an NEC SL2100, a small-business phone system with a desk phone in most rooms. It handles the outside lines, rings the right phones and keeps voicemail. It also knows, call by call, who rang, which phone picked up and which calls nobody answered. None of that was visible anywhere useful.

Everything else in the house already reports to Home Assistant, so the phones were the odd one out. I wanted three things there: a list of who called and which room took the call, a notification on my phone for a missed call with the caller's name on it, and daily totals I could put on a dashboard. Nothing fancy, and nothing that needed a licence, a new box or a cloud service.

The result is ha-nec-pbx, a Home Assistant integration for the NEC SL2100 and SV9100, and aionecpbx, the small Python library underneath it that does the talking and parsing. This is how they work and what the phone system taught me along the way. The people, rooms and numbers in the examples are made up: the household is Alex, Sam and Jordan, and every phone number comes from the 0491 570 range set aside for fiction.

A printer port on the network

NEC sells a proper computer telephony interface for these systems, and it would give you everything live, ringing included. It also needs a paid licence. What every SL2100 has for free is SMDR, Station Message Detail Recording: the call log a phone system used to send to a dot-matrix printer in the back office. One line of text per call.

On the SL2100 that printout can go to the network instead. The phone system is the TCP server: you connect to its SMDR port (Program 10-20, external equipment type 5, port 4001 on Australian systems), and it writes a line each time a call finishes. On our system the settings it needed were already the defaults: Program 35-01-01 set to LAN, and incoming calls printed (35-02-08), which you need for missed calls.

That sounds easy, and the connection really is just a socket. The constraints are what shaped the design:

  • A record only exists once a call ends. There's no "the phone is ringing" from SMDR, and no "on a call". The integration tells you about finished and missed calls. Live ringing needs the CTI licence, or FreePBX, which comes up later.
  • Only one client can hold the port. If Home Assistant is connected, nothing else can be, and anything that opens a second connection "just to test it" can knock the real one off.
  • The layout isn't fixed. The columns move about with the 35-01 and 35-02 options: names or numbers, a date or no date, overseas or North American format. The SV9100 prints different class codes again.
  • It remembers. While nobody is connected, the SL2100 stores calls, and it prints them all the moment someone connects. That one surprised me, and it gets its own section.

The phone system writes a call record when a call ends. Everything else follows from that.

Reading a line of call log

The SL2100's programming manual describes the line as fixed-width columns. In practice the columns move whenever an option changes, so the parser in aionecpbx doesn't trust positions. It works on the line in pieces: it looks for the things that are easy to recognise first, takes each one out of the line as it finds it, and works down to the harder ones, which are easier to see once the rest is gone.

  1. Missed-call words like NO ANSWER, ABANDON or LOST, as whole words only.
  2. A cost, if the system prints one, like $0.25.
  3. The talk time, always H:MM:SS, before anything that could be confused with it.
  4. A class code in the first two tokens: IC, OG, PIN, POT and friends.
  5. The clock time and date, with the date order worked out if it's ambiguous.
  6. The ring time, a short M:SS, usually after the number on incoming calls.
  7. The outside number: the longest run of five or more digits, perhaps ending in X where digits were hidden.
  8. The extension and the line. A known extension number or room name wins. A three-digit number starting with 0 is the outside line.

If there's no class code, the direction is worked out from what's left: a ring time or a missed-call word means incoming, a number means outgoing, and an extension on its own means internal. The parser never raises. Anything it can't read comes back empty, and the raw line is always kept, so a layout I've never seen can be checked and added to the tests.

Pick a line below and step through it, or paste your own. The decoder is a JavaScript copy of the library's parser, run against the same sample lines as its Python tests.

29 FAMILY  18:20 28/11 001                      0491570156       0:21    NO ANSWER

The record

direction
incoming
call_class
none
start
28 Nov, 18:20
duration
none
ring_time
21 s
number
0491570156
caller_name
Alex
station
104 (Family)
trunk
001
cost
none
answered
false
flags
no_answer

In Home Assistant

Call event
missed
Bus events
nec_pbx_call, nec_pbx_missed_call
Logbook
House phones: missed call from Alex (0491570156) to Family, rang 21 s
Fires now?
Yes: it ended at 18:20:21, inside the 15 minutes before 18:30
Fictional lines in the layouts the library's tests use, read with a made-up setup: extensions 101: Kitchen, 103: Study, 104: Family and 106: Upstairs, a phonebook for Alex, Sam and Jordan, and the clock stopped at 18:30 on 28 November 2026. The numbered marks show the order the parser takes pieces out of the line.

Two of those samples are bugs I shipped. The SV9100's PIN and POT class codes weren't in the first table, so its incoming calls read as outgoing, and missed-call alerts could never fire. And until 0.8.1, a caller ID name like BLOSTON marked an answered call as missed, because it contains LOST. Missed-call words now only count as whole words. The 11/05 line is the third: a call on 5 November printed month-first was read as 11 May, looked six months old, and fired nothing. Dates are now read in whichever order isn't in the future, or in the order you choose in the options.

From a line to Home Assistant

The library hands each line to the integration, and the integration turns it into things Home Assistant understands. The phone system is one device, and every extension and outside line you name in the options gets a device of its own under it.

  • Sensors. Last call and last missed call, with every detail as attributes. Calls, missed calls and talk time for today, this week and this month, the longest call today and unknown callers today. Recent calls as a list of the last 20, for a dashboard card.
  • A device per room. Each named extension has its own last call, calls today, missed calls today and talk time today. Each named outside line has its last call and calls today.
  • A Call event entity that fires once per finished call, typed incoming, missed, outgoing, internal or other, so automations pick a type in the editor instead of matching event names. A missed call is typed missed, not incoming.
  • Bus events, nec_pbx_call and nec_pbx_missed_call, carrying everything about the call, raw line included, for YAML automations.
  • Logbook lines that read like a sentence: House phones: missed call from Sam (0491570157) to Family, rang 21 s.
  • A call history calendar holding the last 500 calls across restarts, so the week view shows who rang when.
  • A connection binary sensor with the line count and the last error, and a Repairs warning if the call log has been unreachable for 10 minutes. It clears itself when the connection comes back.
  • Names, not numbers. A phonebook in the options, or a Home Assistant to-do list you can edit from the app. The nec_pbx.save_caller action adds the last caller to it, say from a notification button.
  • A missed-call list, new in 0.9.0: each missed call adds a "Call back …" item to a to-do list, ticked off by itself when someone calls that number back.
  • Two blueprints: a missed-call notification, and an announcement of who's calling on your speakers when FreePBX is connected.

Nothing is polled. The phone system pushes the line the moment the call ends, and the entities update straight away. Here's a missed call from the first ring to the notification on a phone.

  1. Ring18:20:00

    Alex rings the main line, and the Family phone on 104 rings. The SL2100 knows, but SMDR has nothing to say until the call ends.

  2. No answer18:20:21

    Nobody picks up. After 21 seconds Alex hangs up, and the call is over.

  3. SMDR lineat once

    The SL2100 writes one line to whoever holds port 4001: 29 FAMILY 18:20 28/11 001 0491570156 0:21 NO ANSWER

  4. Parseaionecpbx

    No class code, but a ring time and NO ANSWER: incoming, not answered, so missed. The phonebook says the number is Alex, and the name FAMILY matches extension 104.

  5. Eventsha-nec-pbx

    A line it hasn't seen, from a call that ended in the last 15 minutes: the Call event fires as missed, nec_pbx_missed_call goes on the bus, the logbook gets a line, Last missed call and Missed calls today update, and a "Call back" item goes on the to-do list if you chose one.

  6. Notificationblueprint

    The missed-call blueprint, triggered by the Call event's missed type, sends the notification to a phone with the Home Assistant app.

One made-up missed call. The notification text is the blueprint's own format: the caller, the time, and the room that rang. The time between steps 2 and 6 is a moment, not the gaps shown here.

Twelve calls at once

The first time Home Assistant connected to the real SL2100, nothing happened for a second, and then twelve calls arrived at once. The phone system had been keeping every call since its last client went away, and it printed the lot the moment someone connected. If the integration had believed them, it would have sent twelve notifications for calls that were hours old, some of them answered.

The rule that came out of that is simple. Every call fills in the history, the sensors and the totals, but only a call that ended in the last 15 minutes fires events, logbook lines and notifications. A call record carries its start, ring time and talk time, so the end is easy to work out. Fifteen minutes is long enough for a call that finished while Home Assistant was restarting, and short enough that nobody gets told about breakfast-time calls at tea time.

Then 0.4.0 found the second half of the problem. The SL2100 replays its store on every connect, not just the first, so after each restart the daily totals counted the same calls again, and Last call could jump back to an older call. Now every line is remembered by its text with the spacing evened out, and the last 600 are saved across restarts: more than the 500 calls the history keeps, so a full replay can never sneak a duplicate in. A line it has already seen is skipped for counting, events and history.

What happens to each stored call when Home Assistant connects at 18:30
CallEndedFirst connectAfter a restart
Sam, answered in the Kitchen08:06History onlySkipped
Study called Alex08:42History onlySkipped
Unknown caller, answered in Family09:17History onlySkipped
Kitchen to Upstairs, internal10:03History onlySkipped
Missed call from Jordan11:34History onlySkipped
Upstairs called Sam12:01History onlySkipped
Alex, answered in the Study13:52History onlySkipped
Missed call, number withheld15:12History onlySkipped
Family called Jordan16:25History onlySkipped
Sam, answered in Family17:56History onlySkipped
Study called Alex18:15Events fireSkipped
Missed call from Alex18:20Events and a notificationSkipped
An illustrative day of twelve calls stored while nothing was connected. The calls and times are made up; the 15-minute window and the skipping of lines already seen are the integration's real rules. All twelve count towards Calls today on the first connect, because they're new and from today.

One more thing comes from the one-client rule. A dead connection can look alive: if the phone system reboots or a cable is pulled without the socket closing, the integration used to sit there "connected", hearing nothing. TCP keepalive now notices within about 90 seconds and reconnects, backing off from 5 seconds to 5 minutes. And Reconfigure no longer opens a test connection when the address hasn't changed, because that test would take the port from the running integration.

What went wrong

Most of the bugs came from the phone system doing something reasonable that I hadn't expected. These are the ones worth knowing about if you build anything similar.

Every call after a restart was counted twice.Fixed in 0.4.0

The replay on reconnect, again. Lines are now remembered and saved, so a call is counted and announced once. In 0.8.2 the memory grew from 400 lines to 600, after a replay of more than 400 stored calls could still double-count.

The SV9100's incoming calls read as outgoing.Fixed in 0.4.0

Its layout uses PIN, IVIN, PTRS, POT and POTA. They're in the class table now, and PTRS also marks the call as transferred. I've only seen that layout in sample lines, not on a real SV9100, and the docs say so.

A call on 5 November looked six months old.Fixed in 0.4.0

Printed month-first as 11/05, it was read as 11 May, so it looked like an old call and fired nothing. Automatic mode now settles it if one number is over 12, and otherwise takes the reading that isn't in the future. Day-first and month-first can also be set in the options.

An answered call from "BLOSTON" was reported as missed.Fixed in 0.8.1

The missed-call words were found anywhere in the line, and BLOSTON contains LOST. They're whole words only now, with a test for exactly that name.

The same digits could be read twice.Fixed in aionecpbx 0.2.2

The outside number was removed from the line by text, not by position, so the same digits inside another token could be taken out instead. The parser now removes every piece from the exact place it found it.

A phone system that hung up straight away got hammered.Fixed in aionecpbx 0.2.2

If the SL2100 accepted the connection and closed it at once, the back-off reset every time and the client reconnected every 5 seconds. Now a connection only counts as healthy once a line arrives or it has stayed up for a minute.

Privacy, and a mistake I made

A call log is a list of who talks to whom, which makes it some of the most personal data in a house. The integration runs entirely on the local network and treats the numbers carefully:

  • Diagnostics are masked. Numbers are cut to their first four digits, including ones written with spaces or hyphens, so a diagnostics file can go into a public issue.
  • The raw line stays quiet. An unreadable line is logged at debug level only, never as a warning, and the Last call log line sensor is disabled by default because it holds callers' numbers.
  • Less in the database. Recent calls and the connection counters aren't written to the recorder, and nor are the callers' numbers on the live ringing and on-a-call sensors.
  • Passwords don't come back. The FreePBX password is never sent back to the browser; leave the field empty to keep the saved one.

And now the mistake. The first versions were built against our own phone system, and our own data leaked into the repository: real family phone numbers in the test lines, the options examples and the docs. Nobody had to point it out for it to be wrong. I rewrote the repository's history to take them out, rebuilt the releases from the cleaned history, and since 0.3.1 the rule has been fictional data only: made-up names, and numbers from the 0491 570 range that's set aside for exactly this.

It's an easy mistake to make when you're testing against the real thing, and it's why every example on this page, and in the repository, is invented.

Design rules I stuck to

  1. Never raiseA line the parser can't read comes back empty, with the raw text kept for the diagnostics.
  2. Count onceA line already seen is never counted, announced or added to the history again.
  3. Old news is quietOnly calls that ended in the last 15 minutes fire events or notifications.
  4. Respect the portOne client only, so nothing opens a second connection it doesn't need.
  5. Notice silenceKeepalive spots a dead link in about 90 seconds; Repairs speaks up after 10 minutes.
  6. Local pushNo polling and no cloud: the phone system pushes each call as it ends.
The rules behind the integration, each enforced in code and covered by tests.

Where it's at

The current release is 0.9.0, on aionecpbx 0.2.3. It runs every day on our SL2100 (Australian, software 5.21) and installs from HACS or the Home Assistant Marketplace.

  • Every finished call lands in Home Assistant the moment it ends, with the number, the caller's name, the room and how long it rang and talked.
  • Missed calls send a notification with the caller's name, and go on a call-back list that ticks itself off.
  • Calls replayed after a restart fill in the history without a single repeat notification or double count.
  • Both NEC layouts are read: IC/OG and the SV9100-style PIN/POT/PTRS/IVIN, with either date order.
  • With FreePBX connected (optional), ringing, on-a-call and who's calling are live, and do-not-disturb switches follow changes made on a phone.
  • Typed with mypy strict, and tested to at least 95 % in every module, with CI failing below that.

The FreePBX option exists because SMDR can't do live, and FreePBX turned up in the house anyway. Our outside calls now pass through it on their way to the AI phone attendant, and Asterisk's manager interface reports ringing, answered and hung up as they happen. That makes ha-nec-pbx the Home Assistant end of the attendant too. Since 0.7.0 the SMDR and FreePBX clients live in aionecpbx on PyPI, so other projects can use them without Home Assistant.

What's next

  • A real SV9100. Its layout is read from sample lines only. If you have one, the raw line from the diagnostics (numbers masked) is all I need to check it.
  • More layouts. The 35-01 and 35-02 options make many variations, and each one reported becomes a test.
  • The attendant in Home Assistant. An optional add-on that brings the AI attendant's calls and messages into the integration is on a branch, not in a release yet.

Install and read more

Install it from HACS or the Home Assistant Marketplace, add the integration with the phone system's address and SMDR port, then name your rooms and add a phonebook under Configure. Both repositories are MIT licensed.

Thanks to Home Assistant and HACS, and to the nec-smdr project for publishing real SV9100 output, which is where the PIN/POT class codes came from. NEC, SL2100 and SV9100 are trademarks of NEC Corporation; this project isn't affiliated with or endorsed by NEC.