[FR] Conditional service for set_hvac_mode
The problem
I use some switchbot hub mini to replicate IR command for 3 air conditionner.
The switchbot integration create 2 entities for each air conditionner :
- a switch entity that can be used to switch on/off the air conditonner.
- a climate entity that can be used to manage hvac mode, temperature or fan mode, but the hvac_mode "off" is not supported by this entity.
I want to use climate_template to create a new "template entity" and to be able to do a conditional action for hvac_mode :
- if the template-climate hvac_mode is set to off, the called service must be switch.turn_off
- Else the called service must be climate.set_hvac_mode with the state of the "template_entity"
I have done some research (and the last is https://github.com/jcwillox/hass-template-climate/issues/65) but I did not succed to implement a solution that works.
Can you help me ?
Best regards,
What version of Template Climate has the issue?
1.0.1
What version of Home Assistant are you running?
2025.1.2
What type of installation are you running?
None
Example YAML snippet
- platform: climate_template
name: clim_chambre
unique_id: '6b33f07d-3e6b-471f-a648-aa53a8f88ed4'
modes:
- "auto"
- "dry"
- "off"
- "cool"
- "fan_only"
- "heat"
fan_modes:
- "auto"
- "low"
- "medium"
- "high"
min_temp: 16
max_temp: 30
# get current temp.
current_temperature_template: "{{ states('sensor.capteur_temperature_chambre_temperature') }}"
# get current humidity.
current_humidity_template: "{{ states('sensor.capteur_temperature_chambre_humidity') }}"
# example action
hvac_action_template: "{{ states('climate.clim_chambre' ) }}"
set_hvac_mode:
- service: climate.set_hvac_mode
data:
entity_id: climate.clim_parents
hvac_mode: "{{ states('climate.clim_chambre') }}"
set_fan_mode:
- service: climate.set_fan_mode
data:
entity_id: climate.clim_parents
fan_mode: "{{ state_attr('climate.clim_chambre', 'fan_mode') }}"
set_temperature:
- service: climate.set_temperature
data:
entity_id: climate.clim_parents
temperature: "{{ state_attr('climate.clim_chambre', 'temperature') | int }}"
Anything in the logs that might be useful for us?
Additional information
No response
That should be super easy to implement if I understand it right, just use an if statement and a state condition (if template entity is off) then execute the desired action.
The state update is done before the action so if you are setting HVAC mode to off then the state of the template entity will be off before the action runs, so you can use the the normal state condition.
Hello and thank you for your answer.
In principle it is actually not very complicated but I can't find the right syntax in the yaml configuration.
I have tried to put the if statement below or above the "set_hvac_mode" instruction, tried to move the indentation of the "{% if" statement, but still get the same error when reloading the configuration : "error while scanning for the next token found character '%' that cannot start any token" and do not understand why.
The last configuration tried :
set_hvac_mode:
{% if is_state("climate.clim_chambre", "off") %}
- service: switch.turn_off
entity_id: switch.clim_parents
{%- else -%}
- service: climate.set_hvac_mode
data:
entity_id: climate.clim_parents
hvac_mode: "{{ states('climate.clim_chambre') }}"
{%- endif %}
Thank you for your help.
regards,
Actions are not templates. They are inline scripts. I agree it can be ticky to get yaml right for scripts so you might best create a script in the UI and call that as the service. You can pass hvac_mode as data, and that will be a variable in your script which you can see in templates in the script. e.g. template condition for a script if (- if) would be {{ hvac_mode == 'off' }}