Fan control with Shelly and fan-template

Anyone can help me out with the FAN template to control my type-D ventilation? I have a smart plug to turn then vent on and of, and a “shelly 2PM” to set the fan to MID and HI.

Off : smart plug turned OFF
low : smart plug ON, S1 & S2 shelly OFF.
mid: smart plug ON, S1 ON& S2 shelly OFF.
hi: smart plug ON, S1 & S2 shelly ON.

So I created a template with :

And this works :
image
BUT, when I check the entity and I want it to set to Low, Mid, High inside the GUI, the actual value is still ‘Off’ :


When I can change this value, I’m able to set the shelly with Node-Red, So at this moment I just need the above control, but the output needs to match the button I check.

You have no entry for set_preset_mode or preset_mode_template keys to tell the fan entity what the current state is or what to do to change it.

I would probably create a script to call to set presets as you will need to control both plug and shelly to get the right combination. Your preset_mode_template key can the be a template the evaluates the preset based on states of plug and shelly.

1 Like

And you can use something like this for your preset_mode_template

{% set ns = namespace(presets = ['off','low','medium','high']) %}
{{ ns.presets[
   (1 if is_state('switch.fan_switch', 'on') else 0)
   + (1 if is_state('switch.shelly1', 'on') else 0)
   + (1 if is_state('switch.shelly2', 'on') else 0 ) 
   ] 
}}

For your script…

alias: Fan Control
sequence:
  - variables:
      presets:
        - "off"
        - low
        - medium
        - high
  - if:
      - condition: template
        value_template: "{{ presets.index(preset) > 0 }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.fan_switch
    else:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.fan_switch
  - if:
      - condition: template
        value_template: "{{ presets.index(preset) > 1 }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.shelly1
    else:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.shelly1
  - if:
      - condition: template
        value_template: "{{ presets.index(preset) > 2 }}"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.shelly2
    else:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.shelly2
mode: single

And then set_preset_mode as this…

set_preset_mode:
          service: script.fan_control
          data:
            preset: "{{ preset_mode }}"

Hope that helps.

1 Like

Thanks !

This can put me on the right track, yes !
if it still gives issues, I’ll post them here.

Erwin

@msp1974 you made the difference, with some minor adjustments, this works for me. thanks a lot !

FYI, I have a dumb ventilation system with 3 positions, and a hardware 3 position switch.
Added a shelly in between :


So I can set the position from HA.
For it to be 100% correct, I read out the power from the ventilation, and according to that, I show the speed of the ventilator., so I can compare it with the switch.

Created a Node-Red flow to set the ventilator speed according to the humidity in some rooms.

But with your help, I’m also able to set it in the UI in a ‘fan’-like way :

would you mind posting your full code for this, I have a 2 speed dMEV that I would like to control via a shelly plus 2.5PM (Off, Low, High).

Don’t need NODE RED bit, just the template and any scripts for it to work.
Thanks.

Sure, some info :
Switches I used :

  • 1 smart plug
  • 1 shelly 2.5
switch.niko_ventilatie > smart plug to turn on and of complete ventilation.
switch.shelly_ventilatie > simulates 3 way switch 

switch.shelly_ventilatie_switch_0 & switch.shelly_ventilatie_switch_1 = OFF > preset 1
switch.shelly_ventilatie_switch_0 = ON > preset 2
switch.shelly_ventilatie_switch_1 = ON > preset 3

Script :

fan_control:
  alias: fan_control
  sequence:
    - variables:
        presets:
          - "off"
          - low
          - medium
          - high
    - if:
        - condition: template
          value_template: "{{ presets.index(preset) > 0 }}"
      then:
        - service: switch.turn_on
          data: {}
          target:
            entity_id:
              - switch.niko_ventilatie
      else:
        - service: switch.turn_off
          data: {}
          target:
            entity_id: switch.niko_ventilatie
    - if:
        - condition: template
          value_template: "{{ presets.index(preset) > 1 }}"
      then:
        - service: switch.turn_on
          data: {}
          target:
            entity_id: switch.shelly_ventilatie_switch_0
      else:
        - service: switch.turn_off
          data: {}
          target:
            entity_id: switch.shelly_ventilatie_switch_0
    - if:
        - condition: template
          value_template: "{{ presets.index(preset) > 2 }}"
      then:
        - service: switch.turn_on
          data: {}
          target:
            entity_id: switch.shelly_ventilatie_switch_1
      else:
        - service: switch.turn_off
          data: {}
          target:
            entity_id: switch.shelly_ventilatie_switch_1
  mode: single

Thanks, and what do I need for the actual new fan entity.

Hey @andyblac did you get this to work? I’m trying to accomplish something comparable and am getting stuck too.

My goal is to get a fan entity which i can put in off/low/high mode to steer my fan which is designed to be steered by a 3-way swich.

fan:
  - platform: template
    fans:
      preset_mode_fan:
        friendly_name: "House fan"
        turn_on:
          - service: switch.turn_on
            target:
              entity_id: switch.fan_switch_0
          - service: switch.turn_off
            target:
              entity_id: switch.fan_switch_1
        turn_off:
          - service: switch.turn_off
            target:
              entity_id: switch.fan_switch_0
          - service: switch.turn_off
            target:
              entity_id: switch.fan_switch_1
        speed_count: 2
        preset_modes:
          - "off"
          - "low"
          - "high"
        preset_mode_template: >
          {% if is_state('switch.fan_switch_0', 'on') %}
            {% if is_state('switch.fan_switch_1', 'on') %}
              high
            {% elif is_state('switch.fan_switch_1', 'off') %}
              low
            {% endif %}
          {% else %}
            off
          {% endif %}
        set_preset_mode: >
          {% if preset_mode == 'high' %}
          - service: switch.turn_on
            target:
              entity_id: 
              - switch.fan_switch_0
              - switch.fan_switch_1
          {% elif preset_mode == 'low' %}
          - service: switch.turn_on
            target:
              entity_id: 
              - switch.fan_switch_0
          - service: switch.turn_off
            target:
              entity_id: 
              - switch.fan_switch_1
          {% else %}
          - service: switch.turn_off
            target:
              entity_id: 
              - switch.fan_switch_0
              - switch.fan_switch_1
          {% endif %}

Unfortunately this does not work. All examples i can find seem to use an external script to set the preset mode but i would like to contain all logic in the configuration fan template.

Ideally the fan reports its speed back. Currently i use a seperate sensor for that:

  - sensor:
      - name: Fan speed
        unique_id: fan_rpm
        unit_of_measurement: "rpm"
        state: >
          {{ 84.253*float(states('sensor.fan_power'))+613.46 }}

sorry, but no, could not get it to work.