feat: Add Nearest scanner entity_id sensor #374
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds an additional sensor that tracks the entity_id of the nearest scanner. Having the raw entity_id allows you to perform downstream tasks on the scanner entity, such as retrieve all associated labels, get state attributes, etc.
Most of my scanners are Shelly 1 Plus relays controlling light switches. The switch location is not always the same area as the lights it controls (e.g. inside switch controlling outside lights). Exposing the raw entity_id of the nearest scanner lets be use custom attributes or labels in my dashboards.
I've added a custom attribute (switch_location) to each Shelly device, and am instead using this in my dashboards:
Phone is near {{ state_attr(states(entity), 'friendly_name') }} ({{ state_attr(states(entity), 'switch_location') }})
Alternatively, the same can be done using Labels (I've used a prefix "switch_location_[VALUE]"):
Phone is near {{ state_attr(states(entity), 'friendly_name') }} ({{ (labels(states('sensor.phone_nearest_scanner_entity')) | select('match', '^switch_location_.*') | first).replace('switch_location_', '') }})