Open danielbrunt57 opened 6 months ago
Ideally, I would like to be able to do this with the custom:auto-entities card (and have the confirmation option):
tap_action:
action: call-service
service: button.press
service_data:
entity_id: '{{ this.entity_id|replace("sensor.","button.")|replace("_battery_plus","_battery_replaced") }}'
but that is not working unless I hard-code the entity_id. Full code (thus far)...
type: custom:auto-entities
card:
type: custom:battery-state-card
title: Low Batteries
debug: false
card_mod:
class: inline-card
filter:
include:
- integration: battery_notes
domain: sensor
attributes:
device_class: battery
state: < 60
options:
name: '{attributes.friendly_name} ({attributes.battery_type_and_quantity})'
secondary_info: 'Last reported: {attributes.battery_last_reported|reltime()}'
tap_action:
action: call-service
service: button.press
service_data:
entity_id: '{{ this.entity_id|replace("sensor.","button.")|replace("_battery_plus","_battery_replaced") }}'
- integration: battery_notes
domain: binary_sensor
attributes:
device_class: battery
state: 'on'
exclude:
- integration: mobile_app
sort:
method: state
numeric: true
With regards to my templated service_data:
, entity_id:
issue, I started thinking outside the box and thought this might work. Since I use custom-ui, I added a new atrribute to the battery sensor called battery_last_replaced_button which is set to the entity_id of the related button. I then tried using KString in entity_id
but it looks like it's not being parsed there.
configuration.yaml:
homeassistant:
customize_glob:
"*.*":
hide_attributes:
- templates
sensor.*_battery:
templates: &battery_color
icon_color: >
var r = Math.min(255, Math.round(510 - 5.10 * Number(state)));
var g = Math.min(255, Math.round(5.10 * Number(state)));
var h = r * 0x10000 + g * 0x100;
return '#' + ('000000' + h.toString(16)).slice(-6);
sensor.*_battery_level:
templates: *battery_color
sensor.*_battery_plus:
templates:
<<: *battery_color
battery_last_replaced_button: >
return 'button.' + entity.entity_id.split('.')[1].split('_battery_plus')[0] + '_battery_replaced';
and I now have the new attribute:
and battery-state-card appears to see it:
but when I tap, it triggers an error:
Is there any way I can template the entity_id
in tap_action
?
Update As for the templated custom-ui attribute, it seems to only exist in the front-end(?) as I cannot reference it where I need to. I tried creating a script for tap_action to call but the script cannot see the custom-ui created attribute as verified by entering a template into Dev Tools - the attribute is not real. So much for that! I'll ask the Battery Notes author (nicely) to add that attribute and then my script should work, unless of course you add KString parsing to entity_id in tap_action!
FYI, I came up with this card solution which although is a little heavy on templating, enabled me to call the battery_notes.set_battery_replaced
service using device_id:
via a hold_action
. The card I created looks identical to the one I made first using battery-state-card.
The first one...
type: custom:auto-entities
card:
type: entities
title: Low Batteries
debug: false
card_mod:
class: inline-card
filter:
include:
- integration: battery_notes
domain: sensor
attributes:
device_class: battery
state: < 60
options:
type: custom:template-entity-row
entity: this.entity_id
name: >-
{{state_attr(config.entity,'friendly_name')}}
({{state_attr(config.entity,'battery_type_and_quantity')}})
secondary: >-
Replaced: {{
state_attr(config.entity,'battery_last_replaced')|relative_time() }}
ago
state: '{{ states(config.entity)|int(0) }}%'
hold_action: |
{
"action": "call-service",
"service": "battery_notes.set_battery_replaced",
"data": {
"device_id": "{{ device_id(config.entity) }}",
},
"confirmation": {
"text": "Update Battery Replaced Date",
},
}
- integration: battery_notes
domain: binary_sensor
attributes:
device_class: battery
state: 'on'
exclude:
- integration: mobile_app
sort:
method: state
numeric: true
and the second one...
type: custom:auto-entities
card:
type: custom:battery-state-card
title: Low Batteries
debug: false
card_mod:
class: inline-card
filter:
include:
- integration: battery_notes
domain: sensor
attributes:
device_class: battery
state: < 60
options:
name: '{attributes.friendly_name} ({attributes.battery_type_and_quantity})'
secondary_info: 'Reported: {attributes.battery_last_replaced|reltime()}'
hold_action:
action: call-service
service: script.battery_notes_battery_replaced
service_data:
entity_id: this.entity_id
- integration: battery_notes
domain: binary_sensor
attributes:
device_class: battery
state: 'on'
exclude:
- integration: mobile_app
sort:
method: state
numeric: true
Food for thought??
Is your feature request related to a problem? Please describe. I'm always frustrated when... card parameters differ from other mainstream cards.
Describe the solution you'd like Implement the tap_ action configuration variables as per Home Assistant Actions documentation which the Button, Entities, Glance, Light, Picture, Picture element, Picture entity and Picture glance cards use.
Additional context I really want the
confirmation:
option!And
service_data:
should be justdata:
to match other HA cards. You could keepservice_data:
functional for backwards compatibility rather than issuing a breaking change.Ideally, my code would look like...
rather than how I have to do it now...
Oh, and if not too much trouble, could you add
icon_tap_action
like the tile card has???Thanks for listening!