How do I use a template in an automation followed by a choose?

Goal: automatically adjust the humidity setting on my thermostat based on temperature. The humidity setting will also be predictive if it is going to be particularly chilly overnight and the house needs time to lower the humidity.

What works: I have created an automation using the UI to adjust the humidity based on temperature using a choose action. I have also created a template that artificially sets a new temperature if the house needs time to dehumidify when the temperature drops very low at night. The template provides the desired solutions in the template helper.

Problem: I don’t know how to insert the template into an action so that the “artificial_temp” variable can be fed into the choose action which will set the humidity based on my temperature in the formula.

FYI: Adjusting the humidity in Canada is very normal because the sun of the day will often warm the temperature a lot but the temps can drop very low overnight (-40f). Also, I have converted from Fahrenheit to Celsius in the template.

Relevant Code: Below is the template I have created followed by the choose action.

{% set deg_diff_f = states('sensor.calgary_temperature') | float - states('sensor.calgary_low_temperature') | float%} ##calculates the difference in current temp and overnight low
{% set deg_diff_c = ((5/9)*(deg_diff_f -32)) | round(1)%} ##f to c conversion
{% set deg_current_c = ((5/9)*states('sensor.calgary_temperature')|int -32) | round(1)%} ##converts to degrees c and provides int for the if statment later
{% set deg_low_c = ((5/9)*states('sensor.calgary_low_temperature')|int -32) | round(1)%} ##converts f to c for use with else statement


{% if deg_diff_c < -9 and deg_low_c < -20  %} ##statement of the perameters that will artificially lower the actual temperature for humidity purposes ***temp is default in degrees f***
{% set artificial_temp = states('sensor.calgary_temperature')|int - 5|int %} ##amount of temperature in degress the artifical temp is lowered
{% else %}
{% set artificial_temp = deg_current_c %} ##actual temp data gets set if the perameters are not met to adjust
{% endif %}

choose:
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: 4
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 50
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: -1
        below: 4
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 48
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: -6
        below: -0.99
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 46
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: -12
        below: -5.9
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 42
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: -17
        below: -11.9
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 34
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: -22
        below: -16.9
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 32
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        above: -29
        below: -21.9
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 32
  - conditions:
      - condition: numeric_state
        entity_id: sensor.artificial_temp_for_auto_humidity
        below: -28.9
    sequence:
      - device_id: 5cf32b5c819c22c2290ecb038cba2ec3
        domain: humidifier
        entity_id: d839e272c067e420a9af44dfa850ef33
        type: set_humidity
        humidity: 30
default: []

You can’t use templates in device actions. Use a set temperature climate service call instead.

Create a Template Sensor based on the template you have designed. Name it sensor.artificial_temp_for_auto_humidity.

Thank you both! That absolutely worked.