How to use if/then statements to create dictionary variables in scripts

So, probably a newbie question here and I feel like this has probably been answered somewhere but lots of googling has turned up nothing. I have a script where I’m trying to populate a dictionary-type variable based on a select-type helper using the values of some number-type helpers. In the template editor in developer tools, I have this working perfectly:

  {% if mode == "cool" %} 
    {% set setpoints = {'occupied' : states('input_number.hvac_default_temp_occupied_cool'),
                        'empty' : states('input_number.hvac_default_temp_empty_cool'),
                        'overnight' : states('input_number.hvac_default_temp_overnight_cool') } %}
  {% elif mode == "heat" %}
    {% set setpoints = {'occupied' : states('input_number.hvac_default_temp_occupied_heat'),
                        'empty' : states('input_number.hvac_default_temp_empty_heat'),
                        'overnight' : states('input_number.hvac_default_temp_overnight_heat') } %}
  {% endif %}

Eventually, what I want to be able to do is the following:

service: input_number.set_value
target:
  entity_id: input_number.hvac_bedroom_setpoint
data: 
  value: "{{ setpoints['occupied'] }}"

My big question is where to put the first block in the actual script - I keep getting errors any time I try to put that in variables, but I don’t know where else it would go. Thanks for any help!

The following example assumes the variable mode is defined elsewhere in the script or has been passed to the script.

  - variables:
      setpoints:
        occupied: "{{ states('input_number.hvac_default_temp_occupied_' ~ mode) }}"
        empty: "{{ states('input_number.hvac_default_temp_empty_' ~ mode) }}"
        overnight: "{{ states('input_number.hvac_default_temp_overnight_' ~ mode) }}"
  - service: input_number.set_value
    target:
      entity_id: input_number.hvac_bedroom_setpoint
    data: 
      value: "{{ setpoints['occupied'] }}"
3 Likes