Sonoff T1 US/AU and Ifan

Hi,

I am trying to get the iFan02 to turn on when you press a button on the Sonnof T1 highest setting first then when you press the button again it goes to median then low then off.

Is this acheivable?

Thanks

Ben

I think there are a couple of ways to do it.

You could do it with several automations in which you use the button click to turn on the fan to different speeds depending on what the current state of the iFan is.

For example you could make it so that the speed is changed to high only if the current condition of the speed is ‘off’. then you could make it change to medium only if the current condition is ‘high’. And so on…

Or if you are good at templates you could probably do the above in one automation using a service template.

Thanks, I will start to look at automations. Do you have any links to tutorials or examples?

Only the standard HA web pages for automations and templating. And for other real world examples there is the “cookbook” where users post there configurations for inspirations to others.

And if you have more specific questions you can always ask on the forums for additional help.

I can now turn the fan on and off but need an if statement to determine current speed to select the correct action.

- id: '1536982795485'
  alias: Fan2
  trigger:
    platform: state
    entity_id: light.sonoff21
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: fan.pat_ceiling_fan
    data:
      speed: 1
- id: '1536982795486'
  alias: Fan22
  trigger:
    platform: state
    entity_id: light.sonoff21
    to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: fan.pat_ceiling_fan

I don’t understand what you are saying/asking…

Is there a question there?

You could also do this with Tasmota rules and have each switch talk to the other unit via MQTT.

Hi Thanks for the replies. I will try to explain myself better.

I have a sonoff ifan02 hooked to my fan. I also have a sonoff T1US light switch. I want to be able to press the light switch to turn on the fan and change the speeds. e.g press once turn from off to High speed press it again and its turns to medium press it again and it turns to low then press it again and it turns off.

With the code above I can turn on the fan from the T1 light switch but only to low speed (code snippet from above)

entity_id: fan.pat_ceiling_fan
    data:
      speed: 1

I think i need something like this to make it work how I want it. Just not sure of the exact syntax.

- id: Light_Driveway
  alias: Driveway light
  trigger:
platform: mqtt
topic: light/driveway/set
  action:
service: light.turn_on
entity_id: light.driveway_light_4
data_template:
  brightness_pct: >
    {% if( trigger.payload|int > 1) and ( states.light.driveway_light_4.state == 'off') %}
      50
    {% else %}
      {% if trigger.payload|int < 100 %}
        {{ trigger.payload|int }}
      {% else %}
        100
      {% endif %}
    {% endif %}

See the top of the page for how to format your code properly and edit your post please.

Done. Thought did do that :confused:

I sort of have it working. I can now go from off to low then low to medium but it wont go from medium to high then high to off.

- id: '1536982795485'
  alias: Fan2
  trigger:
  - entity_id: light.sonoff21
    platform: state
  action:
    service: homeassistant.turn_on
    entity_id: fan.pat_ceiling_fan
    data_template:
      speed: >
        {% if states.fan.pat_ceiling_fan.state == 'off' %}
          low
        {% else %}
            {% if states.fan.pat_ceiling_fan.state == low %}
              medium
            {% else %}
                {% if states.fan.pat_ceiling_fan.state == 'medium' %}
                    high
                {% else %}
                    {% if states.fan.pat_ceiling_fan.state == 'high' %}
                        off
                    {% endif %}        
                {% endif %} 
            {% endif %} 
        {% endif %}

try:

    data_template:
      speed: >
        {% if states.fan.pat_ceiling_fan.state == 'off' %}
          low
        {% elif states.fan.pat_ceiling_fan.state == low %}
          medium
        {% elif states.fan.pat_ceiling_fan.state == 'medium' %}
          high
        {% elif states.fan.pat_ceiling_fan.state == 'high' %}
          off
        {% endif %}

Hi finity,

Thanks for your suggestion. Still the same?? its really strange!!

- id: '1536982795485'
  alias: Fan2
  trigger:
  - entity_id: light.sonoff21
    platform: state
  action:
    service: homeassistant.turn_on
    entity_id: fan.pat_ceiling_fan
    data_template:
      speed: >
        {% if states.fan.pat_ceiling_fan.state == 'off' %}
          low
        {% elif states.fan.pat_ceiling_fan.state == low %}
            medium
        {% elif states.fan.pat_ceiling_fan.state == 'medium' %}
            high
        {% elif states.fan.pat_ceiling_fan.state == 'high' %}
            off
        {% endif %}

Ok, the next thing I see is that some of your states have ’ marks and some don’t. Try putting ’ marks around all of them:

‘low’

I didn’t catch that before.

If that doesn’t work then try taking off all of them. But I’m pretty sure they need to be there.

unfortunately the same…

I think it has something to do with the code below from my configuration.yaml file how the state_value_template is defined…

fan:
  - platform: mqtt  
    name: "Pat Ceiling Fan"  
    state_topic: "stat/ifan02_1/RESULT"
    speed_state_topic: "stat/ifan02_1/RESULT"
    state_value_template: >
        {% if value_json.FanSpeed is defined %}
          {% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}2{%- endif %}
        {% else %}
          {% if states.fan.pat_ceiling_fan.state == 'off' -%}0{%- elif states.fan.pat_ceiling_fan.state == 'on' -%}2{%- endif %}
        {% endif %}

Ok.

a couple of other things to try…

try to change the template to the way I have mine. I had an issue with the way the status showed up when I used that value of 2 in my state template. That’s why I changed it and posted the change on the tasmota wiki.

{% if value_json.FanSpeed is defined %}
  {% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}4{%- endif %}
{% else %}
  {% if states.fan.master_bedroom_fan.state == 'off' -%}0{%- elif states.fan.master_bedroom_fan.state == 'on' -%}4{%- endif %}
{% endif %}

the other thing to try is to see what MQTT messages are being sent around when you try your code.

But I still think it might have something to do with those single quote marks somehow.

What is your code that you last tried that wouldn’t work?

Unfortunately, I can’t try it now since my wife is asleep and I don’t think she would appreciate me turning on & off the bedroom lights and changing the fan speed randomly. :fearful: :face_with_head_bandage: :laughing:

I tried your code but it doesnt seem to work. I really think it has to do the with config.yaml code. The reason I think this is because no matter if I go from off to high then high to medium then to low or if I go from off to low then low to medium then to high, it always stops at medium. The first 2 steps always work just not the last 2.

I’ll try to test it as soon as I can and get back to you. Likely won’t be till Tuesday sometime.

Thanks mate I really appreciate your help… Told wife they would be kid/wife friendly :rofl:

Try this in your action:

  service: mqtt.publish
  data_template:
    topic: "cmnd/sonoff_MBR_fan/FanSpeed"
    payload_template: >
        {% if states.fan.pat_ceiling_fan.attributes.speed == 'off' %}
          1
        {% elif states.fan.pat_ceiling_fan.attributes.speed == 'low' %}
          2
        {% elif states.fan.pat_ceiling_fan.attributes.speed == 'medium' %}
          3
        {% elif states.fan.pat_ceiling_fan.attributes.speed == 'high' %}
          0
        {% endif %}

You’ll have to be sure that you modify it with your correct speed topic.

1 Like