Data_template - Need help formatting this

Are you saying you can customise using wild cards in customize_glob?
Or have misunderstood something?
If you can is this documented anywhere as I don’t recall ever seeing it?

yes, that’s the whole point of customize_glob…

risking this really getting out of hand, do it like this:

##########################################################################################
# Customize entities matching a pattern
# to override use: custom_ui_state_card: null
# YAML keys can’t start with an asterisk - *, so in that case quotes are required.
# However in the middle or end of the key an asterisk is allowed.
# https://community.home-assistant.io/t/customui-discussion-thread/48694/237?u=mariusthvdb
##########################################################################################

'*.*':
  custom_ui_state_card: state-card-custom-ui

camera.*:
  custom_ui_state_card: null
history_graph.*:
  custom_ui_state_card: null
media_player.*:
  custom_ui_state_card: null
weather.*:
  custom_ui_state_card: null

automation.*:
  templates:
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';
  extra_data_template: >
    if(!attributes.last_triggered) return null;
    var t,s=(new Date()-new Date(attributes.last_triggered))/1e3;
    return(
    (t=Math.floor(s/86400))?t+(t>1?" days":" day"):
    (t=Math.floor(s/3600))?t+(t>1?" hours":" hour"):
    (t=Math.floor(s/60))?t+(t>1?" minutes":" minute"):
    (t=Math.floor(s))!==1?t+" seconds":" second"
    )+" ago";

Fair enough but…

just browse over home-assistant-custom-ui/docs/features.md at master · andrey-git/home-assistant-custom-ui · GitHub and find many examples.

we can customize per entity, per domain, or globally. Depending on the customization you need, you need custom-ui.

wildcards can be used anywhere in the entity_id, as I’ve showed in the example for the template we’re discussing or in my customize_glob.yaml excerpt.

eg:

sensor.*_motion_sensor_temperature:
  templates:
    icon_color: >
      if (state < -5) return 'rgb(30, 255, 255)';
      if (state < 0) return 'rgb(30, 144, 255)';
      if (state < 10) return 'rgb(255, 255, 0)';
      if (state < 15) return 'rgb(255, 211, 30)';
      if (state < 20) return 'rgb(0, 128, 0)';
      if (state < 25) return 'rgb(255, 165, 0)';
      if (state < 30) return 'rgb(255, 0, 0)';
      if (state < 35) return 'rgb(85, 0, 0)';
      return 'rgb(47, 0, 0)';

Ah-ha! That is why I haven’t seen it anywhere.
I always forget you use custom-ui when I see you doing things I don’t recognise!!

If you are only referring to the wilcards being used in customize_glob then you don’t need the custom-ui for that.

see the last example here:

https://www.home-assistant.io/docs/configuration/customizing-devices/#manual-customization

correct, that’s why I said:

icon_color is one of them, as is extra_data_template :wink:

Thankyou!
It was tucked away in the docs all along.

I know.

It was worded a bit funny in your post and, based on the reply, it seemed that Klogg might have had some misunderstanding that I was trying to help clarify for them.

haha I see now. Changed it a bit, hope its better now.

1 Like

this still doesn’t work as it should… if I have objects with a _ in the name, the template results None.

I use a room = template that outputs the exact list of the first template where we hardcoded the names. Somehow the loop doesn’t iterates this list. If only booleans without _ are ‘on’ it works correctly:

output is None but it is below the screenshot.

using the Name in the room= template, gets me further, counts alright, but one shows the names without the _ in the name

Works for me, using my original template.

Double-check the modifications you made to your version of the template.

changed the booleans to have the same entity part as the media_players, to be done with it… works again.

new challenge, which probably requires some nifty templating:

suppose playing over several players (input_booleans = ‘on’) now turn_off one boolean: stop playing over that player automatically.

I don’t want to trigger the play_radio automation, because I only want to stop playing the turned-off player (and don’t want to trigger the play_radio in each state change of the booleans…)

could a template be made that finds the difference between the 2 states before and after, and then switches off that entity?

coming to think of it, the other way round would apply too: add a boolean, and start playing also over that added media_player.

maybe one could record the state in variable, and on state change check if the state is less, than stop the taken out boolean. if the state is more, than start the added boolean…

update

created this automation:

# Update Last Motion variable
  - alias: 'Update Last active radios'
    id: 'Update Last active radios'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.media_players_active
    action:
      service: variable.set_variable
      data:
        variable: last_active_radios
        attributes_template: >
          {
                "history_1": "{{ variable.state }}",
                "history_2": "{{ variable.attributes.history_1 }}"
          }
      data_template:
        value: >
          {{ trigger.to_state.state }}

and variable:

variable:
  last_active_radios:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:radio
      name: 'Last Active radios'

on boolean change the sensor changes state and triggers the variable being recorded.

I first stop the previously selected radio_players, and then play the newly selected ones:


  - alias: 'Play Radio'
    id: 'Play Radio'
    trigger:
      - platform: state
        entity_id: sensor.media_players_active
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.radio_on','on')}}
    action:
      - service: script.stop_radio # using variable
      - service: script.play_radio

@Mariusthvdb

Can you explain in dummy/lay-man terms how your code is structured and planned to work?

I’m thinking of adding further speakers (devices) to my home setup and have a toggle to enable/disable each speaker. Thinking of using the code you are using but need to figure out how it works.

Thanks!

HI, please just check from the beginning to this short thread, the structure will unfold, since it is built upon each step taken. Its all there.

If there’s a specific part you don’t understand, I’d be glad to help and explain.

They probably need to know that it uses two custom components, one for storing variables and another (Custom UI) to augment UI control.

BTW, does Custom UI still allow control over icons in recent versions? I couldn’t get it to work (in Lovelace) after I upgraded to 0.89. For example, this stopped working:

In customize.yaml:

climate.thermostat:
  templates:
    icon: >
      if (state === 'heat') return 'mdi:fire';
      if (state === 'cool') return 'mdi:snowflake';
      if (state === 'auto') return 'mdi:autorenew';
      if (state === 'idle') return 'mdi:sleep';
      return 'mdi:power-off';
    icon_color: >
      if (state === 'heat') return 'red';
      if (state === 'cool') return 'blue';
      if (state === 'auto') return 'green';
      if (state === 'idle') return 'black';
sensor.heating_hours_today:
  icon: mdi:clock-outline

yes no issues here on that matter. My seconds system is on 92.2 and all customized icons show. More for that matter, since it imported the newer Mdi icons I had wanted for a longer period be never dared to upgrade to :wink:

only thing incorrect in your template above would be the missing ‘else’ under the icon_color:

    icon_color: >
      if (state === 'heat') return 'red';
      if (state === 'cool') return 'blue';
      if (state === 'auto') return 'green';
      if (state === 'idle') return 'black';
      return 'grey';   ##<------

It doesn’t need an else (and it worked in 0.84).

Each statement is evaluated separately. If the statement is false it proceeds to evaluate the next statement. Only if all four if statements are false will ever evaluate the final line.

I have other, simpler, custom icon controls and they all stopped working after I upgraded Home Assistant. I also upgraded Custom UI.

The lack of customized icons was purely cosmetic so I just didn’t bother investigating it. All of the Custom UI’s configuration is identical to how it was for 0.84 when it worked (but with the States UI, not Lovelace).

will see again if I adjusted anything in Custom-ui then, really dont think so, and all is still working except for the _stateDisplay customization.

about the ‘else’, I merely suggested it because you do have:

return 'mdi:power-off';

for the icon. I have al my ‘off’ icons set to grey, which I like better than the harder default HA color. Indeed no real ‘error’, sorry.

I have many much more complex, and they all still work :wink: only icons Lovelace took out are the icons of my menu tabs.
Regular, some would even call it legacy HA still going strong in that department in 92.2:

22

  customize:

    group.personal:
      templates:
        icon: >
          if (entities['sensor.family_home'].state === '0') return 'mdi:account-off';
          if (entities['sensor.family_home'].state === '1') return 'mdi:account';
          if (entities['sensor.family_home'].state === '2') return 'mdi:account-multiple';
          if (entities['sensor.family_home'].state === '3') return 'mdi:account-multiple-check';
          return 'mdi:account-group';

which is based on a presence triggered python script created sensor.

I just finished un-commenting all the lines related to the customizer custom component. Re-ran its update.sh script (it reported all its files were up to date) then restarted Home Assistant (0.91). Syslog reported the customizer is loaded. Now the custom icons are being rendered correctly.

Very strange. I’m not a fan of self-correcting problems. :man_shrugging:

No idea what had prevented it from working before but nice to see it working again.