jimz011
(Jim)
August 24, 2020, 5:28am
4403
Hello everyone, I have a question. Would it be possible to pass the current entity_id into a template (without the use of hardcoded variables?)
E.g. I have a template that opens a browser_mod popup, it will require an entity_id.
- type: custom:button-card
template: popup
name: test
entity: light.patio
and in the templates to have something like this
popup:
tap_action:
action: service-call
service: browser_mod.popup
service_data:
title: ' '
card:
- type: custom:more-info-card
entity: "[[[ return entity.this_id ]]]"
This obviously doesnât work, but I was wondering IF there is such a variable that can pass the same entity_id into the templates (much like auto-entities can do with this.entity_id
variable)?
Thanks for your help
itâs [[[ return entity.entity_id ]]]
that you want to use
2 Likes
zentoo
August 24, 2020, 3:17pm
4405
Hi !
I got a problem to use icon depending on state using operator and value based on another entity:
type: 'custom:button-card'
entity: sensor.zigbee2mqtt_version
show_name: false
color_type: icon
size: 32px
styles:
card:
- width: 100px
state:
- value: '{{ states("sensor.zigbee2mqtt_upstream_version") }}'
icon: mdi:checkbox-marked-circle-outline
- operator: default
icon: mdi:arrow-up-circle-outline
color: var(--paper-item-icon-active-color)
the icon stay in default state even if states of sensor.zigbee2mqtt_upstream_version and sensor.zigbee2mqtt_version are the same.
Any idea ?
jimz011
(Jim)
August 24, 2020, 3:53pm
4406
Thanks, great stuff. Unfortunately this doesnât work when passing it to another card (as in my example). Itâs fine didnât think it would work, but hey could always ask haha. It is probably because the other card doesnât support templates. My only other option would be using decluttering-card or lovelace_gen, Iâll figure it out haha. Thanks for the quick reply though (as always).
I still havenât found the time to play with all the new features youâve added :S
KTibow
(Kendell R)
August 24, 2020, 5:11pm
4407
Button card doesnât support Jinja2, instead it uses JS. Try this out:
type: 'custom:button-card'
entity: sensor.zigbee2mqtt_version
show_name: false
color_type: icon
size: 32px
styles:
card:
- width: 100px
state:
- value: |
[[[
return states['sensor.zigbee2mqtt_upstream_version']
]]]
icon: mdi:checkbox-marked-circle-outline
- operator: default
icon: mdi:arrow-up-circle-outline
color: var(--paper-item-icon-active-color)
zentoo
August 24, 2020, 5:55pm
4408
I got no error on lovelace ui but I got the same result, the default icon.
I suppose it"s a variable type problem. How to debug this ?
KTibow
(Kendell R)
August 24, 2020, 5:56pm
4409
Ooops.
type: 'custom:button-card'
entity: sensor.zigbee2mqtt_version
show_name: false
color_type: icon
size: 32px
styles:
card:
- width: 100px
state:
- value: |
[[[
return states['sensor.zigbee2mqtt_upstream_version'].state
]]]
icon: mdi:checkbox-marked-circle-outline
- operator: default
icon: mdi:arrow-up-circle-outline
color: var(--paper-item-icon-active-color)
zentoo
August 24, 2020, 6:09pm
4410
Yes itâs working now ! Thanks KTibow !
Cadster
(Cadster)
August 24, 2020, 8:33pm
4411
Can you please share your config for your sensor.zigbee2mqtt_upstream_version?
KTibow
(Kendell R)
August 24, 2020, 8:35pm
4412
Ask @zentoo . Iâd reccommend a PM to not pollute this thread too much.
CM000n
(Simon)
August 25, 2020, 9:24am
4414
Hi guys, Iâve searched the thread a little, but somehow couldnât find a suitable solution for me. I hope someone of you can help me.
I use this well-known code in my decluttering template for the button card to display the light brightness within the button card:
card:
type: custom:button-card
size: '80%'
color: auto
lock: false
aspect_ratio: '1/1'
entity: '[[entity]]'
label: >
[[[
var bri = Math.round(states['[[entity]]'].attributes.brightness / 2.55);
return 'Helligkeit: ' + (bri ? bri : '0') + '%';
]]]
show_state: true
show_label: true
Unfortunately I have some lights that do not provide brightness attributes (e.g. Shelly Lights Switches and light groups).
For these âlightsâ I get the following error message with the above code and the button card is no longer displayed:
ButtonCardJSTemplateError: TypeError: Cannot read property 'attributes' of undefined in 'var bri = Math.round(states['light.shelly_badspiegel'].attributes.brightness / 2.55); return 'He...'
Is there a simple way within the label template to check whether the brightness attribute is available and to only display the brightness there? For all other lights without a brightness attribute, I just want to display an empty string at this point.
Thank you in advance for your help / advice
KTibow
(Kendell R)
August 25, 2020, 2:05pm
4415
I think
'brightness' in states['[[entity]]'].attributes
should work
CM000n
(Simon)
August 25, 2020, 2:40pm
4416
Sry, but how did you mean that exactly?
For exampe this gives me just blank button cards:
label: >
[[[
if 'brightness' in states['[[entity]]'].attributes
var bri = Math.round(states['[[entity]]'].attributes.brightness / 2.55);
return 'Helligkeit: ' + (bri ? bri : '0') + '%';
]]]
KTibow
(Kendell R)
August 25, 2020, 2:42pm
4417
JS syntaxâŚ
label: >
[[[
if ('brightness' in states['[[entity]]'].attributes) {
var bri = Math.round(states['[[entity]]'].attributes.brightness / 2.55);
return 'Helligkeit: ' + (bri ? bri : '0') + '%';
} else {
return '';
}
]]]
CM000n
(Simon)
August 25, 2020, 3:01pm
4418
Damn, Iâm still a noob
Thank you very much!
But unfortunately the buttons of lights that have no brightness information still do not load with your example
KTibow
(Kendell R)
August 25, 2020, 3:03pm
4419
Try opening the browser console and taking a screenshot.
CM000n
(Simon)
August 25, 2020, 3:05pm
4420
With your example code it is still the same error message that I received at the beginning
KTibow
(Kendell R)
August 25, 2020, 3:07pm
4421
Oh, looks like the light is completely undefined. Check Developer Tools (the HA one) and see if itâs available.
leaves computer, wonât see your next reply until a bit later
CM000n
(Simon)
August 25, 2020, 3:24pm
4422
Damn, that was the cause. Looks like I broke my configuration for light groups somehow and didnât noticed it. Now everything works. Thanks a lot