elad-bar / ha-edgeos

Integration with EdgeOS (Ubiquiti)
134 stars 23 forks source link

Leased DHCP attribute doesn't update as fast as unknown devices #115

Closed PlantsThatsWhatsUp closed 3 months ago

PlantsThatsWhatsUp commented 8 months ago

Using EdgeRouter-X. Trying to create an automation that when an IP is leased on my network, it notifies me and tells me what device it is. To do that, I need the attribute of unknown devices to update - but it doesn't update for quite a long time. Any clue why? The number changes quickly, but the list doesn't.

This is my automation - want to try this, but seems like a good chance I'll need a helper to store the old list and compare (for people looking to do the same):


alias: Notify on Device Connect/Disconnect
description: ""
trigger: 
  - platform: state
    entity_id: sensor.ubnt_unknown_devices
    attribute: leased
action:
  - service: homeassistant.update_entity
    entity_id: sensor.ubnt_unknown_devices
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - variables:
      new_list: "{{ trigger.to_state.attributes.leased | default([]) }}"
      old_list: "{{ trigger.from_state.attributes.leased | default([]) }}"
      connected: "{{ new_list | select('in', old_list) | list }}"
      disconnected: "{{ old_list | select('notin', new_list) | list }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ connected | count > 0 }}"
        sequence:
          - service: notify.mobile_app_galaxy_watch5_24be
            data:
              title: Device Connected
              message: "New device(s) connected: {{ connected | join(', ') }}"
      - conditions:
          - condition: template
            value_template: "{{ disconnected | count > 0 }}"
        sequence:
          - service: notify.mobile_app_sm_g990u2)
            data:
              title: Device Disconnected
              message: "Device(s) disconnected: {{ disconnected | join(', ') }}
illuzn commented 7 months ago

See my PR #119 and my new blueprint.

The idea was all yours but I made it a piece of cake to implement. It currently only notifies on all connects/ disconnects anyway but long term it is intended that the MAC address filters will be able to filter out known devices.

PlantsThatsWhatsUp commented 7 months ago

Looks promising! I'll try it out once the PR is accepted. Thanks!

elad-bar commented 3 months ago

released v2.1.0, pls check and report

elad-bar commented 3 months ago

my automation:

alias: Unknown Devices
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.ubnt_unknown_devices
condition:
  - condition: template
    alias: Check state changed
    value_template: "{{trigger.from_state.state != trigger.to_state.state}}"
action:
  - variables:
      state: "{{trigger.to_state.state | int}}"
      message: "{{trigger.to_state.attributes | join('\r\n') }}"
  - if:
      - condition: template
        value_template: "{{state > 0}}"
    then:
      - service: notify.mobile_app_samsung_s24_ultra
        data:
          title: "{{state}} unknown devices"
          message: "{{message}}"
          data:
            tag: Unknown Devices
            group: Unknown Devices
            color: red
mode: single
elad-bar commented 3 months ago

Fixed in v2.1.x

illuzn commented 3 months ago

Would you consider making these a dict per my pull request? This make it much easier to filter out devices which are shown here (i.e. devices which have a dynamic ip) but are actually known devices e.g. my mobile telephone.

It's a 2 line change and I even have a working blueprint which implements this: https://github.com/illuzn/homeassistant-edgeos-notification/tree/main

Admittedly filtering by MAC address is not entirely secure because of MAC Spoofing but this prevents being spammed with notifications anybody comes home and connects to the network.

illuzn commented 2 months ago

Sorry to tag you into this but @elad-bar