Templating dependent sensor same room customize_glob

Hi everyone,

I want to customize the icon color of a input_button depending on a different entity with similar name for a bulk of input_buttons. The input_buttons are named something like input_button.room_name_of_job and the other entity on which state the icon color should depend on is named something like switch.room_name_of_duty. Maybe someone of you can help me, otherwise I have to set customize it for every single input_button.

The config for one entity looks like this

input_button.schlafzimmer_heizung_boost:
  templates:
    icon_color: >
      if (['unavailable', 'unknown'].includes(state)) return '#bf0056';
      if (entities['switch.schlafzimmer_bosch_thermostat_boost'].state === 'on') return '#ff8100';
      return 'grey';

But I would like to move this into the customize_glob file. So I need a if statement which take the string input_button.schlafzimmer_heizung_boost, replaces “input_button.” with “switch”, and replaces “heizung_boost” with “bosch_thermostat_boost” and checks the state of the entity with the created string. Is this somehow possible?

Edit: I am using custom UI.
Edit 2: Corrected the code to actually work for one entity.

is this using custom UI? If no, then what you want is not possible.

Yes sorry, forgot to mention that.

Ok, then you have to use what custom UI allows. I don’t use it, but I’m 99% sure it does not have jinja functions in it. I.e.

won’t work

EDIT: Looking at the examples, you have to use

entities['switch.schlafzimmer_bosch_thermostat_boost'].state == 'on'

Thank you. I just noticed my first code didn’t work. Corrected it.

So you recon there is no possibility for some string operations?

there is, but its using JS (javascript) not jinja. You’d have to split them based on ‘_’ and/or ‘.’

But javascript is not usable in the yaml files, is it?

jabascript is required for customui. So anytime you’re using customui to set a color, you have to use javascript.

that’s javascript

that’s half javascript, half jinja (will not work)

that’s javascript

Oh, I wasn’t aware of that, thanks. So does that mean I can use javescript functions like .replace(…)?

yes, all JS native functions should be available.

Hm, is there a variable “inside” the template representing the entity?

I believe so, check the examples in the customUI documentation, that’s where I got entities from.

That I did. It seems to be entity.entity_id. I will try something.

Thank you for the javascript hint! I got it working. Here is my code.

"input_button.*_heizung_boost":
  templates:
    icon_color: >
      if (['unavailable', 'unknown'].includes(state)) return '#bf0056';
      function inThermostatBoostVerwandeln(string) {
        return string.replace('input_button', 'switch').replace('heizung_boost', 'bosch_thermostat_boost');
      }
      var thermostat_boost = inThermostatBoostVerwandeln(entity.entity_id);
      if (entities[thermostat_boost].state === 'on') return '#ff8100';
      return 'grey';
1 Like