Using template output string to supply entity_id for another template value

Hi there I have a somewhat unreliable weather station hooked up to HA with rflink - the problem is that the the station will periodically reset the entity_id for the reported values (I have auto-add on rflink which picks up the change).
I have written a template to find the entity_id of the station which is of the format:
dkw2012_00xx_yyy
where
xx is a hexadecimal value (00 to ff) which is assigned when the station drops connection, and
yyy is the station measurement (temp, hum, rain etc.)

I can generate a template output to provide an ID for each measurement:
states.sensor.{{states.input_text.weather_station_rf_name.state}}temp.state
which outputs:
states.sensor.dkw2012_00f0_temp.state
however, I would like to evaluate this above to get the state of the measurement
{{states.sensor.dkw2012_00f0_temp.state}}
to give the latest temperature measuremnet.

I have tried
{{states.sensor.{{states.input_text.weather_station_rf_name.state}}temp.state}}
but gives the error:
TemplateSyntaxError: expected name or number

Please can you point me in the right direction - I have been searching for a while now and sitll not come up with anyting.

Have you tried

{{ states('sensor.dkw2012_00f0_temp') }}

in the developers tool → template to see if it outputs the temperature?

Why don’t just define all possible values as aliases for your sensors?

Yup that template does show up the temperature when it’s tested in the dev tools area.

As for adding aliases for the rflink that was my preference but this solution doesn’t persist with reboots if the id has changed. I did test it with all 255 combinations but it would only work if the first valve was detected when the sensor is activated. Hence my convoluted solution. If you are interested here is the template that I us to get the station id:

    {% for state in states if ( (state.entity_id.startswith("sensor.dkw2012_00")
    and state.entity_id.endswith("_temp") and
    ((now()-state.last_updated).total_seconds()<300) ) )  -%}
    {{state.name[:13]}} {%- endfor %}

Description: loop all states and id states matching station id syntax which has been updated in the last 5min. This will occasionally return 2 IDs after the station changes its id but that is okay as it will fix itself next time they template runs.

Making some progress:
here is the input to the template tool:

sensor.{{states.input_text.weather_station_rf_name.state}}temp
{{ states('sensor.{{states.input_text.weather_station_rf_name.state}}temp') }}
{{ states('sensor.dkw2012_00f0_temp') }}

and the output:

sensor.dkw2012_00f0_temp
unknown
25.1

so there is a reading form the sensor but the template doesnt seem get it - I cant see a typo either…

{{ states('sensor.'  ~ states('input_text.weather_station_rf_name') ~ 'temp') }}

EDIT: Fixed error with braces and use states on the inner call too.

1 Like

Thanks parautenbach,

I tried this and the template tool gave the following error:

TemplateSyntaxError: expected token ':', got '}'

it looks like the first pair of close curly brackets is beeing interpreted see below:
image

After a bit more playing around I realised that I needed to replace the curly brackets with the tilde to get it working:
{{ states('sensor.'~states.input_text.weather_station_rf_name.state~'temp') }}
which solves the problem.
Templates are really powerful houwever I have trouble with the syntax.

Thanks for the assistance.

You’re welcome. Sorry, I was in a hurry and on my phone, so that was my very brief reply – and that error slipped in as it was untested. Glad you got it working. I’ll also update my post above. I think, it’s also better to use the states function on the internal state.

Thanks again,

Just an update - the second bracket for the inner states call wasnt closedm should have been:

{{ states('sensor.' ~ states('input_text.weather_station_rf_name') ~ 'temp') }}
1 Like

For future reference, it can also be done like this:

{{ states('sensor.{}temp'.format(states('input_text.weather_station_rf_name'))) }}

2 Likes