Help getting data from JSON (Sagemcom t-mobile 5g modem )

I need to get data from this JSON obtained from the modem to track 5G and 4G signal strength

The output JSON (from http://192.168.12.1/TMI/v1/gateway?get=signal):

{"signal":{"generic":{"apn":"FBB.HOME","roaming":false,"registration":"registered","hasIPv6":true},"4g":{"eNBID":10000,"cid":2211,"sinr":-18,"rsrp":-98,"rsrq":-12,"rssi":-66,"bands":["b66"],"bars":4},"5g":{"gNBID":0,"cid":0,"sinr":40,"rsrp":-103,"rsrq":-12,"rssi":-117,"bands":["n41"],"bars":3}}}

My code:

sensor:

  # TMobile Home Internet Sensors
 
  - platform: rest
    resource: "http://192.168.12.1/TMI/v1/gateway?get=signal"
    scan_interval: 20
    method: GET
    name: tmhi_radio_status
    json_attributes:
      - generic
      - 4g
      - 5g


  - platform: template
    sensors:
      
      tmhi_5g_snr:
        friendly_name: "TMHI 5G SNR"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '5g') [0] ["sinr"] }}
        unit_of_measurement: "dB"

      tmhi_5g_rsrp:
        friendly_name: "TMHI 5G RSRP"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '5g') [0] ["rsrp"] }}
        unit_of_measurement: "dBm"

      tmhi_5g_rsrq:
        friendly_name: "TMHI 5G RSRQ"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '5g') [0] ["rsrq"] }}
        unit_of_measurement: "dB"
      tmhi_5g_pci:
        friendly_name: "TMHI 5G PCI"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '5g') [0] ["cid"] }}

     
      tmhi_4g_snr:
        friendly_name: "TMHI 4G SNR"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '4g') [0] ["sinr"] }}
        unit_of_measurement: "dB"

      tmhi_4g_rsrp:
        friendly_name: "TMHI 4G RSRP"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '4g') [0] ["rsrp"] }}
        unit_of_measurement: "dBm"

      tmhi_4g_rsrq:
        friendly_name: "TMHI 4G RSRQ"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '4g') [0] ["rsrq"] }}
        unit_of_measurement: "dB"

      tmhi_4g_pci:
        friendly_name: "TMHI 4G PCI"
        value_template: >
          {{ state_attr('sensor.tmhi_radio_status', '4g') [0] ["cid"] }}

I can see the sensors created in the entities, but says entity unavailable.

You have not specified the json_attributes_path (which should be $.signal), however…

Use the RESTful integration instead. You can make as many sensors as you want from the one call to the resource. Then there is no need to mess around with template sensors either.

(configuration.yaml, not sensors.yaml)

rest:
  - resource: http://192.168.12.1/TMI/v1/gateway?get=signal
    method: GET
    scan_interval: 20
    sensor:
      - name: "TMHI 5G SNR"
        value_template: "{{ value_json.signal.5g.sinr }}"
        unit_of_measurement: "dB"
      - name: "TMHI 5G RSRP"
        value_template: "{{ value_json.signal.5g.rsrp }}"
        unit_of_measurement: "dBm"
      - name: "TMHI 5G RSRQ"
        value_template: "{{ value_json.signal.5g.rsrq }}"
        unit_of_measurement: "dB"
      - name: "TMHI 5G PCI"
        value_template: etc...

Thank you very much for the help. I copied the code exactly to configuration.yaml and i am getting image

Do I have to call the json attribute path for every sensor or is there something else wrong? indentation is correct I checked.

Ah, you have to use square bracket notation for json keys that start with numbers. I have trouble mixing up square bracket and dot notation so in these cases I just use all square bracket notation:

rest:
  - resource: http://192.168.12.1/TMI/v1/gateway?get=signal
    method: GET
    scan_interval: 20
    sensor:
      - name: "TMHI 5G SNR"
        value_template: "{{ value_json['signal']['5g']['sinr'] }}"
        unit_of_measurement: "dB"
      - name: "TMHI 5G RSRP"
        value_template: "{{ value_json['signal']['5g']['rsrp'] }}"
        unit_of_measurement: "dBm"
      - name: "TMHI 5G RSRQ"
        value_template: "{{ value_json['signal']['5g']['rsrq'] }}"
        unit_of_measurement: "dB"

But really only the “5g” part needs to be in the square bracket notation.

In your original config?

Yes, but you only had one rest sensor.

You don’t need it for the config above as the paths are included in each sensor.

1 Like