Sonoff T1 US/AU and Ifan

Do you have a link?

Oops!

Not sure how true the AU approved bit it. But its an Aussie company with a store front.

Any Idea’s on the above problem? Im pulling my hair out.

Once the items arrive I’d be happy to look at it but I’m guessing you should have it sussed by then :wink:

I hope so :frowning: … what do you think of light switches?

They look a bit nicer I think they seem to have the ctick logo hard to know I think you should be able to check a website for ctick approval is legit. I have already just purchased enough T1 to cover all my outlets.

T1 look good but see my post here. Sonoff US/AU T1 Mounting Fitting Loose

Code below works but obviously you cant turn off the fan. So it must have something to do with how it passes the off state. Looking at the responses once you go from medium to high the state is always high unless you manually change the state.

- id: '1536982795485'
  alias: Fan2
  trigger:
  - entity_id: light.sonoff21
    platform: state
  action:
    service: mqtt.publish
    data_template:
      topic: cmnd/sonoff_MBR_fan/FanSpeed
      payload_template: >
        {% if states.fan.master_bedroom_fan.attributes.speed == 'off' %}
          1
        {% elif states.fan.master_bedroom_fan.attributes.speed == 'low' %}
          2
        {% elif states.fan.master_bedroom_fan.attributes.speed == 'medium' %}
          3
        {% elif states.fan.master_bedroom_fan.attributes.speed == 'high' %}
          1
        {% endif %}

Ok, I see the problem.

The speed attribute doesn’t get updated to off when the state goes to ‘off’. That seems like a bug to me. :frowning_face:

Here’s a work around. replace your payload template with this:

    payload_template: >
      {% if states.fan.master_bedroom_fan.state == 'off' %}
        1
      {% elif states.fan.master_bedroom_fan.attributes.speed == 'low' %}
        2
      {% elif states.fan.master_bedroom_fan.attributes.speed == 'medium' %}
        3
      {% elif states.fan.master_bedroom_fan.attributes.speed == 'high' %}
        0
      {% endif %}

I’ve tested it with 3 complete cycles and it worked ok for me.

1 Like

thanks buddy. will try tonight and report back.

@finity thanks mate. its is all working. I appreciate all the help you gave me.

Complete config for anyone else who wants to do this.

configuration.yaml

fan:
  - platform: mqtt  
    name: "Master Bedroom Fan"
    command_topic: "cmnd/sonoff_MBR_fan/FanSpeed"
    speed_command_topic: "cmnd/sonoff_MBR_fan/FanSpeed"    
    state_topic: "stat/sonoff_MBR_fan/RESULT"
    speed_state_topic: "stat/sonoff_MBR_fan/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.master_bedroom_fan.attributes.speed == 'off' -%}0{%- elif states.fan.master_bedroom_fan.attributes.speed == 'on' -%}2{%- endif %}
      {% endif %}
    speed_value_template: "{{ value_json.FanSpeed }}"
    availability_topic: tele/sonoff_MBR_fan/LWT
    payload_off: "0"
    payload_on: "2"
    payload_low_speed: "1"
    payload_medium_speed: "2"
    payload_high_speed: "3"
    payload_available: Online
    payload_not_available: Offline
    speeds:
      - off
      - low
      - medium
      - high 

automations.yaml

- id: '1536982795485'
  alias: Master Fan Automation
  trigger:
  - entity_id: light.sonoff21
    platform: state
  action:
    service: mqtt.publish
    data_template:
      topic: cmnd/sonoff_MBR_fan/FanSpeed
      payload_template: >
        {% if states.fan.master_bedroom_fan.state == 'off' %}
          1
        {% elif states.fan.master_bedroom_fan.attributes.speed == 'low' %}
          2
        {% elif states.fan.master_bedroom_fan.attributes.speed == 'medium' %}
          3
        {% elif states.fan.master_bedroom_fan.attributes.speed == 'high' %}
          off
        {% endif %}

thanks everyone

3 Likes

I just tried your code and the fan started toggling 0 to 3 over and over again.

Are you using the ifan02? What mqtt messages are you seeing?

Yes iFan02 with Sonoff-Tasmota 6.2.1.
Not sure how to check mqtt messages…

what are you using as your trigger device?

For that instance I just selected a speed from HA. My plan is to use a Sonoff button.

thats why. when you use the switch change as a triggger it will get stuck in a loop.

Yes, there is no reason to use the automation code from this thread if you only want to control the fan with the normal HA front end.

the OP had a specific desire to control the speed of the fan using a separate binary device which the code here solved for his use.

I have the same intention I just happened to use the HA interface.
I just tried it again with the automation using a Node Red flow set to toggle the fan and got the fan speed cycling from 0-3 over and over.

post the exact code you are using for your automation.

Sorry I did something dumb. I had the trigger set as the fan instead of the switch. It’s working great now!