How to set a temperature condition based on the target temp from a thermostat?

Hi All,

I have a temperature sensor and a space heater connected to a switch in a room that’s consistently colder than the rest of the home. I’m trying to set triggers and conditions for an automation based on the target temperature set for the rest of the home from the thermostat.

How do I reference the target temperature from the thermostat to set the “below” and “above” attributes in my numeric_state conditions for the temperature sensor?

My thermostat entity id is ‘climate.upstairs’. If the target temperature were 19, I’d like to set that as the below threshold to turn on the switch for the heater, and if it goes above 19 + 1, I’d like to turn off the switch.

I assume I’ll need to use a template of some sort of this, but I’m having trouble finding info on how to get the target temperature from the climate entity using templates.

Any help or suggestions for another way to go about this would be appreciated.

Thanks.

I think it depends on your thermostat.

What do you see in Devloper tools → States when you look up that entity (especially the full set of tributes).

Also, what’s the entity ID of your thermometer?

hvac_modes: heat, cool, heat_cool, off
min_temp: 10
max_temp: 32
fan_modes: on, off
preset_modes: none, eco
current_temperature: 19.7
temperature: null
target_temp_high: 25.3
target_temp_low: 19.5
current_humidity: 59
fan_mode: off
hvac_action: idle
preset_mode: none
friendly_name: Upstairs
supported_features: 411

I’ve never used the Developer tools before. So I see target_temp_low is the attribute I’m after.

The entity id of my temp sensor is sensor.front_bedroom_temperature.

Right now, I have something like this:

- conditions:
  - condition: numeric_state
    entity_id: sensor.front_bedroom_temperature
    below: 19
  sequence:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.mini_smart_plug_1
- conditions:
  - condition: numeric_state
    entity_id: sensor.front_bedroom_temperature
    above: 20
  sequence:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.mini_smart_plug_1

So I’d like to reference the target_temp_low somehow to replace the 19 and 20 I currently have hardcoded.

What is your trigger? I’m not sure that this stuff should be going into the conditions, but I can’t tell without seeing the trigger(s).

  triggers:
  - trigger: numeric_state
    entity_id:
    - sensor.front_bedroom_humidity
    above: 55
  - trigger: numeric_state
    entity_id:
    - sensor.front_bedroom_humidity
    below: 45
  - trigger: numeric_state
    entity_id:
    - sensor.front_bedroom_temperature
    below: 19
  - trigger: numeric_state
    entity_id:
    - sensor.front_bedroom_temperature
    above: 20
  conditions:
  - condition: state
    entity_id: input_select.spatial_state
    state: Home
  actions:
  - choose:
    - conditions:
      - condition: state
        entity_id: input_select.mini_smart_plug_1_device
        state: Humidifier
      sequence:
      - choose:
        - conditions:
          - condition: numeric_state
            entity_id: sensor.front_bedroom_humidity
            below: 45
          sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.mini_smart_plug_1
        - conditions:
          - condition: numeric_state
            entity_id: sensor.front_bedroom_humidity
            above: 55
          sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.mini_smart_plug_1
    - conditions:
      - condition: state
        entity_id: input_select.mini_smart_plug_1_device
        state: Heater
      sequence:
      - choose:
        - conditions:
          - condition: numeric_state
            entity_id: sensor.front_bedroom_temperature
            below: 19
          sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.mini_smart_plug_1
        - conditions:
          - condition: numeric_state
            entity_id: sensor.front_bedroom_temperature
            above: 20
          sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.mini_smart_plug_1

Everything works fine, but I just want to replace the hardcode temperature values with references to my thermostats target_temp_low.

I tried replacing 19 with:

"{{ state_attr('climate.upstairs', 'target_temp_low') | float }}"

But I see an error saying String does not match the pattern of “^input_number..*$”

The “Template Preview” shows that it resolves to 19.5 though, so I’m not sure what the problem is.

I just discovered the template tab in the developer tools, and I can see the template in my last message returns “19.5” as a string. How do I convert the string to a float?

You don’t need to. If the below option supported templates the template you have would work. However below does not support templates. You can use input_numbers, but not templates:

https://www.home-assistant.io/docs/scripts/conditions/#numeric-state-condition

You would need to replace the numeric state condition with a template condition:

- condition: template
  value_template: "{{ states('sensor.front_bedroom_temperature')|float(42) < state_attr('climate.upstairs', 'target_temp_low') | float(0) }}"
1 Like

I think I got it now, but I’ll have to do some testing later. I couldn’t replace the “below” value for my numeric_state condition, but I could use a template condition

- condition: template
  value_template: "{{ states('sensor.front_bedroom_aqara_temperature_sensor_temperature') | float < state_attr('climate.upstairs_hallway', 'target_temp_low') | float }}"

@tom_l / @d921

Thanks for pointing me in the right direction! I never knew about the Developer tools, and I’ll need to get more familiar with where/when I can use templates.

Cheers,
Tim

Your solution doesn’t do what you said you wanted. But if it’s working for you, great.

This is the logic I ended up with after a bit of clean up. I was repeating the conditions in the triggers and the actions so I tried using trigger IDs for the first time and that seemed to work well:

  triggers:
  - trigger: template
    id: "temp_below_target"
    value_template: "{{ states('input_select.spatial_state') == 'Home' and 
                        states('sensor.front_bedroom_temperature') | float(19) < state_attr('climate.upstairs', 'target_temp_low') | float(12) }}"
  - trigger: template
    id: "temp_above_target"
    value_template: "{{ states('input_select.spatial_state') == 'Home' and 
                        states('sensor.front_bedroom_temperature') | float(19) > state_attr('climate.upstairs', 'target_temp_low') | float(12) + 1 }}"
  actions:
  - choose:
    - conditions:
      - condition: trigger
        id: 'temp_below_target'
      sequence:
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.mini_smart_plug_1
    - conditions:
      - condition: trigger
        id: 'temp_above_target'
      sequence:
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.mini_smart_plug_1

I ran into an issue where I previously had a automation condition that my ‘spatial_state’ was home, but I found the template triggers were only triggering when the template value changed from false to true… so it wouldn’t trigger when my ‘spatial_state’ changed from ‘away’ to ‘home’. So I ended up adding in the check on the ‘spatial_state’ in the value template for the triggers so they would get re-evaluated when the spatial_state changed.

It seemed to work when the temperature went above target and the switch turned off… Is there a more efficient way to test other than waiting for the test cases to naturally present themselves? i.e. is there a way I can “stub” the temperature sensor / climate target temp / spatial state values to test the triggers and conditions for a specific automation, without affecting/triggering all the other automations that are also impacted by those values?