Mariusthvdb / custom-ui

Add templates and icon_color to Home Assistant UI
163 stars 30 forks source link

Is it just my system or does custom-ui prevent yaml reloading via Location & Customizations? #141

Closed danielbrunt57 closed 8 months ago

danielbrunt57 commented 8 months ago

Hi Marius, Here's my current issue: If I add/delete/change anything in customize:, the quick YAML configuarion reloading of LOCATION & CUSTOMIZATIONS in Dev Tools does not reflect my change I have to restart Home Assistant to implement the edit(s). Is this normal behaviour with custom-ui installed or do I have a bug somewhere in my customizations?

Mariusthvdb commented 8 months ago

without any example, I couldnt say....

usually the changes only kick in after a state change. you can force that in dev tools state

danielbrunt57 commented 8 months ago

I tried setting a new state on an entity after saving a change in configuration.yaml and reloading location & customization but the new value for the templated attribute did not update. Here's my code currently:

homeassistant:
#  customize: !include customize.yaml
#  customize_domain:
#    light:
#      templates:
#        icon_color: "return state === 'on' ? 'yellow' : 'grey';"
  customize_glob:
#    "*.*":
#      hide_attributes:
#        - templates
    sensor.*_battery_plus:
      new_attribute: "and again"
      templates:
        templated_attribute: "Just a string"
        battery_last_replaced_button: return 'button.' + entity.entity_id.split('.')[1].split('_battery_plus')[0] + '_battery_replaced';
        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);
#    binary_sensor.*_motion:
#      templates: &motion_templates
#        icon: "return state === 'on' ? 'mdi:motion-sensor' : 'mdi:human-male';"
#        icon_color: "return state === 'on' ? 'yellow' : 'grey';"
#    binary_sensor.*_occupancy:
#      templates: *motion_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
#        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);

image

Just before the above, I also removed custom-ui-only v2024.1.16 which I had manually installed, and installed custom-ui v2024.1.18 via HACS.

The other thing I'm wondering is whether I should be able to reference the templated attributes in Dev Tools? Currently, I cannot. state_attr('','') returns Noneafter a restart. No, wait a second, now it's showing. But it's not updating in dev tools>template when i see a change in dev tools>states.

image

{% set entity = "sensor.bathroom_battery_plus" %}
{{ entity|replace("sensor.","button.")|replace("_battery_plus","_battery_replaced") }}
{{ state_attr(entity, 'new_attribute') }}
{{ state_attr(entity, 'icon_color') }}
{{ state_attr(entity, 'battery_last_replaced_button') }}

image

This is what I see in dev tools template after a system restart: image

Sometime later (much later) it changes.

danielbrunt57 commented 8 months ago

Those templated attributes still have not updated in dev tools>template...

Mariusthvdb commented 8 months ago

sorry, but you're not making this easy to assist.

please make a minimal failing example, so we can check that.

and no, the attributes can Not be templated n dev tools templates nor core backend templates. we tried, but there is no way. please always search other issues, open and closed, before posting.

for now, just give it a restart to see if the template is actually functional, and updates after that

danielbrunt57 commented 8 months ago

Ok, so upon deep reflection over the last 7 hours and reading and re-reading your documentation slowly and carefully, I came to realize that Dev Tools>Template couldn't possibly see or be influenced by the front-end templating which custom-ui overlays onto the state of things, I dropped that method of testing and went looking for a front-end view of things. I used the battery-state-card where I could view attributes.whatever via its {KSting} within name: and secondary_info: and the custom templated attributes existed as I intended. I changed to this simple config:

homeassistant:
  customize:
    sensor.bathroom_battery_plus:
      templates:
        battery_last_replaced_button: >
           ${ 'button.' + entity.entity_id.split('.')[1].split('_battery_plus')[0] + '_battery_replaced' }
#        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);

restarted HA and verified the state of that entity.
I changed configuration.yaml to:

homeassistant:
  customize:
    sensor.bathroom_battery_plus:
      templates:
        battery_last_replaced_button: >
           ${ 'button.' + entity.entity_id.split('.')[1].split('_battery_plus')[0] + '_battery_replaced' }
        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);

and used reload Locations and customizations but dev tools>state did not show the new attribute. I then changed the state using dev tools but still no change. Then I reloaded the MQTT integration and the attributes updated.

I repeated the process altering included/commented and performed the same reload sequence and although the template disappeared, its resulting attribute from before was still present.

homeassistant:
  customize:
    sensor.bathroom_battery_plus:
      templates:
#        battery_last_replaced_button: >
#           ${ 'button.' + entity.entity_id.split('.')[1].split('_battery_plus')[0] + '_battery_replaced' }
        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);

image

I now have a much better understanding of all the inner workings and although the last edit wasn't fully updated via reload, what I'm seeing appears to just be normal behaviour and I'm okay with that. It's not like I'm doing edits all the time and since a full restart only takes about 90 seconds, it's not the end of the world. Just a little inconvenient when changing things and troubleshooting my errors.

Thank you very much for your time looking at this!