What type of template would work for a HVAC fan?

4 hours ago I started what I expected to be a straightforward UI button in lovelace. The button when tapped should toggle the HVAC fan on or off (obviously the opposite of the current state). This climate system doesn’t have a toggle on the fan it only responds to climate.set_fan_mode with value on / off.

I expected that a template switch was what I wanted. Alas, I now understand that this doesn’t execute different sections of code depending on the template value. I’m basically looking for a stupid dumb simple if then else statement I think. Some awful pseudo code now:

if fan == on then turn it off
else if fan == off then turn it on

I have the value extracted nicely using a template and can access the attribute I want with

{{ states.climate.upstairs.attributes['fan_state'] }}

You can see the data structure returned here - https://imgur.com/a/odb7BlO. Note I have been flipping around between fan_mode and fan_state all evening but I’m happy with the current state of the value_template using fan_state which returns 1 or 0.

I’m getting a bit tired I suppose but it’d be great to have someone weigh in with the correct solution here. I think what I want is a switch that when toggled sets the climate.set_fan_mode to the opposite value of what it was, also reading in the current value of the fan if Home Assistant restarts to ensure we don’t get out of sync.

Thanks in advance.

Welcome to the community!

You want a template switch. This needs to be placed into configuration.yaml. It may already have a switch section. If so, just copy the chunch from -platform: template to the end under your current switch section. If you don’t have it, just copy the whole thing. You can only have 1 switch section.

switch:
- platform: template
  switches:
    hvac_fan:
      value_template: "{{ is_state_attr('climate.upstairs', 'fan_state', 'on') }}"
      turn_on:
        service: climate.set_fan_mode
        data:
          entity_id: climate.upstairs
          fan_mode: 'on'
      turn_off:
        service: climate.set_fan_mode
        data:
          entity_id: climate.upstairs
          fan_mode: 'off'

This will show up as a switch entity with the entity_id of switch.hvac_fan.

For what it’s worth, normally fan modes on climate devices have states like high, low, auto etc. Yours seems to be odd in that it only has on/off.

EDIT: Also, you may need to adjust the fan_mode values because these are not standard and not translated. so ‘on’ might need to change to ‘On’ etc. This also means you’d need to update it in the template line as well.

1 Like

I thought I’d tried that already, maybe the missing thing was the non-standard values. I’ll have a play. Thanks for the reply.

Did you get this working? I’m trying the same and getting:
Entity climate.aircon does not support this service.