I have a custom field which loop to check which water zone is a plant belong to and update button card with last watering time, but I have to manually update the data array each time i have a new plant or move the plants. With the new organizing label tag I know can use jinja template to get the label tag into array but how do i convert this into JS to be use on button card.
{{ labels("plant.apple_tree") }}
['zone1_irrigation', 'plants']
custom_fields:
last_water: |
[[[
var zone1 = ["Plumeria Tree", "Fig Tree", "Persimmon Tree", "Cantaloupe", "Watermelon", "Peach Tree", "Apple Tree", "Grape Tree", "Lemon Tree"];
var zone3 = ["Strawberry", "Jasmine"];
if (zone1.includes(entity.attributes.friendly_name)) {
var date = states['sensor.zone1_last_water'].state;
} else if (zone3.includes(entity.attributes.friendly_name)) {
var date = states['sensor.zone3_last_water'].state;
} else {
return `<ha-icon
icon="mdi:sprinkler"
style="width: 12px; height: 12px; color: deepskyblue;">
</ha-icon><span style="color: var(--text-color-sensor);">Manual</span>`;
}
if (date == "unknown" || date == "unavailable" ){
return `<ha-icon
icon="mdi:sprinkler"
style="width: 12px; height: 12px; color: deepskyblue;">
</ha-icon><span>Last Water: <span style="color: var(--text-color-sensor);">N/A</span></span>`;
} else {
return `<ha-icon
icon="mdi:sprinkler"
style="width: 12px; height: 12px; color: deepskyblue;">
</ha-icon><span style="color: var(--text-color-sensor);">${helpers.formatDateWeekdayShort(date) + " - " + helpers.formatShortDateTime(date)}</span>`;
}
]]]