Help with template to get entity state using variable for entity name

hi all, can someone help me do that the right way?

Temperature:
  speech:
    text: >
      {{'The temperature at the '}}{{ Room }}{{' is '}}
      {% set temp = states('sensor.{{ Room }}_temperature') %}{{temp}}
      {{' degrees. '}}

I don’t think you can convert a string to an entity like that.
Try this:

{%-for state in states.sensor-%}
    {%- if state.name == Room -%}
        The temperature at the {{ Room }} is {{state.state}} degrees.
    {%-endif%}
{%-endfor%}
Temperature:
  speech:
    text: >
      {% set x = 'room_name' %}
      {% set y = 'sensor.%s_temperature' | format(x) %}
      The temperature at the {{ x }} is {{ y }} degrees.

Should work as long as room name is the same as sensor temperature.

You can get any attribute from a device too:

{% set x = states.group.room_1.name %}

There are other attributes located in other areas if name isn’t want you are looking for. Use the template page in HA to look for attributes in states objects.

Also, the {{ }} is only needed around objects you want to pull info from. You went a little over kill on your initial post with them.

I tried that, and I get this:

The temperature at the garage is sensor.garage_temperature degrees

I’m an idiot.

The temperature at the {{ x }} is {{ states(y) }} degrees.

2 Likes

Hi Petro, this is so close to what I need - but I need the object attributes from (y)…

Is that possible?

EDIT: aahhh nevermind, it just dawned on me.

{{ state_attr(y, 'value') }}