[Custom component] AsusRouter integration

yeah I tried that, you get a 'duplicated mapping key ’ syntax error when you include more than 1 ‘entity_id’.

1 Like

Figured it out - for those looking for similar functionality.

  1. Create an automation for Enabling Internet and for Disabling Internet (2 automations)
    1a. Click ADD ACTION (you do not need to set up triggers or conditions unless you want to)
    1b. Choose the service: AsusRouter: Device Internet Access Control
    1c. Enable Entity and choose the appropriate device
    1d. Choose the appropriate state. Block to block access, Disable to grant access
    1e. Create additional actions with the above steps for each of the devices you want to include in the single button click from lovelace


    (in my setup above, I have 2 devices being controlled in this individual automation. I have another automation with the same settings, just a different state of ‘disable rule’ which is used to enable internet access to the same 2 devices)

  2. Create a button on your lovelace dashboard for enabling and disabling internet (2 buttons)

**Enable Code**
type: button
show_name: true
name: Enable Internet
show_icon: true
icon: mdi:account-check
show_state: true
tap_action:
  action: call-service
  service: automation.trigger
  service_data:
    entity_id: automation.internet_grant_ryan

**Disable Code**
show_name: true
show_icon: true
type: button
name: Disable Internet
icon: mdi:account-cancel
show_state: true
tap_action:
  action: call-service
  service: automation.trigger
  service_data:
    entity_id: automation.internet_block_ryan

image
This gives me 2 functional buttons, which when clicked will either enable or disable internet access to both my cell phone and work laptop.

I plan to replicate this so that I have an enable button and disable button for each of my children so I have single click access to turn internet on/off for their groups of devices.

@Vaskivskyi awesome integration!

5 Likes

Hey there,

Yes, it is really only possible currently to enable / disable a single device internet access. I will try to implement such a possibility for multiple devices at the same time since this feature is apparently needed for your use cases.

Please, allow me time till the end of the week. This would require changes both to the integration and to the backend library. But I will try my best to do it faster.


@TekDad,

I like your latest idea with automation! It will really work this way and can be used until the multiple-device control feature is added to AsusRouter.

Also, the YAML snippet I provided was to create a template switch for single-device internet access.


@matssa,

I am using Pi-hole in my network - so it also has static DNS rules for the local network. It runs on the central smart home device (not a Pi :slightly_smiling_face:) together with all the other services and software. The router does specify it as the only DNS server for all the connected devices. I personally don’t like the idea of running DNS on the router. And for me it would anyway not work well - my only router is used for tests of this integration - so all the settings are regularly reset.

I am quite sure, you would hardly notice the difference between traffic and efficiency for DNS queries device-router and device-router-DNS. So if you are fine with it - keep it as it is on the router. If the router is struggling because of DNS (high CPU / RAM usage) - move it. But with RT-AX88U there is enough horsepower.

As for the second part, I cannot yet guarantee any Merlin-only features, since I need to first find out how exactly it works. AsusRouter uses HTTP API, so it can only poll and command over the available API endpoints.

On the opposite, WireGuard is available with the new Asus firmware 3.0.0.4.388.xxx That one will definitely be implemented. But there is no defined timeframe yet.


Sorry if I am not replying immediately. AsusRouter is my free-time project. Free time is not always available during working days. But all the questions and any issues will be addressed.

1 Like

The per-device internet access management is great! Thank you.

Question - As I am using an older device, RT-AC68U, the memory seems to fill up fast through general use, and I need to occasionally (every week or two) log into the router through SSH to run a command to clear it up.
I realise I can schedule the command on the router directly with a cron job, but if I could send a command to the router from Home Assistant then that would be a much better way to go, as the cron job command itself will also consume some of the router memory.
Would it be possible to have an AsusRouter Service, whereby a telnet command can be sent to the router?

For anyone interested the command is:

for line in `nvram show | grep ^[^=]*=$ `; do var=${line%*=}; nvram unset $var; done; nvram commit

You can always call the ssh command from HA to remotely run your code as part of an automation, right?

That sounds like what I am trying to do, yes.
Is this the method what you are describing?
Sound like I will need to bring keys across or something to that effect. Not impossible but a bit of an uphill battle for me.

Hey,

AsusRouter is using HTTP API and does not have access to the shell. It can, of course, set NVRAM values (and can set them as empty), but it cannot unset (at least from what I know about. I can check it in more detail, but I would be surprised if such functionality is possible - unsettling NVRAM values might get users into an entertaining journey of recovering everything :slightly_smiling_face:

P.S. Does your device has so little memory, that you can noticeably gain by removing empty values?

Do you think there will be any chance of getting access to the dual wan functions/button and dhcp server on/off button?

Thanks

There is always a chance :slightly_smiling_face:

What exactly would you like to monitor/control from AsusRouter in DHCP and Dual WAN? Only on/off state or settings as well?

1 Like

That would be awesome - I am just setting up dual wan with failback (sim arriving for a 4g router as failback)
So yes possibly state and on/off for that so I can be notified. I have set up a notification for wan disconnetion state but would be great to see both wan’s.

Also yes the DHCP - currently I use pihole as dhcp but in case of failover would be great to see an option to switch dhcp on/off. Connecting remotely through the asus app I don’t see that option on the app.
I would need to vpn to my lan by pc/browser and open up the router page. Bit of a hassle.
If that was in HA would be something great! :slight_smile:

These states and buttons would be great to have -

and the dhcp switches/state

and asking for the moon on a stick also with this:

The state for the primary WAN is already available - it’s the binary_sensor.{device}_wan. It also has all the following attributes.

The same is definitely possible for the secondary WAN and it can be implemented.

About others - I still need to check

1 Like

Thanks for your feedback.

I understand for the API, greatly helps indeed ^^

Concerning the Merlin features, my guess was to make some ssh requests directly and do some sed or some grep to take the needed info and send come commands directly (via ssh again) ^^ that was where I was going. Can be a little painful since you need to have the known hosts, ssh must be enabled on the router, ssh key must be added in HA for more secure connections, etc…

Anyways, we’ll see! :wink: thanks again and good luck!

Yeah,mkind of…
You can make it as simple as having a shell command stored in the secrets.yaml file including your password in the call, but using an authentication key will certainly add security to this, as secrets.yaml is stores in plain text (and probably your keys will be also).

Anyone have any idea as to how to setup a card that can list, say, the last 5 recently connected devices? I have to imagine that with all the available options (either via attributes or events) that this can be done easily. I have tried used the Logbook Card along with others, however I can’t seem to have any list the last 5-10 recently connected devices.

Also, to jump in on the recent posts regarding running DNS on the router itself or separately (e.g.; via RPi)… I am using an RT-AX58U with Merlin firmware, and am running SkyNet, SCMerlin, Wireguard Manager - Client Only (since my router doesn’t natively support Wireguard - only OpenVPN), Unbound, and AdGuard Home without any issues. I currently run about 75 devices on my network as well - and my RAM usage hovers around 90-95% with a CPU usage of 8-15%. I have not seen any issue with performance - but I also setup a swap on my USB drive attached to the router which holds my Entware and AMTM files.

I was previously running AdGuard Home and Unbound on a RPi 4B but decided that I want to wrap my network within Wireguard (Surfshark VPN Client) while letting all local DNS queries resolve before it goes out to WAN via the VPN - and this was the only way to accomplish my goal. Again, I literally JUST switched over from the RPi to the router for DNS resolution and have NOT seen any degradation in performance (other than WAN speeds, but that is due to the VPN).

Yes, but I am not sure why! I am maybe using more features than the average home user, but definitely not at the power user level. RAM usage seems to creep up over time (now easily visible in HA thanks to your integration).
There are probably 100 clients total, with about 20 having static addresses configured. OpenVPN is configured, DDNS is configured. A symptom of nvram filling up is that all port forwarding would suddenly disappear. I moved most port forwarding to HA NPM for easier management, which also helped with the memory shortage.

Do you mean you have reserved addresses on your dhcp server?

With my older router (RT-AC88U) I used to get issues when editing the dhcp reservations… I was using way more than 20 reserved addresses, but I can tell you that after some point any edit to that table was forcing me to reboot the device otherwise it was making it usage impossible and crashing the Web interface.

Another option would be a command line / switch:

- platform: command_line
  switches:
    desktop_internet:
      friendly_name: "Desktop Internet"
      command_off: "/usr/bin/curl -f -X GET http://homeassistant_ip:7022/disconnect?ip=target_ip"
      command_on: "/usr/bin/curl -f -X GET http://homeassistant_ip:7022/reconnect?ip=target_ip"
      command_state: "/usr/bin/curl -f -X GET http://homeassistant_ip:7022/status?ip=target_ip"
      value_template: >
        {{ value != "1" }}

It’s what I’ve been using, together with scheduler component, to create study times for my kid.
netlock

Yes that’s right. Previously I had a lot of port forwarding but now only one forward.
It seems like the problem might be linked to an increasing use of RAM until I rebooted the router a few minutes ago. I’ll look on the snb forums to find out the cause, but now at least I can set an automation to do something about it (reboot).

The device list can be created in 3 steps and requires 2 entities. At least, I am not sure, whether it can be simplified.

Step 1 - input_text

Create an input_text entity input_text.latest_connected_devices. Set it’s value as (empty) or just put some random MAC, e.g. 12:34:56:78:90:12 - it will be overwritten later.

Step 2 - template

Create a binary_sensor template:

template:
  - binary_sensor:
      - name: Latest Connected Devices
        unique_id: latest_connected_devices
        state: true
        attributes:
          list: >
            {% set string = states("input_text.latest_connected_devices") %}
            {% set macs = string.split("/") %}
            {{ macs }}
Step 3 - automation
alias: AsusRouter - Latest connected devices
description: Keep a list of the latest connected devices
trigger:
  - platform: event
    event_type: asusrouter_device_connected
  - platform: event
    event_type: asusrouter_device_reconnected
condition: []
action:
  - service: input_text.set_value
    data:
      value: >
        {% set macs = state_attr("binary_sensor.latest_connected_devices", "list") %}
        {% set new = trigger.event.data.mac %}
        {% set list_size = 5 %}

        {% for i in range(list_size) %}
          {% if i < macs|length and macs[i] == new %}
            {% set removed = macs.pop(i) %}
          {% endif %}
        {% endfor %}

        {% if macs|length >= list_size %}
          {% set removed = macs.pop(0) %}
        {% endif %}

        {% set string = macs|join("/") + "/" + new %}

        {{ string }}
    target:
      entity_id: input_text.latest_connected_devices
mode: queued
max: 10

So, when the device_connected or device_reconnected event will fire, the automation will take the last list from the attribute of the template binary_sensor. If the new MAC is found in the list - it is removed. If the list is already of the length you set (e.g. 5), the oldest MAC is removed. In the end - a new MAC is added and all is written to the input_text. When input text is updated, the template updates automatically.

The list attribute you can easily use in the frontend card.

Card
type: custom:flex-table-card
sort_by:
  - list+
entities:
  include: binary_sensor.latest_connected_devices
columns:
  - data: list
    name: MAC
    icon: mdi:devices
card_mod:
  style: |
    ha-card {
      box-shadow: none;
      background: none;
    }

P.S. This is a fast prove of concept. Of course, the logic can be improved. You can also create an automaton to remove devices already disconnected.


@poida97,

You may try to wait for RAM to raise higher. In principle, the router should start freeing up RAM as soon as it reaches something between 80 and 90%. Without any need for you to intervene.

1 Like