How to do if in config yaml

Is testing conditions in the config yaml possible? I have a climate template that I what to pass temperature setting into am actual thermostat that only accepts heat set point and cool setpoint. It won’t take a single set temp when in cool mode or heat mode. I’ve tried {%if%} which made home assistant come back on in safe mode after restarting. I’ve tried all sorts of combinations of choose: and conditions: and condition: and while the only thing I’ve ever managed to make work yet is a single condition. The first check for cool will always work but I’ve never managed to make the check for heat work.

  # ---->>>> HVAC Infinitive MQTT<<<<----
  - platform: climate_template
    name: Hall Master
    unique_id: dd8a6da3-3eac-4f22-8b81-734f1eb97efe
    modes:
      - "off"
      - heat_cool
      - cool
      - heat
      #     - "auto"
      #     - "dry"

    #     - "fan_only"
    fan_modes:
      - auto
      - low
      - "medium"
      - "high"
    min_temp: 60
    max_temp: 80
    target_temperature_high_template: 80
    target_temperature_low_template: 60
    # get current temp.
    current_temperature_template: "{{ states('sensor.hvacc_hall_east_temp') }}"

    # get current humidity.
    current_humidity_template: "{{ states('sensor.hall_east_indoor_humidity') }}"

    set_temperature:
      conditions:
        - condition: state
          entity_id: climate.hall_master
          state: "cool"

        - service: climate.set_temperature
          data:
            entity_id:
              - climate.hall_east
              - climate.hall_middle
            target_temp_high: "{{ state_attr('climate.hall_master', 'temperature') | int }}"
            target_temp_low: 60

        - condition: state
          entity_id: climate.hall_master
          state: "heat"
        - service: climate.set_temperature
          data:
            entity_id:
              - climate.hall_east
              - climate.hall_middle
            target_temp_high: 80
            target_temp_low: "{{ state_attr('climate.hall_master', 'temperature') | int }}"
    #temperature: "{{ temperature }}"

Was I headed in the right direction with if and just need to try that more? Or is conditions what I need to be using here?

When asking a question about a custom integration, it is best practice to include a link to its repository… there can be multiple custom integrations with similar names. And, if they do similar things, they may have similar configuration schema, making it difficult for others to provide accurate help.

The set_temperature configuration variable in HASS Template Climate accepts actions, so you should be able to use a Choose action:

...
    set_temperature:
      - choose:
          - conditions:
              - condition: state
                entity_id: climate.hall_master
                state: "cool"
            sequence:
              - service: climate.set_temperature
                data:
                  entity_id:
                    - climate.hall_east
                    - climate.hall_middle
                  target_temp_high: "{{ state_attr('climate.hall_master', 'temperature') | int }}"
                  target_temp_low: 60
          - conditions:
              - condition: state
                entity_id: climate.hall_master
                state: "heat"
            sequence:
              - service: climate.set_temperature
                data:
                  entity_id:
                    - climate.hall_east
                    - climate.hall_middle
                  target_temp_high: 80
                  target_temp_low: "{{ state_attr('climate.hall_master', 'temperature') | int }}"
....

… or you may also be able to test the conditions within your templates:

...
    set_temperature:
      - variables:
          hall_temp: "{{ state_attr('climate.hall_master', 'temperature') | int(0) }}"
          hall_mode: "{{ states('climate.hall_master') }}"
      - service: climate.set_temperature
        data:
          entity_id:
            - climate.hall_east
            - climate.hall_middle
          target_temp_high: "{{ hall_temp if hall_mode == 'cool' else 80 }}"
          target_temp_low: "{{ hall_temp if hall_mode == 'heat' else 60 }}"
....
1 Like

Thanks a lot @Didgeridrew!! At one point I had -choose and -conditions in there and I was trying to add in sequence but it just wasn’t working. thanks for throwing out that test within templates. thats a lot cleaner I though I just about got it:

    set_temperature:
      - variables:
          hall_temp: "{{ state_attr('climate.hall_master', 'temperature') | int }}"
          hall_temp_low: "{{ state_attr('climate.hall_master', 'target_temp_low') | int }}"
          hall_temp_high: "{{ state_attr('climate.hall_master', 'target_temp_high') | int }}"
          hall_mode: "{{ states('climate.hall_master') }}"
          eco_low: "{{ states('input_number.hvacc_eco_low') }}"
          eco_hi: "{{ states('input_number.hvacc_eco_hi') }}"
      - service: climate.set_temperature
        data:
          entity_id:
            - climate.hall_east
            - climate.hall_middle
          target_temp_high: "{{ hall_temp if hall_mode == 'cool' else hall_temp_high if hall_mode == 'heat_cool' else eco_hi }}"
          target_temp_low: "{{ hall_temp if hall_mode == 'heat' else hall_temp_low if hall_mode == 'heat_cool' else eco_low }}"

but now that I’ve thrown in the other 2 variables of hall_temp_low and hall_temp_high there are times that these come back as null when setting temp from hall_temp and it completely locks it all up and reports an error. How can I make this ignore those null values and still run?

hall_temp: 0 if  "{{ state_attr('climate.hall_master', 'temperature') | int }}" == null

???

Is there some way to make a one liner like this work? Maybe I have to create another variable

For an inline if it would need to be inside the template delimiters,

hall_temp: |
  {% set temp = state_attr('climate.hall_master', 'temperature') %}
  {{ temp | int if temp is number else 0 }}

but in this case just add 0 as a default for the int filter.

hall_temp: "{{ state_attr('climate.hall_master', 'temperature') | int(0) }}"

Thanks @Didgeridrew
I just wish I knew why my googlefu was failing me when looking that up. It’s almost like yaml doesn’t support conditions.