Hey All. I haven’t shared piece of my setup in a while. I wanted to contribute something I couldn’t find anywhere else.
In the spring I will be putting Home Assistant in my Airstream RV. I wanted to monitor my Pepwave MAX BR1 MK2 cellular router for connectivity, data usage and to be able to reset the cell connection. Here is what I came up with:
I am pulling this info using the Pepwave local device API, which I found documentation for on a fluke googling other things about the router: https://download.peplink.com/resources/Peplink-Router-API-Documentation-for-Firmware-8.1.3.pdf
To use my configuration you will first need to generate a ClientId and ClientSecret using your regular admin login. Replace XXXXX with your admin password and generate a cookie using this command:
curl -k -c cookies.txt -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"XXXXXXXXXXXXXXXXXXXXXX"}' https://192.168.50.1/api/login
Then generate the ClientId and ClientSecret using this command:
curl -k -b cookies.txt -H "Content-Type: application/json" -X POST -d '{"action":"add","name":"ha","scope":"api"}' https://192.168.50.1/api/auth.client
Now you you can use the following configuration, replacing XXXXX with your generated clientId and clientSecret. The first rest resource below generates a auth token on HA start and every 2 days when the token expires. This auth token is what allows the other rest calls to work.
rest:
- resource: "https://192.168.50.1/api/auth.token.grant"
scan_interval: 172700
verify_ssl: false
method: POST
headers:
Content-Type: application/json
payload: '{"clientId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","clientSecret":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","scope":"api"}'
sensor:
- name: pep_token
value_template: "{{ value_json.response.accessToken }}"
- resource_template: "https://192.168.50.1/api/status.wan.connection?accessToken={{ states('sensor.pep_token') }}&id=2"
scan_interval: 5
verify_ssl: false
sensor:
- name: pep_message
value_template: "{{ value_json.response['2'].message }}"
- name: pep_mobiletype
value_template: "{{ value_json.response['2'].cellular.mobileType }}"
- name: pep_signal
value_template: "{{ value_json.response['2'].cellular.signalLevel }}"
- name: pep_uptime
value_template: "{{ value_json.response['2'].uptime }}"
- resource_template: "https://192.168.50.1/api/status.wan.connection.allowance?accessToken={{ states('sensor.pep_token') }}&connId=2"
scan_interval: 60
verify_ssl: false
sensor:
- name: pep_usage
value_template: "{{ value_json.response['2']['1'].usage }}"
template:
- sensor:
- name: pepwave_message
unique_id: pepwave_message
state: "{{ states('sensor.pep_message') }}"
- name: pepwave_mobiletype
unique_id: pepwave_mobiletype
state: "{{ states('sensor.pep_mobiletype') }}"
- name: pepwave_signal
unique_id: pepwave_signal
state: >-
{{ states('sensor.pep_signal') }} / 5
- name: pepwave_uptime
unique_id: pepwave_uptime
state: >-
{% set time = (states.sensor.pep_uptime.state | int) | int %}
{% set minutes = ((time % 3600) / 60) | int %}
{% set hours = ((time % 86400) / 3600) | int %}
{% set days = (time / 86400) | int %}
{%- if time < 60 -%}
Less than a minute
{%- else -%}
{%- if days > 0 -%}
{{ days }}d
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ hours }}h
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ minutes }}m
{%- endif -%}
{%- endif -%}
- name: pepwave_usage
unique_id: pepwave_usage
unit_of_measurement: GB
state: >-
{{ ( states('sensor.pep_usage') | float / 1048576 ) | round(2) }}
rest_command:
pep_cell_reset:
url: "https://192.168.50.1/api/cmd.cellularModule.reset?accessToken={{ states('sensor.pep_token') }}"
verify_ssl: false
method: POST
headers:
Content-Type: application/json
payload: '{"connId":"2"}'
script:
pep_cell_reset:
alias: Cell Reset
icon: mdi:refresh
sequence:
- service: rest_command.pep_cell_reset
If you have issues troubleshooting is pretty straight forward executing the curl commands in a terminal, using the auth token or cookie authentication. Explore the rest of the API documentation for more information! jsonpathfinder.com is also very helpful for isolating the json value_template.
There is also documentation on accessing information from the router via SSH, SNMP and SMS (I could not get SMS to work), but I found the information from the rest calls sufficient for my needs: https://www.peplink.com/support/downloads/miscellaneous-downloads/
- Rob