Sonoff iFan03 as MQTT fan: HA doesn't understand speed state

I have configured an iFan03 (with Tasmota) as MQTT fan based on some other topics here at the forum. That results in:

  - platform: mqtt  
    name: "Plafondventilator"
    command_topic: "cmnd/tasmota_8FAD22/FanSpeed"
    speed_command_topic: "cmnd/tasmota_8FAD22/FanSpeed"    
    state_topic: "stat/tasmota_8FAD22/RESULT"
    speed_state_topic: "stat/tasmota_8FAD22/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 == 'off' -%}0{%- elif states.plafondventilator.state == 'on' -%}4{%- endif %}
      {% endif %}
   # optimistic: true
    speed_value_template: "{{ value_json.FanSpeed }}"
    availability_topic: tele/tasmota_8FAD22/LWT
    payload_off: "0"
    payload_on: "1"
    payload_low_speed: "1"
    payload_medium_speed: "2"
    payload_high_speed: "3"
    payload_available: Online
    payload_not_available: Offline
      - "off"
      - "low"
      - "medium"
      - "high"

When I comment the optimistic: true, HA will wait for the state topic before setting state. Unfortunately it doesn’t set speed to 'off'.

Here it is on 'low' and 'on'.

Here I set the fan speed to 'off'.

The fan turns off, but the speed remains 'low'

When I uncomment optimistic: true almost all works as expected. When I change speed to 'off' through fan.set_speed, the fan turns off AND the speed changed to 'off'. But… when I change the speed to ‘off’ in the Tasmota interface it won’t change speed again (other speeds DO work though).

This is what Tasmota publishes in the state topic:

**qos**  : 0,  **retain**  : false,  **cmd**  : publish,  **dup**  : false,  **topic**  : stat/tasmota_8FAD22/RESULT,  **messageId**  : ,  **length**  : 42

**qos**  : 0,  **retain**  : false,  **cmd**  : publish,  **dup**  : false,  **topic**  : stat/tasmota_8FAD22/RESULT,  **messageId**  : ,  **length**  : 42

So it seems FanSpeed: 1 is understood (as the speed changes to 'low'), while FanSpeed: 0 isn’t understood by Home Assistant?

So what does Home Assistant expect as a value, as '0' isn’t working, maybe 'off'? How would I change speed_value_template to test?

I don’t think I did anything with the “optimistic” setting so I think that the default is false.

that said mine does the same as yours - the fan works fine and shows the correct state (off when not running and on when running in any speed) but when the state is off it just stays at the last selected speed for the speed setting. I don’t believe it should show the speed as “off”.

So why is there a ‘off’ speed setting then? Then it would be more logical to only have low/medium/high as speed settings and use on/off to toggle. It seems to me something is wrong.

I actually created template switches based on speed for low/medium/high. But now if the fan turns off (without optimistic: true) one of the speed template switches always stays on. When using optimistic I can set speed level to off (where it stays) and all speed template switches show an off state…

I don’t know what to tell you.

My iFan03 works just fine using the code I’ve posted in other threads (which it looks like your code above is based on I think) where the payload_on is set to 4 and optimistic is false.