What's wrong with my service template?

Hey there,

I copied a service_template from my working template fan and am trying to use it to control my heat pump. We only use four modes; off, dry, heat (23) and cool (18) so I’ve set this up an an input_select just like the template fan. However, when I select an option, nothing happens. The four scripts all work when fired on their own. Anyone see what I’ve done wrong?

input_select:
  office_aircon_mode_set:
    name: Office Aircon Mode
    options:
      - 'Off'
      - 'Cool 18'
      - 'Heat 23'
      - 'Dry'
    icon: mdi:fan
script:

  office_aircon_control:
    alias: Office Aircon Control
    sequence:
      - service_template: >
          {% if is_state("input_select.office_aircon_mode_set", "Off") %}
            script.office_aircon_off
          {% elif is_state("input_select.office_aircon_mode_set", "Cool 18") %}
            script.office_aircon_cool_18
          {% elif is_state("input_select.office_aircon_mode_set", "Heat 23") %}
            script.office_aircon_heat_23
          {% elif is_state("input_select.office_aircon_mode_set", "Dry") %}
            script.office_aircon_dry
          {% endif %} 

P.S When I use the new VSCode feature to render the template inside the editor, it correctly spits out the right script based on the selected option.

It should be an automation triggered by the input_select’s state-changes.

automation:
- alias: Office Aircon Control
  trigger:
    platform: state
    entity_id: input_select.office_aircon_mode_set
  action:
    - service_template: >
        script.office_aircon_{{ trigger.to_state.state.replace(' ', '_') | lower }}
1 Like

Thank you. Trying now. Blows my mind how you coding gurus come up with stuff! :stuck_out_tongue:

Any way to update the input_select if the scripts are called via another means?

The “another means” shouldn’t call the scripts directly. It should set the input_select to the desired setting using a service call.

      - service: input_select.select_option
        data:
          entity_id: input_select.office_aircon_mode_set
          option: 'Cool 18'

Thank you and of course, great advice. The automation is working a treat. Learning every day, even after 4 years at this! :grin:

Sorry to bug you again. Any idea on how I could build this out further to create my own heat_cool template switch based on the zone temperature?

I’ve built this on your advice above but I’m essentially wanting to add to it to check if the temperature is below 23 degrees, choose input_select ‘Heat 23’ and if it’s above, use ‘Cool 18’. Is that possible?

My hope is just to have the one template switch for the heat pump (that I can pass to Google and Alexa) and it sets the correct input_select option based on whether the temp is above or below 23 degrees and the sensor.hvac_office_power confirms whether the device is on or off.

  - platform: template
    switches:
      office_aircon_ts_heat_23:
        value_template: "{{ states('sensor.hvac_office_power')|float > 8 }}"
        friendly_name: "Office Aircon Heat 23 Template Switch"
        turn_on:
          service: input_select.select_option
          data:
            entity_id: input_select.office_aircon_mode_set
            option: 'Heat 23'
        turn_off:
          service: input_select.select_option
          data:
            entity_id: input_select.office_aircon_mode_set
            option: 'Off' 

OK, getting close but does not work. Turns on then straight off again.

  - platform: template
    switches:
      office_aircon_ts_auto:
        value_template: "{{ states('sensor.hvac_office_power')|float > 8 }}"
        friendly_name: "Office Aircon Template Switch"
        turn_on:
          service: input_select.select_option
          data_template:
            entity_id: input_select.office_aircon_mode_set
            option: >
              {% if states('sensor.temperature_xiaomi_office')|float <= 23 %} 
                'Heat 23'
              {% else %} 
                'Cool 18'
              {% endif %}
        turn_off:
          service: input_select.select_option
          data:
            entity_id: input_select.office_aircon_mode_set
            option: 'Off' 

That means there’s a problem with the value_template. The Template Switch uses that to determine its state (i.e. whether it considers itself to be on or off). You turn the switch on and then it evaluates value_template to set its state. If its state immediately returns back to off that means the value_template isn’t providing the correct state.

The simplest way to handle this is to use an input_boolean. The value_template simply reports the input_boolean’s state. The scripts you use must set the input_boolean’s state.

Note

I provided an example for a Template Fan in the following post. It uses an input_boolean to indicate the fan’s state. The input_boolean is set by the Template Fan’s turn_on and turn_off. I realize it’s not a Template Switch but the principle is the same.

Thank you, but that doesn’t explain why the one above it works (when manual setting options) What's wrong with my service template?. I think there is something wrong with the data_template and the input_select is not being called so the heat_pump doesn’t come on therefore no power is consumed and the switch turns off.

Here’s what may be happening:

  • Let’s say the input_select’s state is currently ‘Heat 23’.
  • The office temperature is <= 23.
  • You turn on the Template Switch.
  • That will cause it to set the input_select to ‘Heat 23’.
  • HOWEVER, the input_select is already at ‘Heat 23’ so there’s no state-change.
  • Without a state-change, the automation is not triggered and script.office_aircon_heat_23 is not executed.

To prove the theory, review the log. It reports when automations are executed. If automation.office_aircon_control isn’t executed then we know the theory is correct.

Thanks, good advice. I’m not getting to that step though. I would have thought that if the input_select was on ‘Off’ and I turn on the template switch, the input_select should change based on the temperature but that is not happening.

Cracked it! Don’t ask me why but the options in the data_template do not work with quotes. Removed those and it is working as intended now. Thank you so much for your help.

DOES NOT WORK

        turn_on:
          service: input_select.select_option
          data_template:
            entity_id: input_select.office_aircon_mode_set
            option: >
              {% if states('sensor.temperature_xiaomi_office')|float < 23 %} 
                'Heat 23'
              {% else %} 
                'Cool 18'
              {% endif %}
        turn_off:
          service: input_select.select_option
          data:
            entity_id: input_select.office_aircon_mode_set
            option: 'Off' 

WORKS, even though Off is still enclosed. Go figure.

        turn_on:
          service: input_select.select_option
          data_template:
            entity_id: input_select.office_aircon_mode_set
            option: >
              {% if states('sensor.temperature_xiaomi_office')|float < 23 %} 
                Heat 23
              {% else %} 
                Cool 18
              {% endif %}
        turn_off:
          service: input_select.select_option
          data:
            entity_id: input_select.office_aircon_mode_set
            option: 'Off'