Sonoff T1 US/AU and Ifan

Hi Mate,

You wouldnt beleive it. I tried almost that same code last night! the difference being the .attributes.speed part but sadly it did the same thing… I will try your code when I get home.

I had a look at the state of the fan when it goes to medium speed and instead of the state being medium it is on.

So I thought easy fix and tried the code below. By changing medium to on.

- 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 == 'on' %}
            high
        {% elif states.fan.pat_ceiling_fan.state == 'high' %}
            off
        {% endif %}

But now it goes from low to high and the state is always ‘on’ not the speed. Is there a way to set the state from the automations code e.g. (code below). That way the state would not go to ‘on’ and then the if statement would work.

{% elif states.fan.pat_ceiling_fan.state == 'low' %}
        states.fan.pat_ceiling_fan.state == 'medium'  
        medium
{% elif states.fan.pat_ceiling_fan.state == 'medium' %}
        states.fan.pat_ceiling_fan.state == 'high' 
        high

Thanks for your help.

No.

The state will always be on or off. That’s why the if statement when looking at the state wasn’t working. You have to look at the speed attribute to compare.

I put the code above in my setup (using my details of course) and it worked exactly the way you want it to as far as my understanding of what you want…

1 Like

excellent. cant wait to try it… Thanks again

can I get a copy of the fan: part from your config.yaml

- 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 -%}4{%- endif %}
    {% else %}
      {% if states.fan.master_bedroom_fan.state == 'off' -%}0{%- elif states.fan.master_bedroom_fan.state == 'on' -%}4{%- endif %}
    {% endif %}
  speed_value_template: "{{ value_json.FanSpeed }}"
  availability_topic: tele/sonoff_MBR_fan/LWT
  payload_off: "0"
  payload_on: "4"
  payload_low_speed: "1"
  payload_medium_speed: "2"
  payload_high_speed: "3"
  payload_available: Online
  payload_not_available: Offline
  speeds:
    - off
    - low
    - medium
    - high

I do this with a 4ch sonoff but am moving to the exact switch and ifan as you are trying to use ordered all gear this morning. I accomplish this by calling scenes which select the correct input and ensure others are switched off. If you can’t get it to work feel free to have a look at my github

Wow just had a look at your github, its amazing!. Seeing as your in Australia like me, are you worried that the sonoff devices arent certified?

1 Like

Thanks.

Yeah it does concern me about lack of certification but that would drive prices up and lets face it that is their most attractive feature. I was contemplating using the Xiaomi wall switch but its design is based around UK wall plate dimensions and they don’t do anything more then a 2 gang switch. Certified wall plates are really expensive in Australia to the point it would make the project unfeasible.

finity, your code works!! but only once until I reload hass then it works again.Looking at the mqtt messages in MQTT.fx once it has completed one cycle its get stuck on fan speed 0 .

cmnd/sonoff_MBR_fan/FanSpeed
0
stat/sonoff_MBR_fan/RESULT
{"FanSpeed":0}

I tried to add the below code to the automatic but it didn’t work.

{% elif states.fan.master_bedroom_fan.attributes.speed == '0' %}
    1

Any ideas??

@jimpower

have a look at these switches. I have a few they are UL and ctick approved. I have flashed tasmota and have it working in Hassio.

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