Change input_text contents within template sensor

I use input_text sensor for main color of the card where my night clock is shown In order to minimize brightness i use oled tablet and very dark shaded of grey.

For this i created input_text sensor with value “rgb(20,20,20)”. If i want to change color i have manually enter all three numbers each time. Since they are always the same - as said, i only use shades of grey, i wonder, is it possible to “assemble” this sensor into rgb value inside template? I mean somethng like this:

  • i set “input_number.color” with a value 20 - this one i’ll change manually when needed.
  • then i change a template sensor contents, say

set input_text.main_color = 'rgb(' + states('input_number.color') + ',' + states('input_number.color') + ',' + states('input_number.color') + ')

Of course above doesn’t work, it’s just for explaining what i’d like to achieve. Is that even possible without using service? Above combination however works if i set a variable (replace “set input_text.main_color” with, say “some_variable”) , but not if i want to change an entity.

Or is there another way to define grey values easily inside a card?

Why do you not want to use a service? Here’s a very simple automation that calls the service to set the text value any time you change the number:

  - platform: state
    entity_id: input_number.color
  - service: input_text.set_value
      entity_id: input_text.main_color
      value: >
        {% set c = states('input_number.color') %}
        rgb({{ c }}, {{ c }}, {{ c }})

Hm… ok, but how would i run this service? I would like template sensor to be immediately updated when i change that one number. If i’m not thinking wrong using service would require running service each time i change the number, correct?

Yes, which is exactly what the automation does for you. You change the number, it notices and updates the text in line.

1 Like

Aha… you mean that i create an automation which will monitor number change? Ok, that’s doable, i honestly didn’t think of that, thanks for suggestion!

1 Like

Nothing else to do. The automation above is already doing exactly that. :slight_smile:

I succesfully ended up with service and automation, works like a charm.

I just though at the beginning that it’s possible to alter contents of certain entity with some command in template sensor, similar like i define variables, but i can’t find any way except service call.

There is no way. Jinja templates do not “do” anything, they simply generate an output string or object.

1 Like

Aha… many thanks for explanation, another thing learned…