Iām curious, is there a way to target specific attributes from a sensor template?
Iāve managed to set up a sensor template which will create the texts I need.
One of the two is a āNameā attribute, and the other a āStateā attribute :
sensor:
- unique_id: greet
state: template
attributes:
name: >
{% set time = now().hour %}
{% if time <= 1 %} Good night
{% elif time <= 3 %} Good night
{% elif time <= 5 %} Good night
{% elif time <= 7 %} Good morning
{% elif time <= 9 %} Good morning
{% elif time <= 10 %} Good morning
{% elif time <= 13 %} Good afternoon
{% elif time <= 15 %} Good afternoon
{% elif time <= 17 %} Good afternoon
{% elif time <= 19 %} Good evening
{% elif time <= 22 %} Good evening
{% elif time <= 23 %} Good evening
{% else %} Good evening
{% endif %}
state: >
{% set entity_id = 'alarm_control_panel.master' %}
{% if is_state(entity_id, 'disarmed') %}
Alarm system is: <font color='red'>{{ states("alarm_control_panel.master") }}</font>.
{% else %}
Alarm system is: <font color='green'>{{ states("alarm_control_panel.master") }}</font>.
{% endif %}
And they show up perfectly!
However, they only apply the styles for the āCardā and the āNameā
name: >
[[[
if (entity) {
let attr = [];
for (let [k, value] of Object.entries(entity.attributes))
window.navigator.userAgent.match(/iPhone/i)
? k !== 'time' && k !== 'date' && value !== false && (attr += `<p>${k === 'greet' ? `<span class="iphone">${value}</span>` : `${value}`}</p>`)
: value !== false && (attr += `<p>${value}</p>`);
return attr;
}
]]]
styles:
card:
- background: none
- border: none
- box-shadow: none
- padding: "1em"
name:
- justify-self: "start"
- font-size: "3.5rem"
- font-weight: "100"
- font-family: "Merriweather"
- line-height: 10%
state:
- font-size: "1rem"
- font-family: "Raleway"
the state style is not applied as the second line of text should be much smaller and a different font:
Did I miss something, or we canāt target attributes of a sensor?
EDIT
I found the solution:
Sensor:
sensor:
- unique_id: greet
state: template
attributes:
name: >
{% set time = now().hour %}
{% if time <= 1 %} Good night
{% elif time <= 3 %} Good night
{% elif time <= 5 %} Good night
{% elif time <= 7 %} Good morning
{% elif time <= 9 %} Good morning
{% elif time <= 10 %} Good morning
{% elif time <= 13 %} Good afternoon
{% elif time <= 15 %} Good afternoon
{% elif time <= 17 %} Good afternoon
{% elif time <= 19 %} Good evening
{% elif time <= 22 %} Good evening
{% elif time <= 23 %} Good evening
{% else %} Good evening
{% endif %}
- unique_id: alarm_state
state: Measuring
attributes:
name: >
{% set entity_id = 'alarm_control_panel.master' %}
{% if is_state(entity_id, 'disarmed') %}
Alarm system is: <font color='red'>{{ states("alarm_control_panel.master") }}</font>.
{% else %}
Alarm system is: <font color='green'>{{ states("alarm_control_panel.master") }}</font>.
{% endif %}
Button-card Template:
sidebar:
variables:
alarm_state_entity:
weather_state_entity:
show_state: false
show_icon: false
tap_action:
action: none
styles:
grid:
- grid-template-areas: |
"n"
"alarm_state"
- grid-template-rows: auto repeat(2, min-content)
- align-items: start
- will-change: transform
card:
- background: none
- border: none
- box-shadow: none
- padding: 1em
name:
- justify-self: "start"
- font-size: "3.5rem"
- font-weight: "100"
- font-family: "Merriweather"
- line-height: 10%
- white-space: "inherit"
custom_fields:
alarm_state:
- line-height: 80%
- justify-self: "start"
- font-size: "1rem"
- font-family: "Raleway"
name: >
[[[
if (entity) {
let attr = [];
for (let [k, value] of Object.entries(entity.attributes))
window.navigator.userAgent.match(/iPhone/i)
? k !== 'time' && k !== 'date' && value !== false && (attr += `<p>${k === 'greet' ? `<span class="iphone">${value}</span>` : `${value}`}</p>`)
: value !== false && (attr += `<p>${value}</p>`);
return attr;
}
]]]
custom_fields:
alarm_state: >
[[[
var ent_alarm = states[variables.alarm_state_entity].attributes.name
return ent_alarm;
]]]
The name piece came from matt8707ās sidebar
And lastly in my home.yaml:
- type: vertical-stack
view_layout:
grid-area: header
cards:
- type: custom:button-card
entity: sensor.template_greet
variables:
alarm_state_entity: sensor.template_alarm_state
template:
- sidebar
- type: custom:mod-card
card:
show_current: true
icon: none
show_forecast: false
type: weather-forecast
entity: weather.buienradar
secondary_info_attribute: temperature
- !include repeaters/menu.yaml
Which all results inā¦
Next up figuring out how to change the style of the weather card through the button-cards