Sonoff T1 US/AU and Ifan

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!

I’m glad you got it figured out.

Here’s a Node Red flow in case anyone wants to do something similar.

[
    {
        "id": "d3400336.a2973",
        "type": "api-render-template",
        "z": "38ba64c4.52010c",
        "name": "Fan Speed",
        "server": "6882cc0e.116284",
        "template": "{% if states.fan.master_bedroom_fan.state == 'off' %}\n          1\n        {% elif states.fan.master_bedroom_fan.attributes.speed == 'low' %}\n          2\n        {% elif states.fan.master_bedroom_fan.attributes.speed == 'medium' %}\n          3\n        {% elif states.fan.master_bedroom_fan.attributes.speed == 'high' %}\n          off\n        {% endif %}",
        "x": 350,
        "y": 840,
        "wires": [
            [
                "ed96b250.1183"
            ]
        ]
    },
    {
        "id": "ed96b250.1183",
        "type": "switch",
        "z": "38ba64c4.52010c",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "off",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "2",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "3",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 4,
        "x": 630,
        "y": 840,
        "wires": [
            [
                "a6eda8f6.1b44e8"
            ],
            [
                "b2e4cb3d.4ec9f8"
            ],
            [
                "c7e80dc0.1fa1"
            ],
            [
                "39b078ae.c70378"
            ]
        ]
    },
    {
        "id": "b2e4cb3d.4ec9f8",
        "type": "mqtt out",
        "z": "38ba64c4.52010c",
        "name": "1",
        "topic": "cmnd/bedfan/FanSpeed",
        "qos": "",
        "retain": "",
        "broker": "af0c5364.e7f1c",
        "x": 830,
        "y": 820,
        "wires": []
    },
    {
        "id": "a6eda8f6.1b44e8",
        "type": "mqtt out",
        "z": "38ba64c4.52010c",
        "name": "0",
        "topic": "cmnd/bedfan/FanSpeed",
        "qos": "",
        "retain": "",
        "broker": "af0c5364.e7f1c",
        "x": 830,
        "y": 780,
        "wires": []
    },
    {
        "id": "c7e80dc0.1fa1",
        "type": "mqtt out",
        "z": "38ba64c4.52010c",
        "name": "2",
        "topic": "cmnd/bedfan/FanSpeed",
        "qos": "",
        "retain": "",
        "broker": "af0c5364.e7f1c",
        "x": 830,
        "y": 860,
        "wires": []
    },
    {
        "id": "39b078ae.c70378",
        "type": "mqtt out",
        "z": "38ba64c4.52010c",
        "name": "3",
        "topic": "cmnd/bedfan/FanSpeed",
        "qos": "",
        "retain": "",
        "broker": "af0c5364.e7f1c",
        "x": 830,
        "y": 900,
        "wires": []
    },
    {
        "id": "eaa70b77.aa1368",
        "type": "api-current-state",
        "z": "38ba64c4.52010c",
        "name": "Sonoff Switch",
        "server": "6882cc0e.116284",
        "halt_if": "",
        "override_topic": true,
        "override_payload": true,
        "entity_id": "switch.sonoff_switch",
        "x": 120,
        "y": 840,
        "wires": [
            [
                "d3400336.a2973"
            ]
        ]
    },
    {
        "id": "6882cc0e.116284",
        "type": "server",
        "z": "",
        "name": "Home Assistant",
        "url": "http://hassio/homeassistant",
        "pass": "xxxxxxx"
    },
    {
        "id": "af0c5364.e7f1c",
        "type": "mqtt-broker",
        "z": "",
        "name": "MQTT Server",
        "broker": "x.x.x.x",
        "port": "1883",
        "tls": "493cde56.2a596",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "493cde56.2a596",
        "type": "tls-config",
        "z": "",
        "name": "",
        "cert": "",
        "key": "",
        "ca": "",
        "certname": "",
        "keyname": "",
        "caname": "",
        "verifyservercert": true
    }
]
1 Like

I need to learn Node Red!

Node red will change your life :wink:

Any tutorial suggestions?

There are few good ones but it’s best to just jump in and start with a few easy automations like motion lights or xiaomi button triggers and work your way up as you grow in confidence. The community is really helpful with flows I’d be happy to help where I can my github has automations maintained in both yaml and node-red. You can see how one done in yaml is done in node-red they are side by side in my packages folders.

Thanks mate. Will check it out.

@benmprojects try this, and his subsequent articles

1 Like

Thanks mate very gooood tutorial!

1 Like

@benmprojects so the Sonoff Wall plates did not meet the WAF which makes sense as they are pretty damn ugly. I needed to use the standard 3 speed wall switch to trigger the fan also.

I have found a 240v to 5v step down and have placed a ESP12 behind the wall plate with each of the switch toggles tied to ground and one of the GPIO pins. Whenever the switch is moved it sends a MQTT message with its state, I use this state in Node-RED to trigger the iFAN02 MQTT CMND topic. I realize these can get out of sync so Node-RED is only listening for an event change as far as the wall switch is concerned, so I can automate the fans off when I want. From there all you need to do to get the fan to activate from the wall switch is to trigger a state change aka move the switch to a new position.

1 Like