Use of variables in configuration.yaml

I am configuring an OpenHasp Plate as a thermostat similar to the example given in Openhasp:
image

But I would like to be able to manage the thermostatic valves of 9 rooms with this plate. I could of course repeat the whole logic 9 times, each on a different page of the plate, but that would make the yaml file in Homeassistant very long and a nightmare to maintain. So I had the idea to create a variable and 9 buttons. The buttons (button matrix in OpenHasp) would set the variable according to the selected button, and the thermostat logic would then use this variable to access the correct thermostatic valve and adjust it accordingly, something like the image below:

So used the “variables+history” custom integration from HACS and defined a variable like this:

variable:
  thermroom:
    value: 'climate.trv_room1'

For some lines of my YAML file, this seems to work. When I substitute

- obj: "p1b20" # arc slider
      properties:
        "val": >
          {% if state_attr("climate.trv_room1","temperature") is not none %}
          {{ state_attr("climate.trv_room1","temperature") | int * 10 }}
          {%- endif %}

with

    - obj: "p1b20" # arc slider
      properties:
        "val": >
          {% if state_attr(states(variable.thermroom),"temperature") is not none %}
          {{ state_attr(states(variable.thermroom),"temperature") | int * 10 }}
          {%- endif %}

but substituting

      event:
        "changed":
          - service: climate.set_temperature
            target:
              entity_id: climate.trv_room1
            data:
              temperature: "{{ val | int / 10 }}"

with

      event:
        "changed":
          - service: climate.set_temperature
            target:
              entity_id: "{{ states(variable.thermroom) }}"
            data:
              temperature: "{{ val | int / 10 }}"

I get an error message… is it possible to use this variable to determine the entity_id ?

Try this:

        target:
          entity_id: "{{ states('variable.thermroom') }}"

Thanks for the reply. Unfortunately I had no luck with it…

I just had a look at the documentation, it’s pretty good on how to set and update the variables but there’s not much on how to use them, but from this:

https://github.com/snarky-snark/home-assistant-variables#updating-using-tracked-event-types

value_template: "{{ (states('var.y') | int) * ( states('var.z') | int) }}"

it would appear that this is the correct format:

What do each of these return (separately so they bad ones don’t error) in the developer tools template editor?

{{ states('var.thermroom') }}
{{ states('variable.thermroom') }}
{{ states(var.thermroom) }}
{{ states(variable.thermroom) }}

Hi,

Seeing the link you mention in your reply, we might be talking about something different. In HACS there are 2 integrations that can be used to create variables. One is called Variables (the one by snarky-snark) and the other is called variables+history by wibias/rogro82
For no specific reason (I didn’t study their differences very much) I decided to use the latter one.

I also just realised that my code examples have an error: the working examples use quotes which I did not show in my previous post:

{% if state_attr(states('variable.thermroom'),"temperature") is not none %}
 {{ state_attr(states('variable.thermroom'),"temperature") | int * 10 }}

That said, of the 4 choices you mention, only the first one works.
After that, I also tried out the service call to climate.set_temperature in the developer tools services editor and your first suggestion

 entity_id: "{{ states('variable.thermroom') }}" 

works fine in there…

it should just be

              entity_id: "{{ variable.thermroom }}"

states() gets the state of the entity_id. Entity_id expects the entity_id, which is what’s stored in the variable.

Thanks,

I will give this a try tomorrow, but in the meantime I tried again with states(‘variable.thermroom’) and it seems to work fine!!

Results of my trials:

entity_id: "{{ states('variable.thermroom') }}" 

is the one that works. By using the “variables” integration from HACS, defining a variable this way:

variable:
  thermroom:
    value: 'climate.trv_room1'

creates an entity “variable.thermroom” whose state is “climate.trv_room1”

Thanks for the help… this was one step forward in my intent of having a multi-thermostat screen on an openHasp plate. Next step is choosing which valve’s values are displayed on the thermostat gauge depending on which button in the button-matrix has been pressed.
I will open another topic because I am struggling with the yaml/jinja2 combination which is rather new to me and I don’t succeed to grasp its rather weird (at least to me) indentation and spacing rules…