Formatting a string I'm sending to MQTT publish - awtrix caller-ID via Polycom VVX api

Hello. I’ve got the REST integration pulling some values off my Polycom VVX phone. My goal was to be able to stop the music, but then it occurred to me that my Ulanzi/awtrix display would be a good place to send the caller ID.

rest:                                  
  - authentication: basic              
    username: "Polycom"                
    password: 123456                
    scan_interval: 2                   
    resource: http://192.168.1.99/api/v1/webCallControl/callStatus
    sensor:                                                       
      - name: "PolycomCallState"                                  
        value_template: "{{ value_json.data.CallState }}"         
      - name: "PolycomType"                                       
        value_template: "{{ value_json.data.Type }}"              
      - name: "PolycomRemotePartyName"                            
        value_template: "{{ value_json.data.RemotePartyName }}"   
      - name: "PolycomCallType"                                   
        value_template: "{{ value_json.data.RemotePartyNumber }}" 
      - name: "PolycomRinging"                                    
        value_template: "{{ value_json.data.Ringing }}" 

This works fine to bring the values I’m interested in into HA.

Sensor.polycomcalltype will ordinarily show sip:[phone number]@IP address. Ideally I’d like to show only that part rather than the whole value.

alias: Polycom Caller ID
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.polycomcallstate
    for:
      hours: 0
      minutes: 0
      seconds: 2
    from: unknown
conditions: []
actions:
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: true
      qos: 0
      retain: false
      topic: awtrix_0c0dc8/notify
      payload: >-
        {"text": "{{states('sensor.polycomcalltype')}}","rainbow":
        true,"duration": 10,"overlay": "drizzle"}
mode: single

The above works fine to send the whole value to the Ulanzi/Awtrix display. How can I manipulate the string to just be the value between the colon and the @ sign - ie the phone number.

This is the raw JSON from the phone:

{
    "data": [
        {
            "Media Direction": "sendrecv",
            "CallState": "Connected",
            "Remote Connection IP": "1xx.xx.xxx.xx",
            "Type": "Incoming",
            "CallHandle": "ab8ea408",
            "RTPPort": "2254",
            "Ringing": "0",
            "RemotePartyNumber": "sip:[email protected]",
            "Muted": "0",
            "RTCPPort": "2255",
            "Protocol": "SIP",
            "RemotePartyName": "xxxxxxx xxxxxxx",
            "StartTime": "2025-01-30T16:58:37",
            "UIAppearanceIndex": "1*",
            "CallSequence": "1",
            "LineID": "1",
            "DurationSeconds": "3"
        }
    ],
    "Status": "2000"
}

Anyway, I’ll be thrilled if I can get just the text. I read this article that maybe gets me close with the split command, but when I try to split on @ I get a null string. Not sure if I can get crazy and include the for loop described in the stackoverflow article linked.

Right now my Ulanzi shows sip:[email protected] but I really just want +1aaabbbcccc.

This is the latest thing I tried while reading articles about jinja2

{"text": "{%set list1 = {{states('sensor.polycomcalltype')}}.split('@')%} {{ list1[0] }}","rainbow": true,"duration": 10,"overlay": "drizzle"}

The payload is probably the wrong place to try and insert that though, it didn’t like that much

This worked!

Given that the call is presented as sip:[email protected] I first split it on the @ and take the left [0] side and then split that result on the : and grab the right side [1].

Hope this helps someone who’s struggling with string manipulation in jinja2 output. This post got me there when I saw its solution.

{"text": "{{states('sensor.polycomcalltype').split('@')[0].split(':')[1]}}","rainbow": true,"duration": 10,"overlay": "drizzle"}

Of course I had to put it on my TV as well:

data:
  title: INCOMING BUSINESS PHONE CALL
  message: Incoming call from {{states('sensor.polycomcalltype').split('@')[0].split(':')[1]}}.
action: notify.android_tv_fire_tv