Set thermostat temperature from one of 2, based on state attribute of another

I have 2 thermostats, named ‘living_room’ and ‘tv_room’. The returned temperature is saved in ‘climate.ac_currenttemp’, which happens to be a virtual thermostat in Hubitat Integration.

This automation works OK:

service: climate.set_temperature
target:
device_id: 01b0d4df48ec86f84488cafa28e80a0f
entity_id: climate.ac_currenttemp
data:
temperature: “{{states.climate[‘living_room’].attributes.current_temperature}}”

However, I want this automation to return the temperature of either ‘living_room’ OR ‘tv_room’ based on the value of another device (another virtual thermostat in Hubitat). That device is identified as: “sensor.ac_unit_id_temperature”.

So I try this syntax in my automation, and it does not work:

service: climate.set_temperature
target:
device_id: 01b0d4df48ec86f84488cafa28e80a0f
entity_id: climate.ac_currenttemp
data:
temperature: |-
{% if is_state(“sensor.ac_unit_id_temperature” , ‘1’) %}
{{ states.climate[‘living_room’].attributes.current_temperature }}
{% elif is_state(“sensor.ac_unit_id_temperature” , ‘2’) %}
{{ states.climate[‘tv_room’].attributes.current_temperature }}
{% endif %}

The error returned is: “expected float for dictionary value @ data[‘temperature’]. Got None”

Any idea? Not sure why it works in a singular return value but not in the template. I’m very new to YAML, so I’m sure it’s just a syntax or it needs a pipe to convert the number. And sorry if my code is not formatted correctly in this post, I was getting the “Unformatted Code Warning”. Thanks.

So many issues.
First please use state_attr(‘entity’,‘attribute’) rather than states.domain[‘sensor’].attributes.attribute.

temperature: |-
{% if is_state('sensor.ac_unit_id_temperature' , '1') %}
{{ state_attr('climate.living_room','current_temperature')|float(0) }}
{% elif is_state('sensor.ac_unit_id_temperature' , '2') %}
{{ state_attr('climate.tv_room','current_temperature')|float(0) }}
{% endif %}

and if there is only ever going to be either 1 or 2 from the state of ac_unit_id_temperature then you can reduce it to:

temperature: |-
{% if is_state('sensor.ac_unit_id_temperature' , '1') %}
{{ state_attr('climate.living_room','current_temperature')|float(0) }}
{% else %}
{{ state_attr('climate.tv_room','current_temperature')|float(0) }}
{% endif %}

I’d probably even simply it further:

{% set t = 'climate.living_room' if is_state('sensor.ac_unit_id_temeprature','1') else 'climate.tv_room' %}
{{ state_attr(t,'current_temperature')|float(0) }}

Thanks for the prompt and detailed reply! I will have 7 choices, so I tried your first solution and I get the same error as before.

I should have noticed this earlier - but try:

temperature: >-

instead of

temperature: |-

Sorry for the delay, had my Raspberry Pi die on me…
So I edited it as suggested, and same error.

The YAML editor seems to revert the notation from
temperature: >-
back to
temperature: |-

Wonder if it’s the indentation?
Because YAML is picky.

temperature: >-
  {{ jinja code indented 2 spaces just like normal yaml }}

I’m struggling to get this syntax to work. Maybe I can add a script with if-then conditions?

Can you post your YAML again but this time -
Pressing the button that looks like this </> so it formats properly just so we can check for any other odities.

That button does not show up for me:

service: climate.set_temperature
target:
  device_id: 01b0d4df48ec86f84488cafa28e80a0f
  entity_id: climate.ac_currenttemp
data:
  temperature:  |-
    {% if is_state('sensor.ac_unit_id_temperature' , '1') %}
      state_attr('climate.bedford_living_room','current_temperature')|float(0)
    {% elif is_state('sensor.ac_unit_id_temperature' , '2') %}
      state_attr('climate.bedford_tv_room','current_temperature')|float(0)
    {% endif %}
alias: TEST -- Current room temperature to be read by Hubitat

This code in another automation works to set the entity_id, so I’m confused why the failing automation can’t set the temperature.

alias: Call a service 'Climate: Set target temperature' on templated entities
service: climate.set_temperature
target:
  entity_id: |-
    {% if is_state("sensor.ac_unit_id_temperature" , '0') %} 
      null
    {% elif is_state("sensor.ac_unit_id_temperature" , '1') %} 
      climate.bedford_living_room
    {% elif is_state("sensor.ac_unit_id_temperature" , '2') %} 
      climate.bedford_tv_room
    {% elif is_state("sensor.ac_unit_id_temperature" , '3') %} 
      climate.bedford_bedroom
    {% elif is_state("sensor.ac_unit_id_temperature" , '4') %} 
      climate.bedford_peggy_s_office
     {% elif is_state("sensor.ac_unit_id_temperature" , '5') %} 
      climate.bedford_jim_s_office
    {% elif is_state("sensor.ac_unit_id_temperature" , '6') %} 
      climate.samoset_living_room
    {% elif is_state("sensor.ac_unit_id_temperature" , '7') %} 
      climate.samoset_bedroom
    {% endif %} 
data:
  temperature: "{{ (states('sensor.ac_unit_temp_temperature'))|int }}"

Oh I see the problem you are missing the bit that tells jinja “execute this”

data:
  temperature:  |-
    {% if is_state('sensor.ac_unit_id_temperature' , '1') %}
      {{ state_attr('climate.bedford_living_room','current_temperature')|float(0) }}
    {% elif is_state('sensor.ac_unit_id_temperature' , '2') %}
      {{ state_attr('climate.bedford_tv_room','current_temperature')|float(0) }}
    {% endif %}

Those double brackets {{ are super important.

I really appreciate the help. It still give this error:
expected float for dictionary value @ data[‘temperature’]. Got None

I have tested this in the template editor, and get errors until I change it to devices I actually have. Which is leading me to wonder what exactly sensor.ac_unit_id_temperature is showing?

sensor.ac_unit_id_temperature is a virtual thermostat on my Hubitat that I’m using to identify the parameters of each AC unit in Cielo Home that I want to set. I’m sure there’s a more elegant method, but I’m trying to set values on the Hubitat side and then trigger HA to see these values and use them to set the climate entities (my AC units).

As mentioned above, this same virtual thermostat is used in if-else logic to identify the entity_id. So it should also work for determining what AC unit temperature should be set in climate.ac_currenttemp.

Can you go to the Developer tools and click on Template Editor and just paste this in:

{% if is_state('sensor.ac_unit_id_temperature' , '1') %}
      {{ state_attr('climate.bedford_living_room','current_temperature')|float(0) }}
    {% elif is_state('sensor.ac_unit_id_temperature' , '2') %}
      {{ state_attr('climate.bedford_tv_room','current_temperature')|float(0) }}
    {% endif %}

And see if we get a number as a result, or a Jinja error?

Looks to be no result or error:

Result type: string

This template listens for the following state changed events:

  • Entity: sensor.ac_unit_id_temperature

But when I put in his I get a correct value of the sensor as temperature:

data:
  temperature: "{{ (states('sensor.ac_unit_temp_temperature'))|int }}"

OK so the likely issue here then is that currently the sensor.ac_unit_id_temperature is not currently 1 or 2, so none of the if tests are matched.

Well, it did happen to be something other than 1 or 2, but I just set it to 1 and the template editor shows it as 1, and the automation still fails. Regardless of the value being compared, the automation does not like the syntax, so it won’t even run.

Why can the same syntax to determine entity_id not also work for temperature, like this:

service: climate.set_temperature
target:
  entity_id: |-
    {% elif is_state("sensor.ac_unit_id_temperature" , '1') %} 
      climate.bedford_living_room
    {% elif is_state("sensor.ac_unit_id_temperature" , '2') %} 
      climate.bedford_tv_room
    {% endif %} 
data:
  temperature: "{{ (states('sensor.ac_unit_temp_temperature'))|int }}"