[Custom component] AsusRouter integration

Announcements

Multiple-device internet access control (to be released in 0.13.0)

New parameters can be set for the asusrouter.device_internet_access service:

  • entities - a list of entity_ids to set rules for
  • devices - a list of dictionaries with mac and name to set rules for

devices have priority over entities - if both are set, only devices will be affected.

Snapshots


image
image

This will be a non-breaking change. Previously available parameters entity_id, mac and name will be still available for the next 2 versions (0.13.x and 0.14.x) as deprecated. In 0.15.0 all 3 will be removed.

If include deprecated values, this is the priority of executing rules (only the first one available from the list will be affected):

  • devices
  • mac and name
  • entities
  • entity_id

Expected release dates

  • Version 0.13.0 is expected on November 23-24.

  • Version 0.14.0 is expected on December 3-4.

Approximately, of course.

A no-support week in December

Between December 10 and December 17 no updates (including bug fixes) can be released. I will also hardly have any possibility to reply to the issues/messages on GitHub or here.


@TekDad,

I think you might be able to use the new multiple-device control feature to simplify your HA logic.

3 Likes

This is great! Thank you, thank you, thank you!

I tried modifying it a bit to include the Device Name and IP Address with varying success. For some reason, only certain devices are showing up within Flex Table Card - I wish I knew more about the coding within the templating that you use. I can figure out most of what the code is doing, but not all.

What would make it easier is if there was some type of timestamp attribute that I could use - I’ve checked most of the Entities for a timestamp but without enabling Device Tracking, I don’t think there is a timestamp for when a device is connected. Thoughts?

Also - I am a little confused about your Announcement/Update; specifically when you say that entity_id, mac, and name will be removed - can you elaborate on that?

:mag: A new sensor?

In principle, I can implement a separate sensor with a list of the last connected devices as an attribute. Let’s say the following:

  • The sensor value would be the datetime of the last device connected.
  • There would be an attribute list with the list of N device identities (mac, ip, name, connection_type and guest) for the latest connected.
  • The sensor will be disabled by default (so should be manually enabled).
  • [Optional] There could be an option in the AsusRouter configs to select the number of devices for the list. Or it can be just constant (e.g. 5).
  • [Optional] The list could contain:
    • Latest connected devices (it will always have the N elements, but will not guarantee any of the devices still being online).
    • Latest connected AND still connected - so the disconnected devices will be removed from the list (it might have less than N elements, but all of them are definitely online)

:loudspeaker: Let me know if this feature would be useful. It can still be implemented in 0.13.0 (only a couple of days left).


:question: A possibility

Other suggestions, if you would like to:

  • The datetime of devices connected can be added to the device identity - this will influence all the following, that uses identities:
    • devices attribute of the connected_devices sensor
    • connected, disconnected and reconnected events
    • the new sensor from the previous chapter (if implemented)

:loudspeaker: Let me know if this feature would be useful. It can still be implemented in 0.13.0 (only a couple of days left).


:open_book: New parameters for the service

Starting from 0.13.0 asusrouter.device_internet_access service will support setting multiple device rules at once. As a result, there will be no need in providing separate entity_id or mac (and name), since this will be just duplicating the functionality while doing the same.

This will work the following way:

  • new entities parameter will set any number of device_tracker entities to change the internet access rules. E.g. for YAML:
    entities:
      - device_tracker.device_1
      - device_tracker.device_2
    
    instead of:
    entity_id: device_tracker.device_1
    
    The services tab of the HA frontend will get the new input with a multiple selection field in 0.13.0 (which was on the snapshot in the announcement).
  • new devices parameter will set any number of mac (and name) values. E.g. for YAML:
    devices:
      - mac: 01:23:45:67:89:01
        name: Device/1
      - mac: 01:23:45:67:89:02
      - mac: 01:23:45:67:89:03
        name: Device/3
    
    instead of
    mac: 01:23:45:67:89:01
    name: Device/1
    
    There is currently no way to do this from the frontend nicely, so this will be a YAML-only option. (Still might change with time).

:books: For versions 0.13.x and 0.14.x (non-breaking change):

  • There will be an announcement of the new entities and devices parameters
  • There will be a comment about the deprecation of the entity_id, mac and name parameters
  • All 4 options for setting rules will work from YAML
  • Frontend will lose the entity_id field and get the entities input field
  • Frontend will keep mac and name fields
  • Users will be able to use the previously setup automations or scripts using the old parameters
  • Users will be able to use the new parameters

:exclamation: In 0.15.0 (a breaking change):

  • entity_id, mac and name parameters will be removed from the integration completely
  • mac and name will be removed from the frontend
  • All the automations or scripts still using old parameters will produce errors

Hi!
I have tried to use the code snippet to create a switch for one device but I do not manage to get the value_template working. I’ve now tried to even used a simpler value_template as follows, but the switch state does not update in lovelace even if the toggle to block actually works. The switch goes back to the off state after a couple of seconds even if the state in device_tracker.caliopewifi.internet_mode is “block”. Any idea on how to get the switch state right?

This is the switch code:

  • platform: template
    switches:
    internet_caliopewifi:
    friendly_name: CaliopeWiFi
    unique_id: internet_caliopewifi
    value_template: >
    [[[
    return states[‘device_tracker.caliopewifi’].attributes
    && (states[‘device_tracker.caliopewifi’].attributes.internet_mode == ‘block’)
    ]]]
    turn_on:
    service: asusrouter.device_internet_access
    data:
    entity_id: device_tracker.caliopewifi
    state: block
    turn_off:
    service: asusrouter.device_internet_access
    data:
    entity_id: device_tracker.caliopewifi
    state: disable

Hey,

You might try to use a better value_template:

{{ is_state_attr("device_tracker.caliopewifi", "internet_mode", "block") }}

This will return true if there is such an attribute and its value is block.

This will work with the turn_on and turn_off that you have defined. So the switch will be showing when internet access is blocked.

If you would like to change this to the actual internet state (true when internet access is allowed and false - when blocked), you can easily do it with adding not to the statement and swapping turn_on and turn_off

{{ not is_state_attr("device_tracker.caliopewifi", "internet_mode", "block") }}

This would be fantastic. I like the idea of being able to modify the number of devices to show (this is what I did with your example above, although it seems broken now due to my playing around with it).

As far as the last options in the list, I personally like being able to view the data in a static fashion, so the second option updating removing a potentially disconnected device from the list wouldn’t work on my end.

I am constantly tinkering with tech stuff at home, recently focusing a lot of attention at networking which inevitably causes problems within my LAN, so being able to see a list of these types of things helps me :hammer_and_wrench: diagnose and debug (suprisingly, using your Integration has provided me much more insight than the ASUS Web-UI :laughing: ).

This would also be fantastic as it opens up a bunch of great variables to play around with!

Sounds good! Thanks for breaking that down for me. :smiley:

1 Like

Hi all,

I have more than 31 devices connected to my Asus GT-AX11000. The sensor is showing me the right amount of devices. But when I check the device_tracker and select them on attribute: internet_mode (to filter all devices from integration Asus Router.

I only get 3 devices. What can this be?

Hey, @poudenes,

You might need to enable device_trackers. Even though AsusRouter is creating a device_tracker for all the connected devices, the default behaviour for HA will be to disable any, MAC address of which is unknown to HA.

image

Click xxx entities, then check whether device_trackers are disabled

The disabled ones will have a red icon in the Status column and Integration in the Disabled by. Mark those you would like to enable on the left, and you will get the Enable selected option on the top right. It would require ~30 seconds for HA to get everything working

If the connected_devices sensor is showing the correct number - all those device_trackers should already exist in HA - it’s just a feature of the Home Assistant.


@SpicyLimes, deal.

1 Like

thanks this worked!

1 Like

Btw, does AsusRouter work well with your GT-AX11000? If so, what is the firmware version you are using? This model was not yet reported - I would like to add it to the compatible list if everything works well

You can check the FW version in the integration. E.g.:
image

Since day 1 of your Integration I use it without any problem!

Device info

GT-AX11000

by ASUSTek

Firmware: 3.0.0.4.386.7_2

1 Like

There are many old device_trackers in the list. Can’t delete them. Any idea to get rid of those old entities?

@poudenes,

Oh, sorry, I have mixed the models. You really have already reported your one as working :slightly_smiling_face:

There is no way yet to remove the old device_trackers. The idea is that device_trackers are kept, so when a device reconnects - it is handled well. As a result, HA does not allow removing these entities.

I can understand, that some devices are only appearing once in half a year in your local network and then you have some useless trackers.

I think, there is a possibility to allow deleting the old trackers. I will consider it as a feature to implement in the following releases.

1 Like

Its ok. Better to be sure so yes, yes this model is supported :wink:

I play lot with RPi and they connect and disconnect and those are also in HA now. Even when they not in use anymore.

They can be removed to stop HA and edit the core.xxx.xxx files manual. But a tricky way…
(Do not do this when you not know what you’re doing)

That’s so helpful, thanks. Is there a way to create a toggle switch that has THREE states?
OFF - CHANGING - ON
It’s confusing for less-experienced users to flick the toggle switch from OFF to ON, then to see it switch itself OFF again for a little while, then back ON once the change has taken place? I figured if there’s some way to visualise that the state change is in progress people won’t try clicking the toggle switch multiple times when they’re confused about why it’s not doing what they thought it would do.

@spry-salt,

I think, there is no native possibility for HA to have a 3-state switch. And this is something happening to many device-control integrations. HA wants the state to change immediately, but it does not, so the switch goes forth and back until the correct state is actually set.


Unfortunately, the update to 0.13.0 is delayed till the end of the week.

I hope you would agree, that it is better to get a bit more stable release a couple of days later, than a non-tested release faster (and then bugfixes).

4 Likes

0.13.0

:mag: Multiple parental control rules at once

Using new entities and devices parameters of the asusrouter.device_internet_access service, adjust internet access for multiple devices at the same time.

Usage:

entities:
 - device_tracker.device_1
 - device_tracker.device_2
 - device_tracker.device_3

or

devices:
  - mac: 01:23:45:67:89:01
    name: Device/1
  - mac: 01:23:45:67:89:02
  - mac: 01:23:45:67:89:03
    name: Device/3

:loudspeaker: Deprecation warning!

Old entity_id, mac and name parameters are now deprecated and will be removed in version 0.15.0.

Please, check the official documentation for more details: Parental control | AsusRouter (vaskivskyi.com)

:car: Latest connected devices sensor

sensor.{device}_latest_connected

This new sensor provides a datetime object of the last device connected to the router. More details: Connected devices | AsusRouter (vaskivskyi.com)

Its list attribute contains a list of the latest connected devices identities. Number of latest devices in the list can be configured in the AsusRouter options (Operation mode | AsusRouter (vaskivskyi.com)).

:book: New attribute for device identity

Connected device identities got a new attribute connected, showing a datetime since the connection was established.

Identities are used in:

  • Device events (connected, disconnected, reconnected)
  • Connected devices list (devices attribute) of connected_devices sensor
  • List of devices in the new latest_connected sensor.

More on identities: Connected devices | AsusRouter (vaskivskyi.com)

:bug: Bugfix

A nasty bug was fixed, which did not allow the usage of parental control features when the rules list was empty. All the users, which had rules before using them from AsusRouter did not notice it.

Other improvements

  • Code cleanups were made for constants (which helps with maintaining the integration code)
  • A missing string was added to the translations

GitHub release

AsusRouter

In case, you are curious, AsusRouter is not only the integration itself. It consists of the following:

  • Integration (which should be compatible with the latest HA releases and as many versions back as possible)
  • Backend Python library which performs all the communication between integration and your device
  • GitHub repo with the issue tracker
  • This Community thread (including all the nicely formatted release logs and code examples for different usage scenarios)
  • AsusRouter documentation with all the features explained, FAQ and examples

By the way, you might have noticed that for some feature requests I am answering “it is not possible yet”. The reason is - I don’t have devices compatible with some features (including AiMesh, Aura RGB, support of 2.5 Gb/s and 10 Gb/s ports, support of 5 GHz-2 and 6 GHz wireless bands). As a result, some features are either not available yet or are only partially available. If I get the supported device - I will implement them as well.

I am quite sure, AsusRouter easily can replace the native Web UI and mobile app. Just need some time for all the development. :honeybee:

Support AsusRouter development

AsusRouter is my free time project. You can support its development using the Buy Me a Coffee service :coffee:

4 Likes

Thank you for the update! As always, this is working quite well. Below I am attaching code of how I implemented the new sensor.asusrouter_latest_connected information into a card, including some templating of how to convert the timestamp into something that’s easily readable. If someone else has a better method to convert the timestamp into 12hr or 24hr syntax, please post it!

I am also attaching a screenshot of the card using the following code (note: I have my settings set to “10 Recently Connected Devices” rather than the default of 5).

type: entities
entities:
  - type: custom:text-divider-row
    text: Last 5 Connected Devices
    align: left
    card_mod:
      style: |
        h2.text-divider span {
          background: #1A1A1A;
        }
        :host {
           height: 0px;
         }
  - type: custom:flex-table-card
    sort_by:
      - list-
    entities:
      include: sensor.asusrouter_latest_connected
    columns:
      - data: list
        modify: x.connected
        hidden: true
      - data: list
        name: ' Device Name'
        icon: mdi:devices
        modify: x.name
      - data: list
        name: ' Address'
        icon: mdi:ip-network
        modify: x.ip
      - data: list
        name: ' Type'
        icon: mdi:wifi-arrow-up-down
        modify: x.connection_type
    # Below shows an example of how to convert the timestamp 
    # into 'human' readable format - easier methods might 
    # be available (if so, post your code)
      - data: list
        name: ' Time'
        icon: mdi:clock
        modify: |-
          if(x.length == 0){"-"} else {
            var date = new Date(x.connected);
            String(date.getHours()).padStart(2,'0')+":"+
            String(date.getMinutes()).padStart(2,'0')+" "+
            (String(date.getMonth()+ 1).padStart(2,'0'))+"/"+
            date.getDate()+"/"+
            date.getFullYear()
          }
    card_mod:
      style: |
        ha-card {
          background: none;
          padding: 0px;
          spacing: 0px;
        }

Edit: Adding code and screenshot for the ‘Collapsible Card’:

type: entities
entities:
  - type: custom:fold-entity-row
    padding: 0
    head:
      entity: sensor.asusrouter_latest_connected
      name: ASUS Router - 5 Recently Connected Devices
      icon: mdi:lan-check
    entities:
      - type: custom:flex-table-card
        sort_by:
          - list-
        entities:
          include: sensor.asusrouter_latest_connected
        columns:
          - data: list
            modify: x.connected
            hidden: true
          - data: list
            name: ' Device Name'
            icon: mdi:devices
            modify: x.name
          - data: list
            name: ' Address'
            icon: mdi:ip-network
            modify: x.ip
          - data: list
            name: ' Type'
            icon: mdi:wifi-arrow-up-down
            modify: x.connection_type
          - data: list
            name: ' Time'
            icon: mdi:clock
            modify: |-
              if(x.length == 0){"-"} else {
                var date = new Date(x.connected);
                String(date.getHours()).padStart(2,'0')+":"+
                String(date.getMinutes()).padStart(2,'0')+" "+
                (String(date.getMonth()+ 1).padStart(2,'0'))+"/"+
                date.getDate()+"/"+
                date.getFullYear()
              }
        card_mod:
          style: |
            ha-card {
              background: none;
              padding: 0px;
              spacing: 0px;
            }
show_header_toggle: false

6 Likes

@SpicyLimes, cool! Thanks for sharing! :+1:


The same thing differently

You can also convert the datetime to the JSON string and then use regex on it. On the positive side - the JSON time string is in ISO format - so no additional operations are needed to get the correct month number.

# Convert to JSON
# E.g. "2022-11-29T06:03:05:220Z"
var date = (new Date(x.connected)).toJSON();
# Get date - everything before T in JSON
var dateRegex = /^(\d{4})-(\d{2})-(\d{2})T.*$/
var dateData = dateRegex.exec(date);
# Get time - first 3 groups after T. Or you can use one more for 3 digit ms
var timeRegex = /^.*T(\d{2}):(\d{2}):(\d{2}).*$/
var timeData = timeRegex.exec(date);

# Output
dateData[1]+"-"+dateData[2]+"-"+dateData[3]+" "+
    timeData[1]+":"+timeData[2]+":"+timeData[3]

It does not really make code smaller but allows separating output from the processing. So in order to adjust it, one just needs changes in the last part and put 6 values in any needed order.

image


Those who do not care about changing format from ISO to anything else can take date and time as the whole thing:

var date = (new Date(x.connected)).toJSON();
var dateRegex = /^([\d-]{10})T.*$/
var dateData = dateRegex.exec(date);
var timeRegex = /^.*T([\d:]{8}).*$/
var timeData = timeRegex.exec(date);

dateData[1]+" "+timeData[1]

image

1 Like

Interesting - so the JSON coding would be a more ‘efficient’ way of doing it. Makes sense to me - any chance you know how to convert it to 12-Hour formatting? I don’t mind using 24-Hour time (I’ve had to get used to it over the last few years while digging into more tech related subjects), but it would be nice to have it in 12-Hour time (I live in America where we like to do things differently than the rest of the world
:roll_eyes: haha - standard vs. metric, 12h vs. 24hr time, etc.)

Edit: Also, after replacing my original conversion code with yours, I am setting a 6 hour difference in time (i.e.; Original Code: 14:00 hours vs. Your Code: 20:00). Not sure how that is even possible.