[Bug]: Template not rendering in template card
caraar12345 opened this issue Β· comments
Current Behavior
The template doesn't consistently render. It usually just shows the template text itself.
Expected Behavior
The template renders to show 0 guest devices connected
, 1 guest device connected
or X guest devices connected
.
Steps To Reproduce
type: custom:mushroom-template-card
primary: guest_ssid
secondary: >-
password
{% set dev_count = states.device_tracker | selectattr('state', 'eq', 'home') |
selectattr('attributes.essid', 'eq', "guest_ssid") | list | count %}{% if
dev_count == 1 %}{{dev_count}} guest device connected{% else %}{{dev_count}}
guest devices connected{% endif %}
icon: mdi:wifi-lock
multiline_secondary: true
- Set up a template card using this YAML (YAML not necessary, just easier to share).
Environment
- OS: macOS 12.3.1
- Browser: Safari 15.4 / Home Assistant Companion App for macOS / Chrome 102.0.5005.61
- Version: v1.9.0 (HACS)
- Hassio: 2022.6.3
- Template attributes from: UniFi Network
It doesn't really matter what I use. Not been able to find a match between when it does render and when it doesn't though.
Anything else?
Hello π It's an error with your template. You can test it here.
Hello π It's an error with your template. You can test it here.
Hmmm ... that's entirely what I would have expected. But for some reason, it fails in the template tester, but not always in the Template Card !!??
Regardless, doesn't seem to be a Mushroom thing. Thank you anyway π
Maybe you must check if the attribute exist before selectattr? I'm not a Jinja template expert π
Lol nor am I unfortunately. I think it's because device_tracker
doesn't exclusively cover the UniFi entities so it might be something about whether it comes across a UniFi entity first or not. Will have a play!
Cheers and sorry again π
Just to close the loop, this is the cursed thing I've had to produce to make sure it works π
It did not work...
{% set devices = [] %}
{% for entity in states.device_tracker %}
{% if 'essid' in entity['attributes'].keys() %}
{% if 'guest_ssid' in entity['attributes'].values() %}
{% set devices = devices + [entity] %}
{% endif %}
{% endif %}
{% endfor %}
{% set dev_count = devices | list | count %}
{% if dev_count == 1 %}
{{dev_count}} guest device connected
{% else %}
{{dev_count}} guest devices connected
{% endif %}