Setting fan speed via mqtt

Hi all!
I’ve a problem in setting the speed of a fan via mqtt.

Given this code (esphome 1.17.2):

substitutions:
  display_name: test_d1mini_2
  use_address: !secret iot_ip_test_d1mini_2
  static_ip: !secret iot_ip_test_d1mini_2

esphome:
  platform: ESP8266
  board: d1_mini
  name: ${display_name}

wifi:
  use_address: ${use_address}
  ssid: !secret iot_wifi_ssid
  password: !secret iot_wifi_password
  fast_connect: true
  power_save_mode: none
  reboot_timeout: 10min
  manual_ip:
    static_ip: ${static_ip}
    gateway: !secret iot_network_gateway
    subnet: !secret iot_network_subnet
    dns2: !secret iot_network_dns2
    dns1: !secret iot_network_dns1

web_server:
  port: 80
    
logger:
    
api:
  reboot_timeout: 0s

ota:
  password: !secret ota_password

mqtt:
  broker: !secret mqtt_broker
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery: false

status_led:
  pin:
    number: D4
    inverted: True

output:
  - platform: esp8266_pwm
    pin: D6
    frequency: 2200 Hz
    id: pwm_fan_output
    min_power: 0.25

fan:
  - platform: speed
    output: pwm_fan_output
    name: ${display_name} fan
    id: myfan
    speed_count: 100
    speed_state_topic: ${display_name}/fan/myfan/speed_state
    speed_command_topic: ${display_name}/fan/myfan/speed_command

If I add a new esphome integration in homeassistant, I can set the speed in 100 different steps, with no problem. So there’s no error with this code and with the esphome api.
When I try to set the speed via MQTT, I can only use the “old” speed settings (“low”, “medium”, “high”) which get translated to 33%, 66% and 100%.

With this command:

~ mosquitto_pub  -h mqttserver -u mqttuser -P mqttpass -t "test_d1mini_2/fan/myfan/speed_command" -m "low"

I get this on the mqtt debug:

2021-05-15T14:18:06+0200 - test_d1mini_2/fan/myfan/speed_command - low
2021-05-15T14:18:06+0200 - test_d1mini_2/debug - [D][speed.fan:035]: Setting speed: 0.33
2021-05-15T14:18:06+0200 - test_d1mini_2/debug - [D][mqtt.fan:090]: 'test_d1mini_2 fan' Sending state ON.
2021-05-15T14:18:06+0200 - test_d1mini_2/fan/test_d1mini_2_fan/state - ON
2021-05-15T14:18:06+0200 - test_d1mini_2/fan/myfan/speed_state - low

If I ask for 77% with this command:

~ mosquitto_pub  -h mqttserver -u mqttuser -P mqttpass -t "test_d1mini_2/fan/myfan/speed_command" -m "77"

This is simply ignored, because “77” doesn’t correspond to a valid speed set (I assume).

2021-05-15T14:20:58+0200 - test_d1mini_2/fan/myfan/speed_command - 77
2021-05-15T14:20:58+0200 - test_d1mini_2/debug - [D][speed.fan:035]: Setting speed: 0.33
2021-05-15T14:20:58+0200 - test_d1mini_2/debug - [D][mqtt.fan:090]: 'test_d1mini_2 fan' Sending state ON.
2021-05-15T14:20:58+0200 - test_d1mini_2/fan/test_d1mini_2_fan/state - ON
2021-05-15T14:20:58+0200 - test_d1mini_2/fan/myfan/speed_state - low

I found no way to set, let’s say, 25% speed via MQTT.

Is anybody aware of a different method?

thanks

1 Like

have you managed to figure this out or find an alternative? I am trying this as well, but have not found an alternative yet

The only alternative, at the moment, is to use the esphome api. The api is working well.

If you need to use MQTT, you’re stuck with the three fixed speed settings.