What does ~ do?

I watched a YouTube video that is a couple of years old, about a Lovelace design. I learned a few tips that I will use to clean up my Lovelace pages, but he used the ~ symbol in a way I’ve never seen before.

In a card, he defines room:

  - entity: climate.bedroom
    attribute: temperature
    name: Set
    action:
      service: script.temperature_toggle
      room: bedroom

In a script he refers to room like this: ~room

What does the ~ do? Is its use documented?

Here is the whole script:

temperature_toggle:
  alias: Temperature trigger
  sequence:
  - choose:
    - conditions:
        condition: template
        value_template: '{{ state_attr( ''climate.'' ~room , ''temperature'') > 20
          }}'
      sequence:
      - service: climate.set_temperature
        data:
          entity_id: '{{ ''climate.'' ~room }}'
          temperature: 13
    - conditions:
        condition: template
        value_template: '{{ state_attr( ''climate.'' ~room , ''temperature'') < 15
          }}'
      sequence:
      - service: climate.set_temperature
        data:
          entity_id: '{{ ''climate.'' ~room }}'
          temperature: 18
    default:
    - service: climate.set_temperature
      data:
        entity_id: '{{ ''climate.'' ~room }}'
        temperature: 21

it’s a string concatenation

it concatenates the contents of the variable ‘room’ to the string (for example) ‘climate.’

so the end result of the concatenation could be ‘climate.livingroom’ if ‘room’ was equal to ‘livingroom’.

I believe it’s in the jinja docs somewhere. if not it should be in the Python docs.

Thanks. I fail to see how this is useful, so i’ll just keep spelling out my entity names.

Since it’s a script and can pass variables to it, the other person probably had can automation that called the script and they passed the variable ‘room’ to it so they can use the same script for different rooms.

It’s not used a lot but I do use it in some of my config at times. Not necessarily for entity names but also other stuff as well.

You’re probably correct since the video was about having buttons on the home page that link to various room pages. So, if I understand correctly, room is defined in the card as room: bedroom, and in the script ~room expands to “bedroom”?

What else would have scope to room: ?

well, ~ room becomes whatever the room variable is defined as.

so if it’s “room: kitchen” and you are concatenating it to “climate.” then the entity_id becomes “climate.kitchen”

I’m not really sure what you are asking.

“room:” could be anything. It doesn’t even really need to be a room name. it’s just a variable placeholder to hold whatever data you want it to hold.

Is this only useful in scripts?

It’s useful wherever you use Jinja templates and you need to connect two or more strings together, especially when those strings are being stored inside variables. That includes scripts, template sensors, automations, and dashboard cards like Markdown or Mushroom.

1 Like